Skip to content

Commit

Permalink
Psr/container ^2 support (#518)
Browse files Browse the repository at this point in the history
* psr/container v2

* Implement replacement for Picotainer

* Replace Picotainer with LazyContainer in tests and documentation for version >=6

* Fix tests for phpdocumentor/type-resolver minor update

* run cs-fix script

* Fix some cs-fixer errors manually

* fix return type

* abandon psr/container ^1

* Test for LazyContainer
  • Loading branch information
l-you authored Oct 24, 2022
1 parent fc9eae6 commit fed9ffe
Show file tree
Hide file tree
Showing 145 changed files with 639 additions and 897 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"composer/package-versions-deprecated": "^1.8",
"phpdocumentor/reflection-docblock": "^4.3 || ^5.0",
"phpdocumentor/type-resolver": "^1.4",
"psr/container": "^1",
"psr/container": "^2",
"psr/http-factory": "^1",
"psr/http-message": "^1.0.1",
"psr/http-server-handler": "^1",
Expand All @@ -33,7 +33,6 @@
"doctrine/coding-standard": "^9.0 || ^10.0",
"ecodev/graphql-upload": "^6.1",
"laminas/laminas-diactoros": "^2",
"mouf/picotainer": "^1.1",
"myclabs/php-enum": "^1.6.6",
"php-coveralls/php-coveralls": "^2.1",
"phpstan/extension-installer": "^1.1",
Expand Down
8 changes: 2 additions & 6 deletions src/AggregateControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function __construct(private iterable $controllers, private FieldsBuilder
{
}

/**
* @return array<string,FieldDefinition>
*/
/** @return array<string,FieldDefinition> */
public function getQueries(): array
{
$queryList = [];
Expand All @@ -48,9 +46,7 @@ public function getQueries(): array
return $this->flattenList($queryList);
}

/**
* @return array<string, FieldDefinition>
*/
/** @return array<string, FieldDefinition> */
public function getMutations(): array
{
$mutationList = [];
Expand Down
12 changes: 3 additions & 9 deletions src/AggregateQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ class AggregateQueryProvider implements QueryProviderInterface
/** @var QueryProviderInterface[] */
private array $queryProviders;

/**
* @param QueryProviderInterface[] $queryProviders
*/
/** @param QueryProviderInterface[] $queryProviders */
public function __construct(iterable $queryProviders)
{
$this->queryProviders = is_array($queryProviders) ? $queryProviders : iterator_to_array($queryProviders);
}

/**
* @return QueryField[]
*/
/** @return QueryField[] */
public function getQueries(): array
{
$queriesArray = array_map(static function (QueryProviderInterface $queryProvider) {
Expand All @@ -40,9 +36,7 @@ public function getQueries(): array
return array_merge(...$queriesArray);
}

/**
* @return QueryField[]
*/
/** @return QueryField[] */
public function getMutations(): array
{
$mutationsArray = array_map(static function (QueryProviderInterface $queryProvider) {
Expand Down
43 changes: 18 additions & 25 deletions src/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AnnotationReader
{
// In this mode, no exceptions will be thrown for incorrect annotations (unless the name of the annotation we are looking for is part of the docblock)
public const LAX_MODE = 'LAX_MODE';

// In this mode, exceptions will be thrown for any incorrect annotations.
public const STRICT_MODE = 'STRICT_MODE';

Expand Down Expand Up @@ -86,7 +87,7 @@ public function __construct(private Reader $reader, private string $mode = self:
*
* @template T of object
*/
private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass): ?object
private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass): object|null
{
try {
$attribute = $refClass->getAttributes($annotationClass)[0] ?? null;
Expand Down Expand Up @@ -123,7 +124,7 @@ private function getClassAnnotation(ReflectionClass $refClass, string $annotatio
*
* @throws AnnotationException
*/
private function getMethodAnnotation(ReflectionMethod $refMethod, string $annotationClass): ?object
private function getMethodAnnotation(ReflectionMethod $refMethod, string $annotationClass): object|null
{
$cacheKey = $refMethod->getDeclaringClass()->getName() . '::' . $refMethod->getName() . '_' . $annotationClass;
if (array_key_exists($cacheKey, $this->methodAnnotationCache)) {
Expand Down Expand Up @@ -200,7 +201,7 @@ static function ($attribute) {
},
array_filter($refClass->getAttributes(), static function ($annotation) use ($annotationClass): bool {
return is_a($annotation->getName(), $annotationClass, true);
})
}),
);

$toAddAnnotations[] = $attributes;
Expand Down Expand Up @@ -231,7 +232,7 @@ static function ($attribute) {
*
* @template T of object
*/
public function getTypeAnnotation(ReflectionClass $refClass): ?TypeInterface
public function getTypeAnnotation(ReflectionClass $refClass): TypeInterface|null
{
try {
$type = $this->getClassAnnotation($refClass, Type::class)
Expand Down Expand Up @@ -276,7 +277,7 @@ public function getInputAnnotations(ReflectionClass $refClass): array
*
* @template T of object
*/
public function getExtendTypeAnnotation(ReflectionClass $refClass): ?ExtendType
public function getExtendTypeAnnotation(ReflectionClass $refClass): ExtendType|null
{
try {
$extendType = $this->getClassAnnotation($refClass, ExtendType::class);
Expand All @@ -287,15 +288,13 @@ public function getExtendTypeAnnotation(ReflectionClass $refClass): ?ExtendType
return $extendType;
}

public function getEnumTypeAnnotation(ReflectionClass $refClass): ?EnumType
public function getEnumTypeAnnotation(ReflectionClass $refClass): EnumType|null
{
return $this->getClassAnnotation($refClass, EnumType::class);
}

/**
* @param class-string<AbstractRequest> $annotationClass
*/
public function getRequestAnnotation(ReflectionMethod $refMethod, string $annotationClass): ?AbstractRequest
/** @param class-string<AbstractRequest> $annotationClass */
public function getRequestAnnotation(ReflectionMethod $refMethod, string $annotationClass): AbstractRequest|null
{
$queryAnnotation = $this->getMethodAnnotation($refMethod, $annotationClass);
assert($queryAnnotation instanceof AbstractRequest || $queryAnnotation === null);
Expand All @@ -318,15 +317,15 @@ public function getSourceFields(ReflectionClass $refClass): array
return $sourceFields;
}

public function getFactoryAnnotation(ReflectionMethod $refMethod): ?Factory
public function getFactoryAnnotation(ReflectionMethod $refMethod): Factory|null
{
$factoryAnnotation = $this->getMethodAnnotation($refMethod, Factory::class);
assert($factoryAnnotation instanceof Factory || $factoryAnnotation === null);

return $factoryAnnotation;
}

public function getDecorateAnnotation(ReflectionMethod $refMethod): ?Decorate
public function getDecorateAnnotation(ReflectionMethod $refMethod): Decorate|null
{
$decorateAnnotation = $this->getMethodAnnotation($refMethod, Decorate::class);
assert($decorateAnnotation instanceof Decorate || $decorateAnnotation === null);
Expand Down Expand Up @@ -376,9 +375,7 @@ public function getParameterAnnotationsPerParameter(array $refParameters): array
/** @var ParameterAnnotationInterface[] $parameterAnnotations */
$parameterAnnotations = $this->getMethodAnnotations($method, ParameterAnnotationInterface::class);

/**
* @var array<string, array<int,ParameterAnnotations>> $parameterAnnotationsPerParameter
*/
/** @var array<string, array<int,ParameterAnnotations>> $parameterAnnotationsPerParameter */
$parameterAnnotationsPerParameter = [];
foreach ($parameterAnnotations as $parameterAnnotation) {
$parameterAnnotationsPerParameter[$parameterAnnotation->getTarget()][] = $parameterAnnotation;
Expand Down Expand Up @@ -406,25 +403,21 @@ static function ($attribute) {
},
array_filter($attributes, static function ($annotation): bool {
return is_a($annotation->getName(), ParameterAnnotationInterface::class, true);
})
}),
),
];
}

return array_map(
static function (array $parameterAnnotations): ParameterAnnotations {
/**
* @var ParameterAnnotationInterface[] $parameterAnnotations
*/
/** @var ParameterAnnotationInterface[] $parameterAnnotations */
return new ParameterAnnotations($parameterAnnotations);
},
$parameterAnnotationsPerParameter
$parameterAnnotationsPerParameter,
);
}

/**
* @throws AnnotationException
*/
/** @throws AnnotationException */
public function getMiddlewareAnnotations(ReflectionMethod|ReflectionProperty $reflection): MiddlewareAnnotations
{
if ($reflection instanceof ReflectionMethod) {
Expand Down Expand Up @@ -469,7 +462,7 @@ static function ($attribute) {
},
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
return is_a($annotation->getName(), $annotationClass, true);
})
}),
),
];
} catch (AnnotationException $e) {
Expand Down Expand Up @@ -525,7 +518,7 @@ static function ($attribute) {
},
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
return is_a($annotation->getName(), $annotationClass, true);
})
}),
),
];
} catch (AnnotationException $e) {
Expand Down
14 changes: 6 additions & 8 deletions src/Annotations/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

abstract class AbstractRequest
{
private ?string $outputType;
private string|null $outputType;

private ?string $name;
private string|null $name;

/**
* @param mixed[] $attributes
*/
public function __construct(array $attributes = [], ?string $name = null, ?string $outputType = null)
/** @param mixed[] $attributes */
public function __construct(array $attributes = [], string|null $name = null, string|null $outputType = null)
{
$this->outputType = $outputType ?? $attributes['outputType'] ?? null;
$this->name = $name ?? $attributes['name'] ?? null;
Expand All @@ -23,7 +21,7 @@ public function __construct(array $attributes = [], ?string $name = null, ?strin
* Returns the GraphQL return type of the request (as a string).
* The string can represent the FQCN of the type or an entry in the container resolving to the GraphQL type.
*/
public function getOutputType(): ?string
public function getOutputType(): string|null
{
return $this->outputType;
}
Expand All @@ -32,7 +30,7 @@ public function getOutputType(): ?string
* Returns the name of the GraphQL query/mutation/field.
* If not specified, the name of the method should be used instead.
*/
public function getName(): ?string
public function getName(): string|null
{
return $this->name;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Annotations/Autowire.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class Autowire implements ParameterAnnotationInterface
/** @var string|null */
private $identifier;

/**
* @param array<string, mixed>|string $identifier
*/
/** @param array<string, mixed>|string $identifier */
public function __construct(array|string $identifier = [])
{
$values = $identifier;
Expand All @@ -52,7 +50,7 @@ public function getTarget(): string
return $this->for;
}

public function getIdentifier(): ?string
public function getIdentifier(): string|null
{
return $this->identifier;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Annotations/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ class EnumType
/** @var bool */
private $useValues;

/**
* @param mixed[] $attributes
*/
public function __construct(array $attributes = [], ?string $name = null, ?bool $useValues = null)
/** @param mixed[] $attributes */
public function __construct(array $attributes = [], string|null $name = null, bool|null $useValues = null)
{
$this->name = $name ?? $attributes['name'] ?? null;
$this->useValues = $useValues ?? $attributes['useValues'] ?? false;
Expand All @@ -38,7 +36,7 @@ public function __construct(array $attributes = [], ?string $name = null, ?bool
/**
* Returns the GraphQL name for this type.
*/
public function getName(): ?string
public function getName(): string|null
{
return $this->name;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Annotations/ExtendType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class ExtendType
/** @var string|null */
private $name;

/**
* @param mixed[] $attributes
*/
public function __construct(array $attributes = [], ?string $class = null, ?string $name = null)
/** @param mixed[] $attributes */
public function __construct(array $attributes = [], string|null $class = null, string|null $name = null)
{
$className = isset($attributes['class']) ? ltrim($attributes['class'], '\\') : null;
$className = $className ?? $class;
Expand All @@ -53,12 +51,12 @@ public function __construct(array $attributes = [], ?string $class = null, ?stri
*
* @return class-string<object>|null
*/
public function getClass(): ?string
public function getClass(): string|null
{
return $this->class;
}

public function getName(): ?string
public function getName(): string|null
{
return $this->name;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Annotations/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ class Factory
/** @var bool */
private $default;

/**
* @param mixed[] $attributes
*/
public function __construct(array $attributes = [], ?string $name = null, ?bool $default = null)
/** @param mixed[] $attributes */
public function __construct(array $attributes = [], string|null $name = null, bool|null $default = null)
{
$this->name = $name ?? $attributes['name'] ?? null;
// This IS the default if no name is set and no "default" attribute is passed.
Expand All @@ -43,7 +41,7 @@ public function __construct(array $attributes = [], ?string $name = null, ?bool
* Returns the name of the GraphQL input type.
* If not specified, the name of the method should be used instead.
*/
public function getName(): ?string
public function getName(): string|null
{
return $this->name;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Annotations/FailWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class FailWith implements MiddlewareAnnotationInterface
*/
private $value;

/**
* @throws BadMethodCallException
*/
/** @throws BadMethodCallException */
public function __construct(mixed $values = [], mixed $value = '__fail__with__magic__key__')
{
if ($value !== '__fail__with__magic__key__') {
Expand Down
14 changes: 6 additions & 8 deletions src/Annotations/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Field extends AbstractRequest
* @param mixed[] $attributes
* @param string|string[] $for
*/
public function __construct(array $attributes = [], ?string $name = null, ?string $outputType = null, ?string $prefetchMethod = null, string|array|null $for = null, ?string $description = null, ?string $inputType = null)
public function __construct(array $attributes = [], string|null $name = null, string|null $outputType = null, string|null $prefetchMethod = null, string|array|null $for = null, string|null $description = null, string|null $inputType = null)
{
parent::__construct($attributes, $name, $outputType);
$this->prefetchMethod = $prefetchMethod ?? $attributes['prefetchMethod'] ?? null;
Expand All @@ -59,25 +59,23 @@ public function __construct(array $attributes = [], ?string $name = null, ?strin
/**
* Returns the prefetch method name (the method that will be called to fetch many records at once)
*/
public function getPrefetchMethod(): ?string
public function getPrefetchMethod(): string|null
{
return $this->prefetchMethod;
}

/**
* @return string[]|null
*/
public function getFor(): ?array
/** @return string[]|null */
public function getFor(): array|null
{
return $this->for;
}

public function getDescription(): ?string
public function getDescription(): string|null
{
return $this->description;
}

public function getInputType(): ?string
public function getInputType(): string|null
{
return $this->inputType;
}
Expand Down
Loading

0 comments on commit fed9ffe

Please sign in to comment.