Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codebase improvements with php 8.1 features #548

Merged
merged 39 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a5a9a1c
psr/container v2
l-you Oct 21, 2022
d33dc1a
Implement replacement for Picotainer
l-you Oct 21, 2022
6109c7d
Replace Picotainer with LazyContainer in tests and documentation for …
l-you Oct 21, 2022
5c78848
Fix tests for phpdocumentor/type-resolver minor update
l-you Oct 21, 2022
f67c09c
run cs-fix script
l-you Oct 21, 2022
95822e4
Fix some cs-fixer errors manually
l-you Oct 21, 2022
1bd69fe
fix return type
l-you Oct 21, 2022
9a50bd3
abandon psr/container ^1
l-you Oct 21, 2022
307777f
Test for LazyContainer
l-you Oct 24, 2022
af0a1f3
Normalize RootTypeMapperInterface
l-you Oct 28, 2022
e428ddf
Merge branch 'thecodingmachine:master' into master
l-you Oct 28, 2022
895ecce
Merge remote-tracking branch 'origin/master'
l-you Oct 28, 2022
77f5466
Merge branch 'thecodingmachine:master' into master
l-you Nov 9, 2022
cb5c523
Merge branch 'thecodingmachine:master' into master
l-you Dec 17, 2022
e7e49e1
end-to-end test with enums in mutation and as single-returned field
l-you Dec 17, 2022
4e4d68b
Merge branch 'thecodingmachine:master' into master
l-you Jan 9, 2023
9d3f3e7
Bump ecodev/graphql-upload and webonyx/graphql-php"
l-you Jan 12, 2023
822b397
Fix some tests for webonyx ^15.0
l-you Jan 12, 2023
5bb2201
clearer types to fix tests
l-you Jan 12, 2023
9043189
clearer types to fix failing tests for webonyx 15
l-you Jan 12, 2023
c5ed626
clearer types to fix failing tests for webonyx 15
l-you Jan 12, 2023
0889599
fix failing tests for webonyx 15.
l-you Jan 12, 2023
119290c
fix failing tests for webonyx 15.
l-you Jan 13, 2023
8d20d2d
fix some breaking changes for webonyx 15
l-you Jan 13, 2023
1a55852
fix breaking changes for webonyx 15
l-you Jan 13, 2023
dd63344
Update continuous_integration.yml
oojacoboo Jan 13, 2023
7953638
Specify PHP 8.1 minimum support
oojacoboo Jan 13, 2023
64dc04f
Merge branch 'thecodingmachine:master' into master
l-you Jan 21, 2023
4ada4f9
Add 'final' keyword to all classes marked with @internal phpdoc
l-you Jan 21, 2023
6c8cd70
Improve code with php 8.1 features: 'readonly' keyword, native inters…
l-you Jan 21, 2023
9e8b5dd
Improve code with php 8.1 features: 'readonly' keyword, native inters…
l-you Jan 21, 2023
6601ce4
Improve code with php 8.1 features: 'readonly' keyword, native inters…
l-you Jan 21, 2023
6b7d9a7
Improve code with php 8.1 features: str_starts_with
l-you Jan 21, 2023
82894f3
Improve code with php 8.1 features: readonly modifier
l-you Jan 21, 2023
0201925
Improve code with php 8.1 features: intersection types
l-you Jan 21, 2023
4c566f9
Improve code with php 8.1 features: intersection types and readonly m…
l-you Jan 21, 2023
c66d5ae
Improve code with php 8.1 features: intersection types and readonly m…
l-you Jan 21, 2023
933c91e
Bump phpstan to minimum version 1.9. Fixes failed tests with --prefer…
l-you Jan 21, 2023
9b9c7d6
Merge branch 'thecodingmachine:master' into master
l-you Jan 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"myclabs/php-enum": "^1.6.6",
"php-coveralls/php-coveralls": "^2.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^8.5.19 || ^9.5.8",
"symfony/var-dumper": "^5.4 || ^6.0",
"thecodingmachine/phpstan-strict-rules": "^1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/AggregateControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AggregateControllerQueryProvider implements QueryProviderInterface
* @param iterable<string> $controllers A list of controllers name in the container.
* @param ContainerInterface $controllersContainer The container we will fetch controllers from.
*/
public function __construct(private iterable $controllers, private FieldsBuilder $fieldsBuilder, private ContainerInterface $controllersContainer)
public function __construct(private readonly iterable $controllers, private readonly FieldsBuilder $fieldsBuilder, private readonly ContainerInterface $controllersContainer)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/AggregateControllerQueryProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AggregateControllerQueryProviderFactory implements QueryProviderFactoryInt
* @param iterable<string> $controllers A list of controllers name in the container.
* @param ContainerInterface $controllersContainer The container we will fetch controllers from.
*/
public function __construct(private iterable $controllers, private ContainerInterface $controllersContainer)
public function __construct(private readonly iterable $controllers, private readonly ContainerInterface $controllersContainer)
{
}

Expand Down
40 changes: 11 additions & 29 deletions src/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AnnotationReader
* @param string $mode One of self::LAX_MODE or self::STRICT_MODE. If true, no exceptions will be thrown for incorrect annotations in code coming from the "vendor/" directory.
* @param array<int,string> $strictNamespaces Classes in those namespaces MUST have valid annotations (otherwise, an error is thrown).
*/
public function __construct(private Reader $reader, private string $mode = self::STRICT_MODE, private array $strictNamespaces = [])
public function __construct(private readonly Reader $reader, private readonly string $mode = self::STRICT_MODE, private readonly array $strictNamespaces = [])
{
if (! in_array($mode, [self::LAX_MODE, self::STRICT_MODE], true)) {
throw new InvalidArgumentException('The mode passed must be one of AnnotationReader::LAX_MODE, AnnotationReader::STRICT_MODE');
Expand Down Expand Up @@ -100,20 +100,11 @@ private function getClassAnnotation(ReflectionClass $refClass, string $annotatio
assert($type === null || $type instanceof $annotationClass);
return $type;
} catch (AnnotationException $e) {
switch ($this->mode) {
case self::STRICT_MODE:
throw $e;

case self::LAX_MODE:
if ($this->isErrorImportant($annotationClass, $refClass->getDocComment() ?: '', $refClass->getName())) {
throw $e;
}

return null;

default:
throw new RuntimeException("Unexpected mode '" . $this->mode . "'."); // @codeCoverageIgnore
}
return match ($this->mode) {
self::STRICT_MODE=> throw $e,
self::LAX_MODE=>$this->isErrorImportant($annotationClass, $refClass->getDocComment() ?: '', $refClass->getName()) ? throw $e : null,
default=>throw new RuntimeException("Unexpected mode '" . $this->mode . "'.") // @codeCoverageIgnore
};
}
}

Expand All @@ -139,20 +130,11 @@ private function getMethodAnnotation(ReflectionMethod $refMethod, string $annota

return $this->methodAnnotationCache[$cacheKey] = $this->reader->getMethodAnnotation($refMethod, $annotationClass);
} catch (AnnotationException $e) {
switch ($this->mode) {
case self::STRICT_MODE:
throw $e;

case self::LAX_MODE:
if ($this->isErrorImportant($annotationClass, $refMethod->getDocComment() ?: '', $refMethod->getDeclaringClass()->getName())) {
throw $e;
}

return null;

default:
throw new RuntimeException("Unexpected mode '" . $this->mode . "'."); // @codeCoverageIgnore
}
return match ($this->mode) {
self::STRICT_MODE=> throw $e,
self::LAX_MODE=>$this->isErrorImportant($annotationClass, $refMethod->getDocComment() ?: '', $refMethod->getName()) ? throw $e : null,
default=>throw new RuntimeException("Unexpected mode '" . $this->mode . "'.") // @codeCoverageIgnore
};
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/FactoryContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
final class FactoryContext
{
public function __construct(
private AnnotationReader $annotationReader,
private TypeResolver $typeResolver,
private NamingStrategyInterface $namingStrategy,
private TypeRegistry $typeRegistry,
private FieldsBuilder $fieldsBuilder,
private TypeGenerator $typeGenerator,
private InputTypeGenerator $inputTypeGenerator,
private RecursiveTypeMapperInterface $recursiveTypeMapper,
private ContainerInterface $container,
private CacheInterface $cache,
private InputTypeValidatorInterface|null $inputTypeValidator,
private int|null $globTTL,
private int|null $mapTTL = null,
private readonly AnnotationReader $annotationReader,
private readonly TypeResolver $typeResolver,
private readonly NamingStrategyInterface $namingStrategy,
private readonly TypeRegistry $typeRegistry,
private readonly FieldsBuilder $fieldsBuilder,
private readonly TypeGenerator $typeGenerator,
private readonly InputTypeGenerator $inputTypeGenerator,
private readonly RecursiveTypeMapperInterface $recursiveTypeMapper,
private readonly ContainerInterface $container,
private readonly CacheInterface $cache,
private readonly InputTypeValidatorInterface|null $inputTypeValidator,
private readonly int|null $globTTL,
private readonly int|null $mapTTL = null,
) {
}

Expand Down
42 changes: 17 additions & 25 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
use function key;
use function reset;
use function rtrim;
use function strpos;
use function str_starts_with;
use function trim;

use const PHP_EOL;
Expand All @@ -75,16 +75,16 @@ class FieldsBuilder
private TypeHandler $typeMapper;

public function __construct(
private AnnotationReader $annotationReader,
private RecursiveTypeMapperInterface $recursiveTypeMapper,
private ArgumentResolver $argumentResolver,
private TypeResolver $typeResolver,
private CachedDocBlockFactory $cachedDocBlockFactory,
private NamingStrategyInterface $namingStrategy,
private RootTypeMapperInterface $rootTypeMapper,
private ParameterMiddlewareInterface $parameterMapper,
private FieldMiddlewareInterface $fieldMiddleware,
private InputFieldMiddlewareInterface $inputFieldMiddleware,
private readonly AnnotationReader $annotationReader,
private readonly RecursiveTypeMapperInterface $recursiveTypeMapper,
private readonly ArgumentResolver $argumentResolver,
private readonly TypeResolver $typeResolver,
private readonly CachedDocBlockFactory $cachedDocBlockFactory,
private readonly NamingStrategyInterface $namingStrategy,
private readonly RootTypeMapperInterface $rootTypeMapper,
private readonly ParameterMiddlewareInterface $parameterMapper,
private readonly FieldMiddlewareInterface $fieldMiddleware,
private readonly InputFieldMiddlewareInterface $inputFieldMiddleware,
)
{
$this->typeMapper = new TypeHandler($this->argumentResolver, $this->rootTypeMapper, $this->typeResolver);
Expand Down Expand Up @@ -342,7 +342,7 @@ private function getFieldsByMethodAnnotations(string|object $controller, Reflect
$methodName = $refMethod->getName();

if ($queryAnnotation instanceof Field) {
if (strpos($methodName, 'set') === 0) {
if (str_starts_with($methodName, 'set')) {
continue;
}
$for = $queryAnnotation->getFor();
Expand Down Expand Up @@ -673,12 +673,8 @@ private function getMagicGetMethodFromSourceClassOrProxy(ReflectionClass $proxyR
return $sourceRefClass->getMethod($magicGet);
}

/**
* @param ReflectionClass<object> $refClass
*
* @return OutputType&Type
*/
private function resolveOutputType(string $outputType, ReflectionClass $refClass, SourceFieldInterface $sourceField): OutputType
/** @param ReflectionClass<object> $refClass */
private function resolveOutputType(string $outputType, ReflectionClass $refClass, SourceFieldInterface $sourceField): OutputType&Type
{
try {
return $this->typeResolver->mapNameToOutputType($outputType);
Expand All @@ -688,12 +684,8 @@ private function resolveOutputType(string $outputType, ReflectionClass $refClass
}
}

/**
* @param ReflectionClass<object> $refClass
*
* @return OutputType&Type
*/
private function resolvePhpType(string $phpTypeStr, ReflectionClass $refClass, ReflectionMethod $refMethod): OutputType
/** @param ReflectionClass<object> $refClass */
private function resolvePhpType(string $phpTypeStr, ReflectionClass $refClass, ReflectionMethod $refMethod): OutputType&Type
{
$typeResolver = new \phpDocumentor\Reflection\TypeResolver();

Expand Down Expand Up @@ -850,7 +842,7 @@ private function getInputFieldsByMethodAnnotations(string|object $controller, Re

$docBlockObj = $this->cachedDocBlockFactory->getDocBlock($refMethod);
$methodName = $refMethod->getName();
if (strpos($methodName, 'set') !== 0) {
if (! str_starts_with($methodName, 'set')) {
continue;
}
$name = $fieldAnnotations->getName() ?: $this->namingStrategy->getInputFieldNameFromMethodName($methodName);
Expand Down
10 changes: 5 additions & 5 deletions src/Http/WebonyxGraphqlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ final class WebonyxGraphqlMiddleware implements MiddlewareInterface
];

public function __construct(
private ServerConfig $config,
private ResponseFactoryInterface $responseFactory,
private StreamFactoryInterface $streamFactory,
private HttpCodeDeciderInterface $httpCodeDecider,
private string $graphqlUri = '/graphql',
private readonly ServerConfig $config,
private readonly ResponseFactoryInterface $responseFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly HttpCodeDeciderInterface $httpCodeDecider,
private readonly string $graphqlUri = '/graphql',
StandardServer|null $handler = null,
)
{
Expand Down
2 changes: 1 addition & 1 deletion src/InputField.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @phpstan-import-type InputObjectFieldConfig from InputObjectField
* @phpstan-import-type ArgumentType from InputObjectField
*/
class InputField extends InputObjectField
final class InputField extends InputObjectField
{
/** @var callable */
private $resolve;
Expand Down
7 changes: 3 additions & 4 deletions src/InputFieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class InputFieldDescriptor
{
private string $name;
/** @var (InputType&Type)|(InputType&Type&NullableType) */
private InputType $type;
private InputType&Type $type;
/** @var array<string, ParameterInterface> */
private array $parameters = [];
/** @var callable|null */
Expand Down Expand Up @@ -96,13 +96,12 @@ public function setName(string $name): void
}

/** @return ((InputType&Type)|(InputType&Type&NullableType)) */
public function getType(): InputType
public function getType(): InputType&Type
{
return $this->type;
}

/** @param (InputType&Type) $type */
public function setType(InputType $type): void
public function setType(InputType&Type $type): void
{
$this->type = $type;
}
Expand Down
4 changes: 2 additions & 2 deletions src/InputTypeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
class InputTypeUtils
{
public function __construct(
private AnnotationReader $annotationReader,
private NamingStrategyInterface $namingStrategy,
private readonly AnnotationReader $annotationReader,
private readonly NamingStrategyInterface $namingStrategy,
)
{
}
Expand Down
22 changes: 11 additions & 11 deletions src/Mappers/AbstractTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ abstract class AbstractTypeMapper implements TypeMapperInterface

public function __construct(
string $cachePrefix,
private TypeGenerator $typeGenerator,
private InputTypeGenerator $inputTypeGenerator,
private InputTypeUtils $inputTypeUtils,
private ContainerInterface $container,
private AnnotationReader $annotationReader,
private NamingStrategyInterface $namingStrategy,
private RecursiveTypeMapperInterface $recursiveTypeMapper,
private CacheInterface $cache,
private readonly TypeGenerator $typeGenerator,
private readonly InputTypeGenerator $inputTypeGenerator,
private readonly InputTypeUtils $inputTypeUtils,
private readonly ContainerInterface $container,
private readonly AnnotationReader $annotationReader,
private readonly NamingStrategyInterface $namingStrategy,
private readonly RecursiveTypeMapperInterface $recursiveTypeMapper,
private readonly CacheInterface $cache,
protected int|null $globTTL = 2,
private int|null $mapTTL = null,
private readonly int|null $mapTTL = null,
)
{
$this->cacheContract = new Psr16Adapter($this->cache, $cachePrefix, $this->globTTL ?? 0);
Expand Down Expand Up @@ -185,7 +185,7 @@ private function buildMapClassToExtendTypeArray(): GlobExtendTypeMapperCache
$globExtendTypeMapperCache = new GlobExtendTypeMapperCache();

$classes = $this->getClassList();
foreach ($classes as $className => $refClass) {
foreach ($classes as $refClass) {
$annotationsCache = $this->mapClassToExtendAnnotationsCache->get($refClass, function () use ($refClass) {
$extendAnnotationsCache = new GlobExtendAnnotationsCache();

Expand Down Expand Up @@ -320,7 +320,7 @@ public function mapClassToInputType(string $className): ResolvableMutableInputIn
* @throws CannotMapTypeExceptionInterface
* @throws ReflectionException
*/
public function mapNameToType(string $typeName): Type
public function mapNameToType(string $typeName): Type&NamedType
{
$typeClassName = $this->getMaps()->getTypeByGraphQLTypeName($typeName);

Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/CompositeTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function mapClassToInputType(string $className): ResolvableMutableInputIn
*
* @return NamedType&Type&((ResolvableMutableInputInterface&InputObjectType)|MutableInterface)
*/
public function mapNameToType(string $typeName): Type
public function mapNameToType(string $typeName): Type&NamedType
{
foreach ($this->typeMappers as $typeMapper) {
if ($typeMapper->canMapNameToType($typeName)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/GlobExtendAnnotationsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @internal
*/
class GlobExtendAnnotationsCache
final class GlobExtendAnnotationsCache
{
private string|null $extendTypeClassName = null;

Expand Down
6 changes: 1 addition & 5 deletions src/Mappers/Parameters/ContainerParameterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
*/
class ContainerParameterHandler implements ParameterMiddlewareInterface
{
/** @var ContainerInterface */
private $container;

public function __construct(ContainerInterface $container)
public function __construct(private readonly ContainerInterface $container)
{
$this->container = $container;
}

public function mapParameter(ReflectionParameter $parameter, DocBlock $docBlock, Type|null $paramTagType, ParameterAnnotations $parameterAnnotations, ParameterHandlerInterface $next): ParameterInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/Parameters/InjectUserParameterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class InjectUserParameterHandler implements ParameterMiddlewareInterface
{
public function __construct(private AuthenticationServiceInterface $authenticationService)
public function __construct(private readonly AuthenticationServiceInterface $authenticationService)
{
}

Expand Down
9 changes: 4 additions & 5 deletions src/Mappers/Parameters/TypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ class TypeHandler implements ParameterHandlerInterface
private PhpDocumentorTypeResolver $phpDocumentorTypeResolver;

public function __construct(
private ArgumentResolver $argumentResolver,
private RootTypeMapperInterface $rootTypeMapper,
private TypeResolver $typeResolver,
private readonly ArgumentResolver $argumentResolver,
private readonly RootTypeMapperInterface $rootTypeMapper,
private readonly TypeResolver $typeResolver,
)
{
$this->phpDocumentorTypeResolver = new PhpDocumentorTypeResolver();
}

/** @return GraphQLType&OutputType */
public function mapReturnType(ReflectionMethod $refMethod, DocBlock $docBlockObj): GraphQLType
public function mapReturnType(ReflectionMethod $refMethod, DocBlock $docBlockObj): GraphQLType&OutputType
{
$returnType = $refMethod->getReturnType();
if ($returnType !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/PorpaginasTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function canMapNameToType(string $typeName): bool
*
* @throws CannotMapTypeExceptionInterface
*/
public function mapNameToType(string $typeName): Type
public function mapNameToType(string $typeName): Type&NamedType
{
if (! $this->canMapNameToType($typeName)) {
throw CannotMapTypeException::createForName($typeName);
Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/Proxys/MutableInterfaceTypeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @internal
*/
class MutableInterfaceTypeAdapter extends MutableInterfaceType implements MutableInterface
final class MutableInterfaceTypeAdapter extends MutableInterfaceType
{
/** @use MutableAdapterTrait */
use MutableAdapterTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/Proxys/MutableObjectTypeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @internal
*/
class MutableObjectTypeAdapter extends MutableObjectType implements MutableInterface
final class MutableObjectTypeAdapter extends MutableObjectType
{
/** @use MutableAdapterTrait */
use MutableAdapterTrait;
Expand Down
Loading