Skip to content

Commit ae6db57

Browse files
l-youvyacheslavko
andauthored
Some code improvements (#508)
* unit test for union type with null * Nullable union test * Nullable union bug fix * Fix test for old php version * Targeting PHP minimum version 8.0 * Targeting PHP minimum version 8.0 * Targeting PHP minimum version 8.0 * Run cs fix * Expanding php 8.0 features usage * Expanding php 8.0 features usage * Expanding php 8.0 features usage * Datetime class name bug fix * Failing tests fixed * Bad params with stdClass test removed. It's already throws an error in php 8.0 * Improving code with php 8.0 features * Improving code with php 8.0 features * Improving code with php 8.0 features * Improving code with php 8.0 features * Improving code with php 8.0 features * Improving code with php 8.0 features * Expanding php 8.0 features * drop test for php <=7.4 * remove some php 7.4 polyfill * Version 6.1 docs. Drop all examples for php <8.0. * Failed tests fix * Improving code with php 8 features * Improving code with php 8 features * Improving code with php 8 features * Improving code with php 8 features * Improving code with php 8 features * Improving code with php 8 features * Improving code with php 8 features * Remove php 7.4 from CI tests * Improving code with php 8 features * Improving code with php 8 features * Replacing array_merge with built-in spread syntax for lists due to big performance difference. * Removed types: Container does not match CI server psr extension classes * Failing CI test fix * Failing CI test fix * Fix types for fields descriptors. That caused misunderstanding in methods purpose... * Failing CI test fix. * Failing CI test fix. * Failing CI test fix. Just remove throws tag * Failing CI test fix. Just remove throws tag Co-authored-by: madness <ss@gmail.com>
1 parent f3d944a commit ae6db57

File tree

172 files changed

+6116
-1663
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+6116
-1663
lines changed

.github/workflows/continuous_integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
matrix:
2020
install-args: ['', '--prefer-lowest']
21-
php-version: ['7.4', '8.0', '8.1']
21+
php-version: ['8.0', '8.1']
2222
fail-fast: false
2323

2424
steps:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=7.4",
13+
"php": ">=8.0",
1414
"ext-json": "*",
1515
"doctrine/annotations": "1.13.2",
1616
"composer/package-versions-deprecated": "^1.8",

src/AggregateControllerQueryProvider.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,16 @@
2525
*/
2626
class AggregateControllerQueryProvider implements QueryProviderInterface
2727
{
28-
/** @var iterable<string> */
29-
private $controllers;
30-
/** @var ContainerInterface */
31-
private $controllersContainer;
32-
/** @var FieldsBuilder */
33-
private $fieldsBuilder;
34-
3528
/**
3629
* @param iterable<string> $controllers A list of controllers name in the container.
3730
* @param ContainerInterface $controllersContainer The container we will fetch controllers from.
3831
*/
39-
public function __construct(iterable $controllers, FieldsBuilder $fieldsBuilder, ContainerInterface $controllersContainer)
32+
public function __construct(private iterable $controllers, private FieldsBuilder $fieldsBuilder, private ContainerInterface $controllersContainer)
4033
{
41-
$this->controllers = $controllers;
42-
$this->fieldsBuilder = $fieldsBuilder;
43-
$this->controllersContainer = $controllersContainer;
4434
}
4535

4636
/**
47-
* @return FieldDefinition[]
37+
* @return array<string,FieldDefinition>
4838
*/
4939
public function getQueries(): array
5040
{
@@ -59,7 +49,7 @@ public function getQueries(): array
5949
}
6050

6151
/**
62-
* @return FieldDefinition[]
52+
* @return array<string, FieldDefinition>
6353
*/
6454
public function getMutations(): array
6555
{

src/AggregateControllerQueryProviderFactory.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@
1111
*/
1212
class AggregateControllerQueryProviderFactory implements QueryProviderFactoryInterface
1313
{
14-
/** @var iterable<string> */
15-
private $controllers;
16-
/** @var ContainerInterface */
17-
private $controllersContainer;
18-
1914
/**
20-
* @param iterable<string> $controllers A list of controllers name in the container.
15+
* @param iterable<string> $controllers A list of controllers name in the container.
2116
* @param ContainerInterface $controllersContainer The container we will fetch controllers from.
2217
*/
23-
public function __construct(iterable $controllers, ContainerInterface $controllersContainer)
18+
public function __construct(private iterable $controllers, private ContainerInterface $controllersContainer)
2419
{
25-
$this->controllers = $controllers;
26-
$this->controllersContainer = $controllersContainer;
2720
}
2821

2922
public function create(FactoryContext $context): QueryProviderInterface

src/AggregateQueryProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class AggregateQueryProvider implements QueryProviderInterface
1616
{
1717
/** @var QueryProviderInterface[] */
18-
private $queryProviders;
18+
private array $queryProviders;
1919

2020
/**
2121
* @param QueryProviderInterface[] $queryProviders

src/AnnotationReader.php

Lines changed: 61 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,39 @@
4040
use function in_array;
4141
use function is_a;
4242
use function reset;
43-
use function strpos;
43+
use function str_contains;
44+
use function str_starts_with;
4445
use function strrpos;
4546
use function substr;
4647

4748
use const PHP_MAJOR_VERSION;
4849

4950
class AnnotationReader
5051
{
51-
/** @var Reader */
52-
private $reader;
53-
5452
// 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)
5553
public const LAX_MODE = 'LAX_MODE';
5654
// In this mode, exceptions will be thrown for any incorrect annotations.
5755
public const STRICT_MODE = 'STRICT_MODE';
5856

59-
/**
60-
* Classes in those namespaces MUST have valid annotations (otherwise, an error is thrown).
61-
*
62-
* @var string[]
63-
*/
64-
private $strictNamespaces;
65-
66-
/**
67-
* If true, no exceptions will be thrown for incorrect annotations in code coming from the "vendor/" directory.
68-
*
69-
* @var string
70-
*/
71-
private $mode;
7257

7358
/** @var array<string, (object|null)> */
74-
private $methodAnnotationCache = [];
59+
private array $methodAnnotationCache = [];
7560

7661
/** @var array<string, array<object>> */
77-
private $methodAnnotationsCache = [];
62+
private array $methodAnnotationsCache = [];
7863

7964
/** @var array<string, array<object>> */
80-
private $propertyAnnotationsCache = [];
65+
private array $propertyAnnotationsCache = [];
8166

8267
/**
83-
* @param string $mode One of self::LAX_MODE or self::STRICT_MODE
84-
* @param string[] $strictNamespaces
68+
* @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.
69+
* @param string[] $strictNamespaces Classes in those namespaces MUST have valid annotations (otherwise, an error is thrown).
8570
*/
86-
public function __construct(Reader $reader, string $mode = self::STRICT_MODE, array $strictNamespaces = [])
71+
public function __construct(private Reader $reader, private string $mode = self::STRICT_MODE, private array $strictNamespaces = [])
8772
{
88-
$this->reader = $reader;
8973
if (! in_array($mode, [self::LAX_MODE, self::STRICT_MODE], true)) {
9074
throw new InvalidArgumentException('The mode passed must be one of AnnotationReader::LAX_MODE, AnnotationReader::STRICT_MODE');
9175
}
92-
$this->mode = $mode;
93-
$this->strictNamespaces = $strictNamespaces;
9476
}
9577

9678
/**
@@ -108,7 +90,6 @@ public function __construct(Reader $reader, string $mode = self::STRICT_MODE, ar
10890
*/
10991
private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass): ?object
11092
{
111-
$type = null;
11293
try {
11394
// If attribute & annotation, let's prefer the PHP 8 attribute
11495
if (PHP_MAJOR_VERSION >= 8) {
@@ -123,6 +104,7 @@ private function getClassAnnotation(ReflectionClass $refClass, string $annotatio
123104

124105
$type = $this->reader->getClassAnnotation($refClass, $annotationClass);
125106
assert($type === null || $type instanceof $annotationClass);
107+
return $type;
126108
} catch (AnnotationException $e) {
127109
switch ($this->mode) {
128110
case self::STRICT_MODE:
@@ -131,21 +113,22 @@ private function getClassAnnotation(ReflectionClass $refClass, string $annotatio
131113
case self::LAX_MODE:
132114
if ($this->isErrorImportant($annotationClass, $refClass->getDocComment() ?: '', $refClass->getName())) {
133115
throw $e;
134-
} else {
135-
return null;
136116
}
117+
118+
return null;
119+
137120
default:
138121
throw new RuntimeException("Unexpected mode '" . $this->mode . "'."); // @codeCoverageIgnore
139122
}
140123
}
141-
142-
return $type;
143124
}
144125

145126
/**
146127
* Returns a method annotation and handles correctly errors.
147128
*
148129
* @param class-string<object> $annotationClass
130+
*
131+
* @throws AnnotationException
149132
*/
150133
private function getMethodAnnotation(ReflectionMethod $refMethod, string $annotationClass): ?object
151134
{
@@ -173,9 +156,10 @@ private function getMethodAnnotation(ReflectionMethod $refMethod, string $annota
173156
case self::LAX_MODE:
174157
if ($this->isErrorImportant($annotationClass, $refMethod->getDocComment() ?: '', $refMethod->getDeclaringClass()->getName())) {
175158
throw $e;
176-
} else {
177-
return null;
178159
}
160+
161+
return null;
162+
179163
default:
180164
throw new RuntimeException("Unexpected mode '" . $this->mode . "'."); // @codeCoverageIgnore
181165
}
@@ -188,13 +172,13 @@ private function getMethodAnnotation(ReflectionMethod $refMethod, string $annota
188172
private function isErrorImportant(string $annotationClass, string $docComment, string $className): bool
189173
{
190174
foreach ($this->strictNamespaces as $strictNamespace) {
191-
if (strpos($className, $strictNamespace) === 0) {
175+
if (str_starts_with($className, $strictNamespace)) {
192176
return true;
193177
}
194178
}
195179
$shortAnnotationClass = substr($annotationClass, strrpos($annotationClass, '\\') + 1);
196180

197-
return strpos($docComment, '@' . $shortAnnotationClass) !== false;
181+
return str_contains($docComment, '@' . $shortAnnotationClass);
198182
}
199183

200184
/**
@@ -212,13 +196,10 @@ private function isErrorImportant(string $annotationClass, string $docComment, s
212196
*/
213197
public function getClassAnnotations(ReflectionClass $refClass, string $annotationClass, bool $inherited = true): array
214198
{
215-
/**
216-
* @var array<array<A>>
217-
*/
218199
$toAddAnnotations = [];
219200
do {
220201
try {
221-
$allAnnotations = $this->reader->getClassAnnotations($refClass);
202+
$allAnnotations = $this->reader->getClassAnnotations($refClass);
222203
$toAddAnnotations[] = array_filter($allAnnotations, static function ($annotation) use ($annotationClass): bool {
223204
return $annotation instanceof $annotationClass;
224205
});
@@ -242,10 +223,11 @@ static function ($attribute) {
242223
throw $e;
243224
}
244225

245-
if ($this->mode === self::LAX_MODE) {
246-
if ($this->isErrorImportant($annotationClass, $refClass->getDocComment() ?: '', $refClass->getName())) {
247-
throw $e;
248-
}
226+
if (
227+
($this->mode === self::LAX_MODE)
228+
&& $this->isErrorImportant($annotationClass, $refClass->getDocComment() ?: '', $refClass->getName())
229+
) {
230+
throw $e;
249231
}
250232
}
251233
$refClass = $refClass->getParentClass();
@@ -433,14 +415,17 @@ public function getParameterAnnotationsPerParameter(array $refParameters): array
433415
foreach ($refParameters as $refParameter) {
434416
Assert::methodExists($refParameter, 'getAttributes');
435417
$attributes = $refParameter->getAttributes();
436-
$parameterAnnotationsPerParameter[$refParameter->getName()] = array_merge($parameterAnnotationsPerParameter[$refParameter->getName()] ?? [], array_map(
437-
static function ($attribute) {
438-
return $attribute->newInstance();
439-
},
440-
array_filter($attributes, static function ($annotation): bool {
441-
return is_a($annotation->getName(), ParameterAnnotationInterface::class, true);
442-
})
443-
));
418+
$parameterAnnotationsPerParameter[$refParameter->getName()] = [...$parameterAnnotationsPerParameter[$refParameter->getName()] ??
419+
[],
420+
...array_map(
421+
static function ($attribute) {
422+
return $attribute->newInstance();
423+
},
424+
array_filter($attributes, static function ($annotation): bool {
425+
return is_a($annotation->getName(), ParameterAnnotationInterface::class, true);
426+
})
427+
),
428+
];
444429
}
445430
}
446431

@@ -451,11 +436,9 @@ static function ($attribute) {
451436
}
452437

453438
/**
454-
* @param ReflectionMethod|ReflectionProperty $reflection
455-
*
456439
* @throws AnnotationException
457440
*/
458-
public function getMiddlewareAnnotations($reflection): MiddlewareAnnotations
441+
public function getMiddlewareAnnotations(ReflectionMethod|ReflectionProperty $reflection): MiddlewareAnnotations
459442
{
460443
if ($reflection instanceof ReflectionMethod) {
461444
$middlewareAnnotations = $this->getMethodAnnotations($reflection, MiddlewareAnnotationInterface::class);
@@ -486,21 +469,24 @@ public function getMethodAnnotations(ReflectionMethod $refMethod, string $annota
486469

487470
$toAddAnnotations = [];
488471
try {
489-
$allAnnotations = $this->reader->getMethodAnnotations($refMethod);
472+
$allAnnotations = $this->reader->getMethodAnnotations($refMethod);
490473
$toAddAnnotations = array_filter($allAnnotations, static function ($annotation) use ($annotationClass): bool {
491474
return $annotation instanceof $annotationClass;
492475
});
493476
if (PHP_MAJOR_VERSION >= 8) {
494477
Assert::methodExists($refMethod, 'getAttributes');
495478
$attributes = $refMethod->getAttributes();
496-
$toAddAnnotations = array_merge($toAddAnnotations, array_map(
497-
static function ($attribute) {
498-
return $attribute->newInstance();
499-
},
500-
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
501-
return is_a($annotation->getName(), $annotationClass, true);
502-
})
503-
));
479+
$toAddAnnotations = [
480+
...$toAddAnnotations,
481+
...array_map(
482+
static function ($attribute) {
483+
return $attribute->newInstance();
484+
},
485+
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
486+
return is_a($annotation->getName(), $annotationClass, true);
487+
})
488+
),
489+
];
504490
}
505491
} catch (AnnotationException $e) {
506492
if ($this->mode === self::STRICT_MODE) {
@@ -542,21 +528,24 @@ public function getPropertyAnnotations(ReflectionProperty $refProperty, string $
542528

543529
$toAddAnnotations = [];
544530
try {
545-
$allAnnotations = $this->reader->getPropertyAnnotations($refProperty);
531+
$allAnnotations = $this->reader->getPropertyAnnotations($refProperty);
546532
$toAddAnnotations = array_filter($allAnnotations, static function ($annotation) use ($annotationClass): bool {
547533
return $annotation instanceof $annotationClass;
548534
});
549535
if (PHP_MAJOR_VERSION >= 8) {
550536
Assert::methodExists($refProperty, 'getAttributes');
551537
$attributes = $refProperty->getAttributes();
552-
$toAddAnnotations = array_merge($toAddAnnotations, array_map(
553-
static function ($attribute) {
554-
return $attribute->newInstance();
555-
},
556-
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
557-
return is_a($annotation->getName(), $annotationClass, true);
558-
})
559-
));
538+
$toAddAnnotations = [
539+
...$toAddAnnotations,
540+
...array_map(
541+
static function ($attribute) {
542+
return $attribute->newInstance();
543+
},
544+
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
545+
return is_a($annotation->getName(), $annotationClass, true);
546+
})
547+
),
548+
];
560549
}
561550
} catch (AnnotationException $e) {
562551
if ($this->mode === self::STRICT_MODE) {

src/Annotations/AbstractRequest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
abstract class AbstractRequest
88
{
9-
/** @var string|null */
10-
private $outputType;
9+
private ?string $outputType;
1110

12-
/** @var string|null */
13-
private $name;
11+
private ?string $name;
1412

1513
/**
1614
* @param mixed[] $attributes

src/Annotations/Autowire.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Autowire implements ParameterAnnotationInterface
3131
/**
3232
* @param array<string, mixed>|string $identifier
3333
*/
34-
public function __construct($identifier = [])
34+
public function __construct(array|string $identifier = [])
3535
{
3636
$values = $identifier;
3737
if (is_string($values)) {

src/Annotations/Decorate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Decorate
3030
*
3131
* @throws BadMethodCallException
3232
*/
33-
public function __construct($inputTypeName = [])
33+
public function __construct(array|string $inputTypeName = [])
3434
{
3535
$values = $inputTypeName;
3636
if (is_string($values)) {

0 commit comments

Comments
 (0)