diff --git a/composer.json b/composer.json index 57826fba1e..e501cf35dd 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/AggregateControllerQueryProvider.php b/src/AggregateControllerQueryProvider.php index 61135bacb7..a85e97bc1f 100644 --- a/src/AggregateControllerQueryProvider.php +++ b/src/AggregateControllerQueryProvider.php @@ -29,7 +29,7 @@ class AggregateControllerQueryProvider implements QueryProviderInterface * @param iterable $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) { } diff --git a/src/AggregateControllerQueryProviderFactory.php b/src/AggregateControllerQueryProviderFactory.php index 56e549a69b..1eb77795e8 100644 --- a/src/AggregateControllerQueryProviderFactory.php +++ b/src/AggregateControllerQueryProviderFactory.php @@ -15,7 +15,7 @@ class AggregateControllerQueryProviderFactory implements QueryProviderFactoryInt * @param iterable $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) { } diff --git a/src/AnnotationReader.php b/src/AnnotationReader.php index 2e9164ab1d..a65d15ac10 100644 --- a/src/AnnotationReader.php +++ b/src/AnnotationReader.php @@ -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 $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'); @@ -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 + }; } } @@ -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 + }; } } diff --git a/src/FactoryContext.php b/src/FactoryContext.php index 4985a2040a..a59dafd668 100644 --- a/src/FactoryContext.php +++ b/src/FactoryContext.php @@ -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, ) { } diff --git a/src/FieldsBuilder.php b/src/FieldsBuilder.php index b1781ff325..b126d410b1 100644 --- a/src/FieldsBuilder.php +++ b/src/FieldsBuilder.php @@ -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; @@ -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); @@ -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(); @@ -673,12 +673,8 @@ private function getMagicGetMethodFromSourceClassOrProxy(ReflectionClass $proxyR return $sourceRefClass->getMethod($magicGet); } - /** - * @param ReflectionClass $refClass - * - * @return OutputType&Type - */ - private function resolveOutputType(string $outputType, ReflectionClass $refClass, SourceFieldInterface $sourceField): OutputType + /** @param ReflectionClass $refClass */ + private function resolveOutputType(string $outputType, ReflectionClass $refClass, SourceFieldInterface $sourceField): OutputType&Type { try { return $this->typeResolver->mapNameToOutputType($outputType); @@ -688,12 +684,8 @@ private function resolveOutputType(string $outputType, ReflectionClass $refClass } } - /** - * @param ReflectionClass $refClass - * - * @return OutputType&Type - */ - private function resolvePhpType(string $phpTypeStr, ReflectionClass $refClass, ReflectionMethod $refMethod): OutputType + /** @param ReflectionClass $refClass */ + private function resolvePhpType(string $phpTypeStr, ReflectionClass $refClass, ReflectionMethod $refMethod): OutputType&Type { $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); @@ -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); diff --git a/src/Http/WebonyxGraphqlMiddleware.php b/src/Http/WebonyxGraphqlMiddleware.php index acc02a2fa8..82e0485e7b 100644 --- a/src/Http/WebonyxGraphqlMiddleware.php +++ b/src/Http/WebonyxGraphqlMiddleware.php @@ -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, ) { diff --git a/src/InputField.php b/src/InputField.php index 6507fa8b20..367ff96d55 100644 --- a/src/InputField.php +++ b/src/InputField.php @@ -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; diff --git a/src/InputFieldDescriptor.php b/src/InputFieldDescriptor.php index aa909ce49c..d7e79f5620 100644 --- a/src/InputFieldDescriptor.php +++ b/src/InputFieldDescriptor.php @@ -27,7 +27,7 @@ class InputFieldDescriptor { private string $name; /** @var (InputType&Type)|(InputType&Type&NullableType) */ - private InputType $type; + private InputType&Type $type; /** @var array */ private array $parameters = []; /** @var callable|null */ @@ -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; } diff --git a/src/InputTypeUtils.php b/src/InputTypeUtils.php index c96899e0f1..3c38de38aa 100644 --- a/src/InputTypeUtils.php +++ b/src/InputTypeUtils.php @@ -33,8 +33,8 @@ class InputTypeUtils { public function __construct( - private AnnotationReader $annotationReader, - private NamingStrategyInterface $namingStrategy, + private readonly AnnotationReader $annotationReader, + private readonly NamingStrategyInterface $namingStrategy, ) { } diff --git a/src/Mappers/AbstractTypeMapper.php b/src/Mappers/AbstractTypeMapper.php index 60828ee63f..95c20f4f29 100644 --- a/src/Mappers/AbstractTypeMapper.php +++ b/src/Mappers/AbstractTypeMapper.php @@ -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); @@ -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(); @@ -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); diff --git a/src/Mappers/CompositeTypeMapper.php b/src/Mappers/CompositeTypeMapper.php index 44302d6ca2..74a290d6e8 100644 --- a/src/Mappers/CompositeTypeMapper.php +++ b/src/Mappers/CompositeTypeMapper.php @@ -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)) { diff --git a/src/Mappers/GlobExtendAnnotationsCache.php b/src/Mappers/GlobExtendAnnotationsCache.php index d99bf1e47b..4ed59fe6de 100644 --- a/src/Mappers/GlobExtendAnnotationsCache.php +++ b/src/Mappers/GlobExtendAnnotationsCache.php @@ -9,7 +9,7 @@ * * @internal */ -class GlobExtendAnnotationsCache +final class GlobExtendAnnotationsCache { private string|null $extendTypeClassName = null; diff --git a/src/Mappers/Parameters/ContainerParameterHandler.php b/src/Mappers/Parameters/ContainerParameterHandler.php index 7988189d01..e75a0d9b9a 100644 --- a/src/Mappers/Parameters/ContainerParameterHandler.php +++ b/src/Mappers/Parameters/ContainerParameterHandler.php @@ -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 diff --git a/src/Mappers/Parameters/InjectUserParameterHandler.php b/src/Mappers/Parameters/InjectUserParameterHandler.php index d794544daa..b7fe0ddd76 100644 --- a/src/Mappers/Parameters/InjectUserParameterHandler.php +++ b/src/Mappers/Parameters/InjectUserParameterHandler.php @@ -18,7 +18,7 @@ */ class InjectUserParameterHandler implements ParameterMiddlewareInterface { - public function __construct(private AuthenticationServiceInterface $authenticationService) + public function __construct(private readonly AuthenticationServiceInterface $authenticationService) { } diff --git a/src/Mappers/Parameters/TypeHandler.php b/src/Mappers/Parameters/TypeHandler.php index 810e971b39..47ceea4681 100644 --- a/src/Mappers/Parameters/TypeHandler.php +++ b/src/Mappers/Parameters/TypeHandler.php @@ -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) { diff --git a/src/Mappers/PorpaginasTypeMapper.php b/src/Mappers/PorpaginasTypeMapper.php index 7fee55162a..6fd520a862 100644 --- a/src/Mappers/PorpaginasTypeMapper.php +++ b/src/Mappers/PorpaginasTypeMapper.php @@ -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); diff --git a/src/Mappers/Proxys/MutableInterfaceTypeAdapter.php b/src/Mappers/Proxys/MutableInterfaceTypeAdapter.php index 4d5ccc7985..2f179ab4e1 100644 --- a/src/Mappers/Proxys/MutableInterfaceTypeAdapter.php +++ b/src/Mappers/Proxys/MutableInterfaceTypeAdapter.php @@ -25,7 +25,7 @@ * * @internal */ -class MutableInterfaceTypeAdapter extends MutableInterfaceType implements MutableInterface +final class MutableInterfaceTypeAdapter extends MutableInterfaceType { /** @use MutableAdapterTrait */ use MutableAdapterTrait; diff --git a/src/Mappers/Proxys/MutableObjectTypeAdapter.php b/src/Mappers/Proxys/MutableObjectTypeAdapter.php index 4e15a064f1..cae384e1e0 100644 --- a/src/Mappers/Proxys/MutableObjectTypeAdapter.php +++ b/src/Mappers/Proxys/MutableObjectTypeAdapter.php @@ -26,7 +26,7 @@ * * @internal */ -class MutableObjectTypeAdapter extends MutableObjectType implements MutableInterface +final class MutableObjectTypeAdapter extends MutableObjectType { /** @use MutableAdapterTrait */ use MutableAdapterTrait; diff --git a/src/Mappers/RecursiveTypeMapper.php b/src/Mappers/RecursiveTypeMapper.php index 9df69bafa5..0b3415740f 100644 --- a/src/Mappers/RecursiveTypeMapper.php +++ b/src/Mappers/RecursiveTypeMapper.php @@ -341,11 +341,9 @@ public function mapClassToType(string $className, OutputType|null $subType): Mut * @param class-string $className The exact class name to look for (this function does not look into parent classes). * @param (OutputType&Type&NamedType)|null $subType A subtype (if the main className is an iterator) * - * @return OutputType&Type&NamedType - * * @throws CannotMapTypeExceptionInterface */ - public function mapClassToInterfaceOrType(string $className, OutputType|null $subType): OutputType + public function mapClassToInterfaceOrType(string $className, OutputType|null $subType): OutputType&Type&NamedType { $closestClassName = $this->findClosestMatchingParent($className); if ($closestClassName === null) { @@ -478,7 +476,7 @@ public function canMapNameToType(string $typeName): bool * * @return NamedType&Type&(InputType|OutputType) */ - public function mapNameToType(string $typeName): Type + public function mapNameToType(string $typeName): Type&NamedType { if ($this->typeRegistry->hasType($typeName)) { return $this->typeRegistry->getType($typeName); diff --git a/src/Mappers/RecursiveTypeMapperInterface.php b/src/Mappers/RecursiveTypeMapperInterface.php index 549405db9e..b90b6b5ec1 100644 --- a/src/Mappers/RecursiveTypeMapperInterface.php +++ b/src/Mappers/RecursiveTypeMapperInterface.php @@ -47,11 +47,9 @@ public function mapClassToType(string $className, OutputType|null $subType): Mut * @param string $className The exact class name to look for (this function does not look into parent classes). * @param (OutputType&Type)|null $subType A subtype (if the main className is an iterator) * - * @return OutputType&Type - * * @throws CannotMapTypeExceptionInterface */ - public function mapClassToInterfaceOrType(string $className, OutputType|null $subType): OutputType; + public function mapClassToInterfaceOrType(string $className, OutputType|null $subType): OutputType&Type; /** * Finds the list of interfaces returned by $className. diff --git a/src/Mappers/Root/BaseTypeMapper.php b/src/Mappers/Root/BaseTypeMapper.php index 22ec7a8ff6..d30bd239a0 100644 --- a/src/Mappers/Root/BaseTypeMapper.php +++ b/src/Mappers/Root/BaseTypeMapper.php @@ -45,12 +45,12 @@ */ class BaseTypeMapper implements RootTypeMapperInterface { - public function __construct(private RootTypeMapperInterface $next, private RecursiveTypeMapperInterface $recursiveTypeMapper, private RootTypeMapperInterface $topRootTypeMapper) + public function __construct(private readonly RootTypeMapperInterface $next, private readonly RecursiveTypeMapperInterface $recursiveTypeMapper, private readonly RootTypeMapperInterface $topRootTypeMapper) { } /** @throws CannotMapTypeExceptionInterface */ - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { $mappedType = $this->mapBaseType($type); if ($mappedType !== null) { @@ -78,7 +78,7 @@ public function toGraphQLOutputType(Type $type, OutputType|null $subType, Reflec * @throws CannotMapTypeException * @throws CannotMapTypeExceptionInterface */ - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType { $mappedType = $this->mapBaseType($type); if ($mappedType !== null) { @@ -127,23 +127,23 @@ private function mapBaseType(Type $type): BooleanType|FloatType|IDType|IntType|S if ($type instanceof Object_) { $fqcn = (string) $type->getFqsen(); - switch ($fqcn) { - case '\\' . DateTimeImmutable::class: - case '\\' . DateTimeInterface::class: - return self::getDateTimeType(); + return match ($fqcn) { + '\\' . DateTimeImmutable::class, + '\\' . DateTimeInterface::class=> + self::getDateTimeType(), - case '\\' . UploadedFileInterface::class: - return self::getUploadType(); + '\\' . UploadedFileInterface::class=> + self::getUploadType(), - case '\\' . DateTime::class: - throw CannotMapTypeException::createForDateTime(); + '\\' . DateTime::class=> + throw CannotMapTypeException::createForDateTime(), - case '\\' . ID::class: - return GraphQLType::id(); + '\\' . ID::class + => GraphQLType::id(), - default: - return null; - } + default=> + null + }; } return null; @@ -178,17 +178,13 @@ private static function getDateTimeType(): DateTimeType * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { // No need to map base types, only types added by us. - if ($typeName === 'Upload') { - return self::getUploadType(); - } - - if ($typeName === 'DateTime') { - return self::getDateTimeType(); - } - - return $this->next->mapNameToType($typeName); + return match ($typeName) { + 'Upload'=>self::getUploadType(), + 'DateTime'=>self::getDateTimeType(), + default=>$this->next->mapNameToType($typeName) + }; } } diff --git a/src/Mappers/Root/CompoundTypeMapper.php b/src/Mappers/Root/CompoundTypeMapper.php index 70323ae80a..c3e7389874 100644 --- a/src/Mappers/Root/CompoundTypeMapper.php +++ b/src/Mappers/Root/CompoundTypeMapper.php @@ -40,15 +40,15 @@ class CompoundTypeMapper implements RootTypeMapperInterface { public function __construct( - private RootTypeMapperInterface $next, - private RootTypeMapperInterface $topRootTypeMapper, - private NamingStrategyInterface $namingStrategy, - private TypeRegistry $typeRegistry, - private RecursiveTypeMapperInterface $recursiveTypeMapper, + private readonly RootTypeMapperInterface $next, + private readonly RootTypeMapperInterface $topRootTypeMapper, + private readonly NamingStrategyInterface $namingStrategy, + private readonly TypeRegistry $typeRegistry, + private readonly RecursiveTypeMapperInterface $recursiveTypeMapper, ) { } - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { if (! $type instanceof Compound) { return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); @@ -87,7 +87,7 @@ public function toGraphQLOutputType(Type $type, OutputType|null $subType, Reflec return $return; } - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType { if (! $type instanceof Compound) { return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); @@ -165,7 +165,7 @@ private function getTypeFromUnion(array $unionTypes): GraphQLType * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { return $this->next->mapNameToType($typeName); } diff --git a/src/Mappers/Root/EnumTypeMapper.php b/src/Mappers/Root/EnumTypeMapper.php index fbe6e6802f..13b7519240 100644 --- a/src/Mappers/Root/EnumTypeMapper.php +++ b/src/Mappers/Root/EnumTypeMapper.php @@ -38,24 +38,20 @@ class EnumTypeMapper implements RootTypeMapperInterface /** @param NS[] $namespaces List of namespaces containing enums. Used when searching an enum by name. */ public function __construct( - private RootTypeMapperInterface $next, - private AnnotationReader $annotationReader, - private CacheInterface $cacheService, - private array $namespaces, + private readonly RootTypeMapperInterface $next, + private readonly AnnotationReader $annotationReader, + private readonly CacheInterface $cacheService, + private readonly array $namespaces, ) { } - /** - * @param (OutputType&GraphQLType)|null $subType - * - * @return OutputType&GraphQLType - */ + /** @param (OutputType&GraphQLType)|null $subType */ public function toGraphQLOutputType( Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj, - ): OutputType { + ): OutputType&GraphQLType { $result = $this->map($type); return $result ?? $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); } @@ -69,7 +65,7 @@ public function toGraphQLInputType( string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj, - ): InputType + ): InputType&GraphQLType { $result = $this->map($type); if ($result === null) { @@ -140,7 +136,7 @@ private function getTypeName(ReflectionClass $reflectionClass): string * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { // This is a hack to make sure "$schema->assertValid()" returns true. // The mapNameToType will fail if the mapByClassName method was not called before. diff --git a/src/Mappers/Root/FinalRootTypeMapper.php b/src/Mappers/Root/FinalRootTypeMapper.php index e7881c5843..e3f7a2ba11 100644 --- a/src/Mappers/Root/FinalRootTypeMapper.php +++ b/src/Mappers/Root/FinalRootTypeMapper.php @@ -23,24 +23,22 @@ */ final class FinalRootTypeMapper implements RootTypeMapperInterface { - public function __construct(private RecursiveTypeMapperInterface $recursiveTypeMapper) + public function __construct(private readonly RecursiveTypeMapperInterface $recursiveTypeMapper) { } /** * @param (OutputType&GraphQLType)|null $subType * - * @return OutputType&GraphQLType - * * @throws CannotMapTypeException */ - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { throw CannotMapTypeException::createForPhpDocType($type); } /** @throws CannotMapTypeException */ - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType { throw CannotMapTypeException::createForPhpDocType($type); } @@ -52,7 +50,7 @@ public function toGraphQLInputType(Type $type, InputType|null $subType, string $ * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { return $this->recursiveTypeMapper->mapNameToType($typeName); } diff --git a/src/Mappers/Root/IteratorTypeMapper.php b/src/Mappers/Root/IteratorTypeMapper.php index f0eaca1ab7..8d9f9fb1da 100644 --- a/src/Mappers/Root/IteratorTypeMapper.php +++ b/src/Mappers/Root/IteratorTypeMapper.php @@ -34,11 +34,11 @@ */ class IteratorTypeMapper implements RootTypeMapperInterface { - public function __construct(private RootTypeMapperInterface $next, private RootTypeMapperInterface $topRootTypeMapper) + public function __construct(private readonly RootTypeMapperInterface $next, private readonly RootTypeMapperInterface $topRootTypeMapper) { } - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { if (! $type instanceof Compound) { try { @@ -70,7 +70,7 @@ public function toGraphQLOutputType(Type $type, OutputType|null $subType, Reflec return $result; } - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType { if (! $type instanceof Compound) { //try { @@ -102,7 +102,7 @@ public function toGraphQLInputType(Type $type, InputType|null $subType, string $ * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { // TODO: how to handle this? Do we need? return $this->next->mapNameToType($typeName); @@ -125,7 +125,7 @@ private function getTypeInArray(Type $typeHint): Type|null * * @return (OutputType&GraphQLType)|(InputType&GraphQLType)|null */ - private function toGraphQLType(Compound $type, Closure $topToGraphQLType, bool $isOutputType) + private function toGraphQLType(Compound $type, Closure $topToGraphQLType, bool $isOutputType): GraphQLType|null { $types = iterator_to_array($type); diff --git a/src/Mappers/Root/MyCLabsEnumTypeMapper.php b/src/Mappers/Root/MyCLabsEnumTypeMapper.php index 1797e27720..7ad0d1f9d8 100644 --- a/src/Mappers/Root/MyCLabsEnumTypeMapper.php +++ b/src/Mappers/Root/MyCLabsEnumTypeMapper.php @@ -35,17 +35,17 @@ class MyCLabsEnumTypeMapper implements RootTypeMapperInterface private array $cacheByName = []; /** @param NS[] $namespaces List of namespaces containing enums. Used when searching an enum by name. */ - public function __construct(private RootTypeMapperInterface $next, private AnnotationReader $annotationReader, private CacheInterface $cacheService, private array $namespaces) + public function __construct(private readonly RootTypeMapperInterface $next, private readonly AnnotationReader $annotationReader, private readonly CacheInterface $cacheService, private readonly array $namespaces) { } - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&\GraphQL\Type\Definition\Type { $result = $this->map($type); return $result ?? $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); } - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&\GraphQL\Type\Definition\Type { $result = $this->map($type); return $result ?? $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); @@ -102,7 +102,7 @@ private function getTypeName(ReflectionClass $refClass): string * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&\GraphQL\Type\Definition\Type { // This is a hack to make sure "$schema->assertValid()" returns true. // The mapNameToType will fail if the mapByClassName method was not called before. diff --git a/src/Mappers/Root/NullableTypeMapperAdapter.php b/src/Mappers/Root/NullableTypeMapperAdapter.php index 09260e0d16..42d478bd79 100644 --- a/src/Mappers/Root/NullableTypeMapperAdapter.php +++ b/src/Mappers/Root/NullableTypeMapperAdapter.php @@ -39,7 +39,7 @@ public function setNext(RootTypeMapperInterface $next): void $this->next = $next; } - public function toGraphQLOutputType(Type $type, OutputType|GraphQLType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, OutputType|GraphQLType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { // Let's check a "null" value in the docblock $isNullable = $this->isNullable($type); @@ -65,7 +65,7 @@ public function toGraphQLOutputType(Type $type, OutputType|GraphQLType|null $sub return $graphQlType; } - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType { // Let's check a "null" value in the docblock $isNullable = $this->isNullable($type); @@ -99,7 +99,7 @@ public function toGraphQLInputType(Type $type, InputType|null $subType, string $ * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { return $this->next->mapNameToType($typeName); } diff --git a/src/Mappers/Root/RootTypeMapperFactoryContext.php b/src/Mappers/Root/RootTypeMapperFactoryContext.php index 5a6f27d28f..254a5ac081 100644 --- a/src/Mappers/Root/RootTypeMapperFactoryContext.php +++ b/src/Mappers/Root/RootTypeMapperFactoryContext.php @@ -19,15 +19,15 @@ final class RootTypeMapperFactoryContext { public function __construct( - private AnnotationReader $annotationReader, - private TypeResolver $typeResolver, - private NamingStrategyInterface $namingStrategy, - private TypeRegistry $typeRegistry, - private RecursiveTypeMapperInterface $recursiveTypeMapper, - private ContainerInterface $container, - private CacheInterface $cache, - 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 RecursiveTypeMapperInterface $recursiveTypeMapper, + private readonly ContainerInterface $container, + private readonly CacheInterface $cache, + private readonly int|null $globTTL, + private readonly int|null $mapTTL = null, ) { } diff --git a/src/Mappers/Root/RootTypeMapperInterface.php b/src/Mappers/Root/RootTypeMapperInterface.php index 03fc11aa11..237768c969 100644 --- a/src/Mappers/Root/RootTypeMapperInterface.php +++ b/src/Mappers/Root/RootTypeMapperInterface.php @@ -25,19 +25,11 @@ */ interface RootTypeMapperInterface { - /** - * @param (OutputType&GraphQLType)|null $subType - * - * @return OutputType&GraphQLType - */ - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType; + /** @param (OutputType&GraphQLType)|null $subType */ + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType; - /** - * @param (InputType&GraphQLType)|null $subType - * - * @return InputType&GraphQLType - */ - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType; + /** @param (InputType&GraphQLType)|null $subType */ + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType; /** * Returns a GraphQL type by name. @@ -46,5 +38,5 @@ public function toGraphQLInputType(Type $type, InputType|null $subType, string $ * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType; + public function mapNameToType(string $typeName): NamedType&GraphQLType; } diff --git a/src/Mappers/StaticTypeMapper.php b/src/Mappers/StaticTypeMapper.php index 916fc66ead..d41d7cdb37 100644 --- a/src/Mappers/StaticTypeMapper.php +++ b/src/Mappers/StaticTypeMapper.php @@ -161,7 +161,7 @@ public function mapClassToInputType(string $className): ResolvableMutableInputIn * * @throws CannotMapTypeExceptionInterface */ - public function mapNameToType(string $typeName): Type + public function mapNameToType(string $typeName): Type&NamedType { if (isset($this->notMappedTypes[$typeName])) { return $this->notMappedTypes[$typeName]; diff --git a/src/Mappers/TypeMapperInterface.php b/src/Mappers/TypeMapperInterface.php index c402e230de..c3c32e3a4d 100644 --- a/src/Mappers/TypeMapperInterface.php +++ b/src/Mappers/TypeMapperInterface.php @@ -53,7 +53,7 @@ public function canMapNameToType(string $typeName): bool; * * @return NamedType&Type&((ResolvableMutableInputInterface&InputObjectType)|MutableObjectType|MutableInterfaceType) */ - public function mapNameToType(string $typeName): Type; + public function mapNameToType(string $typeName): Type&NamedType; /** * Returns the list of classes that have matching GraphQL types. diff --git a/src/Middlewares/AuthorizationInputFieldMiddleware.php b/src/Middlewares/AuthorizationInputFieldMiddleware.php index 5d1676db03..c31e6c276b 100644 --- a/src/Middlewares/AuthorizationInputFieldMiddleware.php +++ b/src/Middlewares/AuthorizationInputFieldMiddleware.php @@ -20,8 +20,8 @@ class AuthorizationInputFieldMiddleware implements InputFieldMiddlewareInterface { public function __construct( - private AuthenticationServiceInterface $authenticationService, - private AuthorizationServiceInterface $authorizationService, + private readonly AuthenticationServiceInterface $authenticationService, + private readonly AuthorizationServiceInterface $authorizationService, ) { } diff --git a/src/Middlewares/InputNext.php b/src/Middlewares/InputNext.php index 80f8b4f82e..f7f1493090 100644 --- a/src/Middlewares/InputNext.php +++ b/src/Middlewares/InputNext.php @@ -23,7 +23,7 @@ final class InputNext implements InputFieldHandlerInterface * @param InputFieldHandlerInterface $fallbackHandler Fallback handler to * invoke when the queue is exhausted. */ - public function __construct(SplQueue $queue, private InputFieldHandlerInterface $fallbackHandler) + public function __construct(SplQueue $queue, private readonly InputFieldHandlerInterface $fallbackHandler) { $this->queue = clone $queue; } diff --git a/src/Middlewares/MagicPropertyResolver.php b/src/Middlewares/MagicPropertyResolver.php index 9c1e3826cc..502ba1afcf 100644 --- a/src/Middlewares/MagicPropertyResolver.php +++ b/src/Middlewares/MagicPropertyResolver.php @@ -16,7 +16,7 @@ * * @internal */ -class MagicPropertyResolver implements SourceResolverInterface +final class MagicPropertyResolver implements SourceResolverInterface { private object|null $object = null; diff --git a/src/Middlewares/Next.php b/src/Middlewares/Next.php index fb9a1ddc7c..ef0925454a 100644 --- a/src/Middlewares/Next.php +++ b/src/Middlewares/Next.php @@ -21,7 +21,7 @@ final class Next implements FieldHandlerInterface * @param FieldHandlerInterface $fallbackHandler Fallback handler to * invoke when the queue is exhausted. */ - public function __construct(SplQueue $queue, private FieldHandlerInterface $fallbackHandler) + public function __construct(SplQueue $queue, private readonly FieldHandlerInterface $fallbackHandler) { $this->queue = clone $queue; } diff --git a/src/Middlewares/SecurityFieldMiddleware.php b/src/Middlewares/SecurityFieldMiddleware.php index 6dbb07c8ae..a6a9bde9d9 100644 --- a/src/Middlewares/SecurityFieldMiddleware.php +++ b/src/Middlewares/SecurityFieldMiddleware.php @@ -26,7 +26,7 @@ */ class SecurityFieldMiddleware implements FieldMiddlewareInterface { - public function __construct(private ExpressionLanguage $language, private AuthenticationServiceInterface $authenticationService, private AuthorizationServiceInterface $authorizationService/*, ?LoggerInterface $logger = null*/) + public function __construct(private readonly ExpressionLanguage $language, private readonly AuthenticationServiceInterface $authenticationService, private readonly AuthorizationServiceInterface $authorizationService/*, ?LoggerInterface $logger = null*/) { /*$this->logger = $logger;*/ } diff --git a/src/Middlewares/SecurityInputFieldMiddleware.php b/src/Middlewares/SecurityInputFieldMiddleware.php index 89a93bb5cb..bc21fa57bf 100644 --- a/src/Middlewares/SecurityInputFieldMiddleware.php +++ b/src/Middlewares/SecurityInputFieldMiddleware.php @@ -25,9 +25,9 @@ class SecurityInputFieldMiddleware implements InputFieldMiddlewareInterface { public function __construct( - private ExpressionLanguage $language, - private AuthenticationServiceInterface $authenticationService, - private AuthorizationServiceInterface $authorizationService, + private readonly ExpressionLanguage $language, + private readonly AuthenticationServiceInterface $authenticationService, + private readonly AuthorizationServiceInterface $authorizationService, ) { } diff --git a/src/Middlewares/ServiceResolver.php b/src/Middlewares/ServiceResolver.php index f1135fae8d..1f0c9f8c39 100644 --- a/src/Middlewares/ServiceResolver.php +++ b/src/Middlewares/ServiceResolver.php @@ -12,7 +12,7 @@ * * @internal */ -class ServiceResolver implements ResolverInterface +final class ServiceResolver implements ResolverInterface { /** @var callable&array{0:object, 1:string} */ private $callable; diff --git a/src/Middlewares/SourceInputPropertyResolver.php b/src/Middlewares/SourceInputPropertyResolver.php index ad5607c9c9..66b76ee459 100644 --- a/src/Middlewares/SourceInputPropertyResolver.php +++ b/src/Middlewares/SourceInputPropertyResolver.php @@ -15,11 +15,11 @@ * * @internal */ -class SourceInputPropertyResolver implements SourceResolverInterface +final class SourceInputPropertyResolver implements SourceResolverInterface { private object|null $object = null; - public function __construct(private string $propertyName) + public function __construct(private readonly string $propertyName) { } diff --git a/src/Middlewares/SourcePropertyResolver.php b/src/Middlewares/SourcePropertyResolver.php index 5444070400..29582ab0c1 100644 --- a/src/Middlewares/SourcePropertyResolver.php +++ b/src/Middlewares/SourcePropertyResolver.php @@ -15,11 +15,11 @@ * * @internal */ -class SourcePropertyResolver implements SourceResolverInterface +final class SourcePropertyResolver implements SourceResolverInterface { private object|null $object = null; - public function __construct(private string $propertyName) + public function __construct(private readonly string $propertyName) { } diff --git a/src/Middlewares/SourceResolver.php b/src/Middlewares/SourceResolver.php index 51618d721a..8f4b6e5cc4 100644 --- a/src/Middlewares/SourceResolver.php +++ b/src/Middlewares/SourceResolver.php @@ -15,11 +15,11 @@ * * @internal */ -class SourceResolver implements SourceResolverInterface +final class SourceResolver implements SourceResolverInterface { private object|null $object = null; - public function __construct(private string $methodName) + public function __construct(private readonly string $methodName) { } diff --git a/src/Parameters/ContainerParameter.php b/src/Parameters/ContainerParameter.php index 4937d23619..1e926618a2 100644 --- a/src/Parameters/ContainerParameter.php +++ b/src/Parameters/ContainerParameter.php @@ -12,7 +12,7 @@ */ class ContainerParameter implements ParameterInterface { - public function __construct(private ContainerInterface $container, private string $identifier) + public function __construct(private readonly ContainerInterface $container, private readonly string $identifier) { } diff --git a/src/Parameters/DefaultValueParameter.php b/src/Parameters/DefaultValueParameter.php index 39866b1f34..78e86b86dd 100644 --- a/src/Parameters/DefaultValueParameter.php +++ b/src/Parameters/DefaultValueParameter.php @@ -11,7 +11,7 @@ */ class DefaultValueParameter implements ParameterInterface { - public function __construct(private mixed $defaultValue) + public function __construct(private readonly mixed $defaultValue) { } diff --git a/src/Parameters/InjectUserParameter.php b/src/Parameters/InjectUserParameter.php index d6fad0a51a..32755a32ce 100644 --- a/src/Parameters/InjectUserParameter.php +++ b/src/Parameters/InjectUserParameter.php @@ -12,7 +12,7 @@ */ class InjectUserParameter implements ParameterInterface { - public function __construct(private AuthenticationServiceInterface $authenticationService) + public function __construct(private readonly AuthenticationServiceInterface $authenticationService) { } diff --git a/src/Parameters/InputTypeParameter.php b/src/Parameters/InputTypeParameter.php index 60cc458316..67a62ee009 100644 --- a/src/Parameters/InputTypeParameter.php +++ b/src/Parameters/InputTypeParameter.php @@ -15,8 +15,7 @@ class InputTypeParameter implements InputTypeParameterInterface private bool $doesHaveDefaultValue; private string|null $description = null; - /** @param InputType&Type $type */ - public function __construct(private string $name, private InputType $type, bool $hasDefaultValue, private mixed $defaultValue, private ArgumentResolver $argumentResolver) + public function __construct(private readonly string $name, private readonly InputType&Type $type, bool $hasDefaultValue, private readonly mixed $defaultValue, private readonly ArgumentResolver $argumentResolver) { $this->doesHaveDefaultValue = $hasDefaultValue; } @@ -46,8 +45,7 @@ public function getName(): string return $this->name; } - /** @return InputType&Type */ - public function getType(): InputType + public function getType(): InputType&Type { return $this->type; } diff --git a/src/Parameters/InputTypeParameterInterface.php b/src/Parameters/InputTypeParameterInterface.php index 55a0906731..221640e514 100644 --- a/src/Parameters/InputTypeParameterInterface.php +++ b/src/Parameters/InputTypeParameterInterface.php @@ -14,8 +14,7 @@ interface InputTypeParameterInterface extends ParameterInterface /** @param array $args */ public function resolve(object|null $source, array $args, mixed $context, ResolveInfo $info): mixed; - /** @return InputType&Type */ - public function getType(): InputType; + public function getType(): InputType&Type; public function hasDefaultValue(): bool; diff --git a/src/Parameters/InputTypeProperty.php b/src/Parameters/InputTypeProperty.php index 88170be42c..ee62a0245d 100644 --- a/src/Parameters/InputTypeProperty.php +++ b/src/Parameters/InputTypeProperty.php @@ -10,8 +10,7 @@ class InputTypeProperty extends InputTypeParameter { - /** @param InputType&Type $type */ - public function __construct(private string $propertyName, string $fieldName, InputType $type, bool $hasDefaultValue, mixed $defaultValue, ArgumentResolver $argumentResolver) + public function __construct(private readonly string $propertyName, string $fieldName, InputType&Type $type, bool $hasDefaultValue, mixed $defaultValue, ArgumentResolver $argumentResolver) { parent::__construct($fieldName, $type, $hasDefaultValue, $defaultValue, $argumentResolver); } diff --git a/src/QueryField.php b/src/QueryField.php index f6ae0b92ad..df02c837db 100644 --- a/src/QueryField.php +++ b/src/QueryField.php @@ -37,7 +37,7 @@ * @phpstan-import-type ArgumentListConfig from FieldDefinition * @phpstan-import-type ComplexityFn from FieldDefinition */ -class QueryField extends FieldDefinition +final class QueryField extends FieldDefinition { /** * @param OutputType&Type $type diff --git a/src/QueryFieldDescriptor.php b/src/QueryFieldDescriptor.php index 12c3514f7a..52252dab25 100644 --- a/src/QueryFieldDescriptor.php +++ b/src/QueryFieldDescriptor.php @@ -67,8 +67,7 @@ public function getType(): Type|null return $this->type; } - /** @param OutputType&Type $type */ - public function setType(Type $type): void + public function setType(OutputType&Type $type): void { $this->type = $type; } diff --git a/src/Reflection/CachedDocBlockFactory.php b/src/Reflection/CachedDocBlockFactory.php index 31be932ce0..70b5c6699c 100644 --- a/src/Reflection/CachedDocBlockFactory.php +++ b/src/Reflection/CachedDocBlockFactory.php @@ -32,7 +32,7 @@ class CachedDocBlockFactory private ContextFactory $contextFactory; /** @param CacheInterface $cache The cache we fetch data from. Note this is a SAFE cache. It does not need to be purged. */ - public function __construct(private CacheInterface $cache, DocBlockFactory|null $docBlockFactory = null) + public function __construct(private readonly CacheInterface $cache, DocBlockFactory|null $docBlockFactory = null) { $this->docBlockFactory = $docBlockFactory ?: DocBlockFactory::createInstance(); $this->contextFactory = new ContextFactory(); diff --git a/src/ResolveUtils.php b/src/ResolveUtils.php index 0d6d2bbeb8..b3aa657668 100644 --- a/src/ResolveUtils.php +++ b/src/ResolveUtils.php @@ -19,7 +19,7 @@ * * @internal */ -class ResolveUtils +final class ResolveUtils { public static function assertInnerReturnType(mixed $result, Type $type): void { diff --git a/src/Types/ArgumentResolver.php b/src/Types/ArgumentResolver.php index 0025e5076a..7f5f5fb53a 100644 --- a/src/Types/ArgumentResolver.php +++ b/src/Types/ArgumentResolver.php @@ -26,13 +26,11 @@ class ArgumentResolver { /** - * Casts a value received from GraphQL into an argument passed to a method. - * - * @param InputType&Type $type + * Casts a value received from GraphQL into an argument passed to a method.* * * @throws Error */ - public function resolve(object|null $source, mixed $val, mixed $context, ResolveInfo $resolveInfo, InputType $type): mixed + public function resolve(object|null $source, mixed $val, mixed $context, ResolveInfo $resolveInfo, InputType&Type $type): mixed { $type = $this->stripNonNullType($type); if ($type instanceof ListOfType) { @@ -68,10 +66,12 @@ public function resolve(object|null $source, mixed $val, mixed $context, Resolve throw new RuntimeException('Unexpected type: ' . $type::class); } - private function stripNonNullType(Type $type): Type + private function stripNonNullType(InputType&Type $type): InputType&Type { if ($type instanceof NonNull) { - return $this->stripNonNullType($type->getWrappedType()); + $wrapped = $type->getWrappedType(); + assert($wrapped instanceof InputType); + return $this->stripNonNullType($wrapped); } return $type; diff --git a/src/Types/EnumType.php b/src/Types/EnumType.php index 4962f3162b..f04c02b1ae 100644 --- a/src/Types/EnumType.php +++ b/src/Types/EnumType.php @@ -18,7 +18,7 @@ class EnumType extends BaseEnumType { /** @param class-string $enumName */ - public function __construct(string $enumName, string $typeName, private bool $useValues = false) + public function __construct(string $enumName, string $typeName, private readonly bool $useValues = false) { $values = []; foreach ($enumName::cases() as $case) { diff --git a/src/Types/ID.php b/src/Types/ID.php index 363585a911..950214a4ed 100644 --- a/src/Types/ID.php +++ b/src/Types/ID.php @@ -19,7 +19,7 @@ class ID /** * Note: if $value is an object, it has a __toString method on it. */ - public function __construct(private bool|float|int|string|object $value) + public function __construct(private readonly bool|float|int|string|object $value) { if (! is_scalar($value) && (! is_object($value) || ! method_exists($value, '__toString'))) { throw new InvalidArgumentException('ID constructor cannot be passed a non scalar value.'); diff --git a/src/Types/TypeResolver.php b/src/Types/TypeResolver.php index c5fc455049..5edb635a01 100644 --- a/src/Types/TypeResolver.php +++ b/src/Types/TypeResolver.php @@ -52,8 +52,7 @@ public function mapNameToType(string $typeName): Type return $type; } - /** @return OutputType&Type */ - public function mapNameToOutputType(string $typeName): OutputType + public function mapNameToOutputType(string $typeName): OutputType&Type { $type = $this->mapNameToType($typeName); if (! $type instanceof OutputType || ($type instanceof WrappingType && ! $type->getWrappedType() instanceof OutputType)) { @@ -63,8 +62,7 @@ public function mapNameToOutputType(string $typeName): OutputType return $type; } - /** @return InputType&Type */ - public function mapNameToInputType(string $typeName): InputType + public function mapNameToInputType(string $typeName): InputType&Type { $type = $this->mapNameToType($typeName); if (! $type instanceof InputType || ($type instanceof WrappingType && ! $type->getWrappedType() instanceof InputType)) { diff --git a/src/Utils/NamespacedCache.php b/src/Utils/NamespacedCache.php index 00fc886e03..209e6efb23 100644 --- a/src/Utils/NamespacedCache.php +++ b/src/Utils/NamespacedCache.php @@ -20,7 +20,7 @@ class NamespacedCache implements CacheInterface private string $namespace; - public function __construct(private CacheInterface $cache) + public function __construct(private readonly CacheInterface $cache) { $this->namespace = substr(md5(Versions::getVersion('thecodingmachine/graphqlite')), 0, 8); } diff --git a/src/Utils/Namespaces/NS.php b/src/Utils/Namespaces/NS.php index 65c1b59c7f..5c8f3bf9ec 100644 --- a/src/Utils/Namespaces/NS.php +++ b/src/Utils/Namespaces/NS.php @@ -30,7 +30,7 @@ final class NS private array|null $classes = null; /** @param string $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation) */ - public function __construct(private string $namespace, private CacheInterface $cache, private ClassNameMapper $classNameMapper, private int|null $globTTL, private bool $recursive) + public function __construct(private readonly string $namespace, private readonly CacheInterface $cache, private readonly ClassNameMapper $classNameMapper, private readonly int|null $globTTL, private readonly bool $recursive) { } @@ -44,8 +44,8 @@ public function getClassList(): array { if ($this->classes === null) { $this->classes = []; - $explorer = new GlobClassExplorer($this->namespace, $this->cache, $this->globTTL, $this->classNameMapper, $this->recursive); - $classes = $explorer->getClassMap(); + $explorer = new GlobClassExplorer($this->namespace, $this->cache, $this->globTTL, $this->classNameMapper, $this->recursive); + $classes = $explorer->getClassMap(); foreach ($classes as $className => $phpFile) { if (! class_exists($className, false) && ! interface_exists($className, false)) { // Let's try to load the file if it was not imported yet. diff --git a/src/Utils/Namespaces/NamespaceFactory.php b/src/Utils/Namespaces/NamespaceFactory.php index b10b087a52..9d1c6d32cf 100644 --- a/src/Utils/Namespaces/NamespaceFactory.php +++ b/src/Utils/Namespaces/NamespaceFactory.php @@ -16,7 +16,7 @@ final class NamespaceFactory { private ClassNameMapper $classNameMapper; - public function __construct(private CacheInterface $cache, ClassNameMapper|null $classNameMapper = null, private int|null $globTTL = 2) + public function __construct(private readonly CacheInterface $cache, ClassNameMapper|null $classNameMapper = null, private int|null $globTTL = 2) { $this->classNameMapper = $classNameMapper ?? ClassNameMapper::createFromComposerFile(null, null, true); } diff --git a/tests/AbstractQueryProviderTest.php b/tests/AbstractQueryProviderTest.php index 2c1ba84bc8..cd40b3cb21 100644 --- a/tests/AbstractQueryProviderTest.php +++ b/tests/AbstractQueryProviderTest.php @@ -5,6 +5,7 @@ use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; use GraphQL\Type\Definition\InputObjectType; +use GraphQL\Type\Definition\NamedType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\OutputType; use GraphQL\Type\Definition\Type; @@ -180,7 +181,7 @@ public function getSupportedClasses(): array return [TestObject::class, TestObject2::class]; } - public function mapNameToType(string $typeName): Type + public function mapNameToType(string $typeName): Type&NamedType { return match ($typeName) { 'TestObject' => $this->testObjectType, @@ -432,8 +433,7 @@ protected function getTypeRegistry(): TypeRegistry protected function resolveType(string $type): \phpDocumentor\Reflection\Type { - $phpDocumentorTypeResolver = new PhpDocumentorTypeResolver(); - return $phpDocumentorTypeResolver->resolve($type); + return (new PhpDocumentorTypeResolver())->resolve($type); } protected function getNamespaceFactory(): NamespaceFactory diff --git a/tests/Mappers/CompositeTypeMapperTest.php b/tests/Mappers/CompositeTypeMapperTest.php index 553e0fc4e2..f26e46f916 100644 --- a/tests/Mappers/CompositeTypeMapperTest.php +++ b/tests/Mappers/CompositeTypeMapperTest.php @@ -78,19 +78,17 @@ public function getSupportedClasses(): array * @param string $typeName The name of the GraphQL type * @return NamedType&Type&((ResolvableMutableInputInterface&InputObjectType)|MutableObjectType) */ - public function mapNameToType(string $typeName): Type + public function mapNameToType(string $typeName): Type&NamedType { - switch ($typeName) { - case 'TestObject': - return new MutableObjectType([ - 'name' => 'TestObject', - 'fields' => [ - 'test' => Type::string(), - ], - ]); - default: - throw CannotMapTypeException::createForName($typeName); - } + return match ($typeName) { + 'TestObject' => new MutableObjectType([ + 'name' => 'TestObject', + 'fields' => [ + 'test' => Type::string(), + ], + ]), + default => throw CannotMapTypeException::createForName($typeName), + }; } /** diff --git a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php index 447d12f503..66b02bee74 100644 --- a/tests/Mappers/Root/NullableTypeMapperAdapterTest.php +++ b/tests/Mappers/Root/NullableTypeMapperAdapterTest.php @@ -50,17 +50,17 @@ public function testNonNullableReturnedByWrappedMapper(): void $typeMapper->setNext(new class implements RootTypeMapperInterface { - public function toGraphQLOutputType(Type $type, ?OutputType $subType, $reflector, DocBlock $docBlockObj): OutputType + public function toGraphQLOutputType(Type $type, ?OutputType $subType, $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { return new NonNull(new StringType()); } - public function toGraphQLInputType(Type $type, null|InputType $subType, string $argumentName, $reflector, DocBlock $docBlockObj): InputType + public function toGraphQLInputType(Type $type, null|InputType $subType, string $argumentName, $reflector, DocBlock $docBlockObj): InputType&GraphQLType { throw new \RuntimeException('Not implemented'); } - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { throw new \RuntimeException('Not implemented'); } diff --git a/tests/Mappers/Root/VoidRootTypeMapper.php b/tests/Mappers/Root/VoidRootTypeMapper.php index 42b0c17906..23d4874d1b 100644 --- a/tests/Mappers/Root/VoidRootTypeMapper.php +++ b/tests/Mappers/Root/VoidRootTypeMapper.php @@ -15,30 +15,18 @@ class VoidRootTypeMapper implements RootTypeMapperInterface { - /** @var RootTypeMapperInterface */ - private $next; - - public function __construct(RootTypeMapperInterface $next) + public function __construct(private readonly RootTypeMapperInterface $next) { - $this->next = $next; } - /** - * @param (OutputType&GraphQLType)|null $subType - * - * @return OutputType&GraphQLType - */ - public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType + /** @param (OutputType&GraphQLType)|null $subType */ + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType { return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); } - /** - * @param (InputType&GraphQLType)|null $subType - * - * @return InputType&GraphQLType - */ - public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType + /** @param (InputType&GraphQLType)|null $subType */ + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType { return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); } @@ -50,7 +38,7 @@ public function toGraphQLInputType(Type $type, InputType|null $subType, string $ * * @param string $typeName The name of the GraphQL type */ - public function mapNameToType(string $typeName): NamedType + public function mapNameToType(string $typeName): NamedType&GraphQLType { return $this->next->mapNameToType($typeName); }