Skip to content

Commit

Permalink
Fixed PHPCS errors
Browse files Browse the repository at this point in the history
  • Loading branch information
oojacoboo committed Feb 12, 2024
1 parent fd11330 commit 88b5bd7
Show file tree
Hide file tree
Showing 26 changed files with 374 additions and 191 deletions.
3 changes: 2 additions & 1 deletion src/AggregateControllerQueryProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class AggregateControllerQueryProviderFactory implements QueryProviderFactoryInt
public function __construct(
private readonly iterable $controllers,
private readonly ContainerInterface $controllersContainer,
) {}
) {
}

public function create(FactoryContext $context): QueryProviderInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Exceptions/GraphQLAggregateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class GraphQLAggregateException extends Exception implements GraphQLAggregateExc
public function __construct(iterable $exceptions = [])
{
parent::__construct('Many exceptions have be thrown:');

foreach ($exceptions as $exception) {
$this->add($exception);
}
Expand Down
5 changes: 3 additions & 2 deletions src/FailedResolvingInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

namespace TheCodingMachine\GraphQLite;

use ArgumentCountError;
use RuntimeException;

use function sprintf;

class FailedResolvingInputType extends RuntimeException
{
public static function createForMissingConstructorParameter(\ArgumentCountError $original): self
public static function createForMissingConstructorParameter(ArgumentCountError $original): self
{
return new self(sprintf("%s. It should be mapped as required field.", $original->getMessage()), previous: $original);
return new self(sprintf('%s. It should be mapped as required field.', $original->getMessage()), previous: $original);
}

public static function createForDecorator(string $class): self
Expand Down
306 changes: 210 additions & 96 deletions src/FieldsBuilder.php

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/GlobControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace TheCodingMachine\GraphQLite;

use function class_exists;
use function interface_exists;
use function is_array;
use function str_replace;
use GraphQL\Type\Definition\FieldDefinition;
use InvalidArgumentException;
use Mouf\Composer\ClassNameMapper;
Expand All @@ -22,6 +18,11 @@
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Annotations\Subscription;

use function class_exists;
use function interface_exists;
use function is_array;
use function str_replace;

/**
* Scans all the classes in a given namespace of the main project (not the vendor directory).
* Analyzes all classes and detects "Query", "Mutation", and "Subscription" annotations.
Expand Down
6 changes: 2 additions & 4 deletions src/InputField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
bool $isUpdate,
bool $hasDefaultValue,
mixed $defaultValue,
array|null $additionalConfig = null
array|null $additionalConfig = null,
) {
$config = [
'name' => $name,
Expand All @@ -63,9 +63,7 @@ public function __construct(

$this->resolve = function (object|null $source, array $args, $context, ResolveInfo $info) use ($arguments, $originalResolver, $resolver) {
if ($this->forConstructorHydration) {
$toPassArgs = [
$arguments[$this->name]->resolve($source, $args, $context, $info)
];
$toPassArgs = [$arguments[$this->name]->resolve($source, $args, $context, $info)];
} else {
$toPassArgs = $this->paramsToArguments($arguments, $source, $args, $context, $info, $resolver);
}
Expand Down
8 changes: 0 additions & 8 deletions src/InputFieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@

use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\Type;
use ReflectionMethod;
use ReflectionProperty;
use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotations;
use TheCodingMachine\GraphQLite\Middlewares\MagicPropertyResolver;
use TheCodingMachine\GraphQLite\Middlewares\ResolverInterface;
use TheCodingMachine\GraphQLite\Middlewares\ServiceResolver;
use TheCodingMachine\GraphQLite\Middlewares\SourceConstructorParameterResolver;
use TheCodingMachine\GraphQLite\Middlewares\SourceInputPropertyResolver;
use TheCodingMachine\GraphQLite\Middlewares\SourceMethodResolver;
use TheCodingMachine\GraphQLite\Middlewares\SourcePropertyResolver;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use TheCodingMachine\GraphQLite\Utils\Cloneable;

use function assert;
use function is_callable;

/**
* A class that describes a field to be created.
* To contains getters and setters to alter the field behaviour.
Expand Down
1 change: 0 additions & 1 deletion src/InputTypeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use ReflectionNamedType;
use RuntimeException;
use TheCodingMachine\GraphQLite\Parameters\ExpandsInputTypeParameters;
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameter;
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameterInterface;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;

Expand Down
17 changes: 14 additions & 3 deletions src/Mappers/AbstractTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputInterface;

use UnitEnum;
use function assert;

/**
Expand Down Expand Up @@ -70,8 +69,20 @@ public function __construct(
)
{
$this->cacheContract = new Psr16Adapter($this->cache, $cachePrefix, $this->globTTL ?? 0);
$this->mapClassToAnnotationsCache = new ClassBoundCacheContract(new ClassBoundMemoryAdapter(new ClassBoundCache(new FileBoundCache($this->cache, 'classToAnnotations_' . $cachePrefix))));
$this->mapClassToExtendAnnotationsCache = new ClassBoundCacheContract(new ClassBoundMemoryAdapter(new ClassBoundCache(new FileBoundCache($this->cache, 'classToExtendAnnotations_' . $cachePrefix))));

$classToAnnotationsCache = new ClassBoundCache(
new FileBoundCache($this->cache, 'classToAnnotations_' . $cachePrefix),
);
$this->mapClassToAnnotationsCache = new ClassBoundCacheContract(
new ClassBoundMemoryAdapter($classToAnnotationsCache),
);

$classToExtendedAnnotationsCache = new ClassBoundCache(
new FileBoundCache($this->cache, 'classToExtendAnnotations_' . $cachePrefix),
);
$this->mapClassToExtendAnnotationsCache = new ClassBoundCacheContract(
new ClassBoundMemoryAdapter($classToExtendedAnnotationsCache),
);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Mappers/GlobAnnotationsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ final class GlobAnnotationsCache

/**
* @param class-string<object>|null $typeClassName
* @param array<string, array{0: string, 1:class-string<object>|null, 2:bool, 3:class-string<object>}> $factories An array mapping a factory method name to an input name / class name / default flag / declaring class
* @param array<string, array{0: string, 1:class-string<object>}> $decorators An array mapping a decorator method name to an input name / declaring class
* @param array<string, array{0: class-string<object>, 1: bool, 2: string|null, 3: bool}> $inputs An array mapping an input type name to an input name / declaring class
* @param array<string, array{0: string, 1:class-string<object>|null, 2:bool, 3:class-string<object>}> $factories
* An array mapping a factory method name to an input name / class name / default flag /
* declaring class
* @param array<string, array{0: string, 1:class-string<object>}> $decorators
* An array mapping a decorator method name to an input name / declaring class
* @param array<string, array{0: class-string<object>, 1: bool, 2: string|null, 3: bool}> $inputs
* An array mapping an input type name to an input name / declaring class
*/
public function __construct(
private readonly string|null $typeClassName = null,
Expand Down Expand Up @@ -67,6 +71,7 @@ public function registerFactory(string $methodName, string $inputName, string|nu
return $this->with(
factories: [
...$this->factories,
// phpcs:ignore
$methodName => [$inputName, $className, $isDefault, $declaringClass],
],
);
Expand All @@ -78,6 +83,7 @@ public function registerDecorator(string $methodName, string $inputName, string
return $this->with(
decorators: [
...$this->decorators,
// phpcs:ignore
$methodName => [$inputName, $declaringClass],
],
);
Expand Down Expand Up @@ -105,6 +111,7 @@ public function registerInput(string $name, string $className, Input $input): se
return $this->with(
inputs: [
...$this->inputs,
// phpcs:ignore
$name => [$className, $input->isDefault(), $input->getDescription(), $input->isUpdate()],
],
);
Expand Down
43 changes: 38 additions & 5 deletions src/Mappers/GlobTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,44 @@
*/
final class GlobTypeMapper extends AbstractTypeMapper
{
/** @param NS $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation) */
public function __construct(private NS $namespace, TypeGenerator $typeGenerator, InputTypeGenerator $inputTypeGenerator, InputTypeUtils $inputTypeUtils, ContainerInterface $container, AnnotationReader $annotationReader, NamingStrategyInterface $namingStrategy, RecursiveTypeMapperInterface $recursiveTypeMapper, CacheInterface $cache, int|null $globTTL = 2, int|null $mapTTL = null)
{
$cachePrefix = str_replace(['\\', '{', '}', '(', ')', '/', '@', ':'], '_', $namespace->getNamespace());
parent::__construct($cachePrefix, $typeGenerator, $inputTypeGenerator, $inputTypeUtils, $container, $annotationReader, $namingStrategy, $recursiveTypeMapper, $cache, $globTTL, $mapTTL);
/**
* Constructor
*
* @param NS $namespace The namespace that contains the GraphQL types
* (they must have a `@Type` annotation)
*/
public function __construct(
private NS $namespace,
TypeGenerator $typeGenerator,
InputTypeGenerator $inputTypeGenerator,
InputTypeUtils $inputTypeUtils,
ContainerInterface $container,
AnnotationReader $annotationReader,
NamingStrategyInterface $namingStrategy,
RecursiveTypeMapperInterface $recursiveTypeMapper,
CacheInterface $cache,
int|null $globTTL = 2,
int|null $mapTTL = null,
) {
$cachePrefix = str_replace(
['\\', '{', '}', '(', ')', '/', '@', ':'],
'_',
$namespace->getNamespace(),
);

parent::__construct(
$cachePrefix,
$typeGenerator,
$inputTypeGenerator,
$inputTypeUtils,
$container,
$annotationReader,
$namingStrategy,
$recursiveTypeMapper,
$cache,
$globTTL,
$mapTTL,
);
}

/**
Expand Down
25 changes: 15 additions & 10 deletions src/Mappers/Root/EnumTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use function assert;
use function enum_exists;
use function ltrim;

/**
* Maps an enum class to a GraphQL type (only available in PHP>=8.1)
Expand Down Expand Up @@ -129,25 +130,29 @@ private function mapByClassName(string $enumClass): EnumType|null
$enumDescription = $docBlock->getSummary();
}

/** @var array<string, string> $enumCaseDescriptions */
$enumCaseDescriptions = [];
/** @var array<string, string> $enumCaseDeprecationReasons */
$enumCaseDeprecationReasons = [];

foreach ($reflectionEnum->getCases() as $reflectionEnumCase) {
$docComment = $reflectionEnumCase->getDocComment();
if ($docComment) {
$docBlock = $docBlockFactory->create($docComment);
$enumCaseDescription = $docBlock->getSummary();
if (! $docComment) {
continue;
}

$enumCaseDescriptions[$reflectionEnumCase->getName()] = $enumCaseDescription;
$deprecation = $docBlock->getTagsByName('deprecated')[0] ?? null;
$docBlock = $docBlockFactory->create($docComment);
$enumCaseDescription = $docBlock->getSummary();

if ($deprecation) {
$enumCaseDeprecationReasons[$reflectionEnumCase->getName()] = (string) $deprecation;
}
$enumCaseDescriptions[$reflectionEnumCase->getName()] = $enumCaseDescription;
$deprecation = $docBlock->getTagsByName('deprecated')[0] ?? null;

// phpcs:ignore
if ($deprecation) {
$enumCaseDeprecationReasons[$reflectionEnumCase->getName()] = (string) $deprecation;
}
}

/** @var array<string, string> $enumCaseDescriptions */
/** @var array<string, string> $enumCaseDeprecationReasons */
$type = new EnumType($enumClass, $typeName, $enumDescription, $enumCaseDescriptions, $enumCaseDeprecationReasons, $useValues);

return $this->cacheByName[$type->name] = $this->cache[$enumClass] = $type;
Expand Down
28 changes: 13 additions & 15 deletions src/Mappers/Root/MyCLabsEnumTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ public function toGraphQLInputType(
): InputType&\GraphQL\Type\Definition\Type
{
$result = $this->map($type);
return $result ?? $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj);
return $result ?? $this->next->toGraphQLInputType(
$type,
$subType,
$argumentName,
$reflector,
$docBlockObj,
);
}

private function map(Type $type): EnumType|null
Expand Down Expand Up @@ -101,7 +107,6 @@ private function mapByClassName(string $enumClass): EnumType|null
return $this->cacheByName[$type->name] = $this->cache[$enumClass] = $type;
}


private function getTypeName(ReflectionClass $refClass): string
{
$enumType = $this->annotationReader->getEnumTypeAnnotation($refClass);
Expand All @@ -116,16 +121,17 @@ private function getTypeName(ReflectionClass $refClass): string

/**
* Returns a GraphQL type by name.
* If this root type mapper can return this type in "toGraphQLOutputType" or "toGraphQLInputType", it should
* also map these types by name in the "mapNameToType" method.
* If this root type mapper can return this type in "toGraphQLOutputType" or "toGraphQLInputType",
* it should also map these types by name in the "mapNameToType" method.
*
* @param string $typeName The name of the GraphQL type
*/
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.
// This is actually not an issue in real life scenarios where enum types are never queried by type name.
// This is actually not an issue in real life scenarios where enum types are never queried
// by type name.
if (isset($this->cacheByName[$typeName])) {
return $this->cacheByName[$typeName];
}
Expand All @@ -138,15 +144,6 @@ public function mapNameToType(string $typeName): NamedType&\GraphQL\Type\Definit
return $type;
}

/*if (strpos($typeName, 'MyCLabsEnum_') === 0) {
$className = str_replace('__', '\\', substr($typeName, 12));
$type = $this->mapByClassName($className);
if ($type !== null) {
return $type;
}
}*/

return $this->next->mapNameToType($typeName);
}

Expand All @@ -161,15 +158,16 @@ private function getNameToClassMapping(): array
$this->nameToClassMapping = $this->cacheService->get('myclabsenum_name_to_class', function () {
$nameToClassMapping = [];
foreach ($this->namespaces as $ns) {
/** @var class-string<Enum> $className */
foreach ($ns->getClassList() as $className => $classRef) {
if (! $classRef->isSubclassOf(Enum::class)) {
continue;
}

/** @var class-string<Enum> $className */
$nameToClassMapping[$this->getTypeName($classRef)] = $className;
}
}

return $nameToClassMapping;
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/Mappers/Root/RootTypeMapperFactoryContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
final class RootTypeMapperFactoryContext
{
/**
* Constructor
*
* @param iterable<NS> $typeNamespaces
*/
public function __construct(
Expand Down
Loading

0 comments on commit 88b5bd7

Please sign in to comment.