Skip to content

Commit 65edfb7

Browse files
l-youvyacheslavko
andauthored
Code improvements (#510)
* 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 * Removed some php<8 code examples * Remove test limit for only >=8.0 as minimum php version is 8 from now * Improve type safety * Improve type safety * Improve type safety * Improve type safety * Remove unnecessary assertion * Replace with native assertion * Remove PHP_MAJOR_VERSION >= 8 conditions and redundant assertions as minimum php version is 8.0 * Replace webmozart/assert with native assertions, throw logic exception where needed. Consequently, remove phpstan/phpstan-webmozart-assert. * Improve type safety with better array type explanation Co-authored-by: madness <ss@gmail.com>
1 parent ae6db57 commit 65edfb7

Some content is hidden

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

47 files changed

+237
-281
lines changed

composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"symfony/expression-language": "^4 || ^5 || ^6",
2727
"thecodingmachine/cache-utils": "^1",
2828
"thecodingmachine/class-explorer": "^1.1.0",
29-
"webmozart/assert": "^1.10",
3029
"webonyx/graphql-php": "^v14.9.0"
3130
},
3231
"require-dev": {
@@ -39,7 +38,6 @@
3938
"php-coveralls/php-coveralls": "^2.1",
4039
"phpstan/extension-installer": "^1.1",
4140
"phpstan/phpstan": "^1.0",
42-
"phpstan/phpstan-webmozart-assert": "^1.0",
4341
"phpunit/phpunit": "^8.5.19 || ^9.5.8",
4442
"symfony/var-dumper": "^5.4 || ^6.0",
4543
"thecodingmachine/phpstan-strict-rules": "^1.0"

src/AnnotationReader.php

Lines changed: 73 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use TheCodingMachine\GraphQLite\Annotations\SourceFieldInterface;
2828
use TheCodingMachine\GraphQLite\Annotations\Type;
2929
use TheCodingMachine\GraphQLite\Annotations\TypeInterface;
30-
use Webmozart\Assert\Assert;
3130

3231
use function array_diff_key;
3332
use function array_filter;
@@ -36,6 +35,7 @@
3635
use function array_merge;
3736
use function array_values;
3837
use function assert;
38+
use function count;
3939
use function get_class;
4040
use function in_array;
4141
use function is_a;
@@ -45,8 +45,6 @@
4545
use function strrpos;
4646
use function substr;
4747

48-
use const PHP_MAJOR_VERSION;
49-
5048
class AnnotationReader
5149
{
5250
// 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)
@@ -66,7 +64,7 @@ class AnnotationReader
6664

6765
/**
6866
* @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).
67+
* @param array<int,string> $strictNamespaces Classes in those namespaces MUST have valid annotations (otherwise, an error is thrown).
7068
*/
7169
public function __construct(private Reader $reader, private string $mode = self::STRICT_MODE, private array $strictNamespaces = [])
7270
{
@@ -91,17 +89,12 @@ public function __construct(private Reader $reader, private string $mode = self:
9189
private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass): ?object
9290
{
9391
try {
94-
// If attribute & annotation, let's prefer the PHP 8 attribute
95-
if (PHP_MAJOR_VERSION >= 8) {
96-
Assert::methodExists($refClass, 'getAttributes');
97-
$attribute = $refClass->getAttributes($annotationClass)[0] ?? null;
98-
if ($attribute) {
99-
$instance = $attribute->newInstance();
100-
assert($instance instanceof $annotationClass);
101-
return $instance;
102-
}
92+
$attribute = $refClass->getAttributes($annotationClass)[0] ?? null;
93+
if ($attribute) {
94+
$instance = $attribute->newInstance();
95+
assert($instance instanceof $annotationClass);
96+
return $instance;
10397
}
104-
10598
$type = $this->reader->getClassAnnotation($refClass, $annotationClass);
10699
assert($type === null || $type instanceof $annotationClass);
107100
return $type;
@@ -138,13 +131,9 @@ private function getMethodAnnotation(ReflectionMethod $refMethod, string $annota
138131
}
139132

140133
try {
141-
// If attribute & annotation, let's prefer the PHP 8 attribute
142-
if (PHP_MAJOR_VERSION >= 8) {
143-
Assert::methodExists($refMethod, 'getAttributes');
144-
$attribute = $refMethod->getAttributes($annotationClass)[0] ?? null;
145-
if ($attribute) {
146-
return $this->methodAnnotationCache[$cacheKey] = $attribute->newInstance();
147-
}
134+
$attribute = $refMethod->getAttributes($annotationClass)[0] ?? null;
135+
if ($attribute) {
136+
return $this->methodAnnotationCache[$cacheKey] = $attribute->newInstance();
148137
}
149138

150139
return $this->methodAnnotationCache[$cacheKey] = $this->reader->getMethodAnnotation($refMethod, $annotationClass);
@@ -203,21 +192,18 @@ public function getClassAnnotations(ReflectionClass $refClass, string $annotatio
203192
$toAddAnnotations[] = array_filter($allAnnotations, static function ($annotation) use ($annotationClass): bool {
204193
return $annotation instanceof $annotationClass;
205194
});
206-
if (PHP_MAJOR_VERSION >= 8) {
207-
Assert::methodExists($refClass, 'getAttributes');
208-
209-
/** @var A[] $attributes */
210-
$attributes = array_map(
211-
static function ($attribute) {
212-
return $attribute->newInstance();
213-
},
214-
array_filter($refClass->getAttributes(), static function ($annotation) use ($annotationClass): bool {
215-
return is_a($annotation->getName(), $annotationClass, true);
216-
})
217-
);
218-
219-
$toAddAnnotations[] = $attributes;
220-
}
195+
196+
/** @var A[] $attributes */
197+
$attributes = array_map(
198+
static function ($attribute) {
199+
return $attribute->newInstance();
200+
},
201+
array_filter($refClass->getAttributes(), static function ($annotation) use ($annotationClass): bool {
202+
return is_a($annotation->getName(), $annotationClass, true);
203+
})
204+
);
205+
206+
$toAddAnnotations[] = $attributes;
221207
} catch (AnnotationException $e) {
222208
if ($this->mode === self::STRICT_MODE) {
223209
throw $e;
@@ -264,7 +250,7 @@ public function getTypeAnnotation(ReflectionClass $refClass): ?TypeInterface
264250
/**
265251
* @param ReflectionClass<T> $refClass
266252
*
267-
* @return Input[]
253+
* @return array<int,Input>
268254
*
269255
* @throws AnnotationException
270256
*
@@ -273,7 +259,7 @@ public function getTypeAnnotation(ReflectionClass $refClass): ?TypeInterface
273259
public function getInputAnnotations(ReflectionClass $refClass): array
274260
{
275261
try {
276-
/** @var Input[] $inputs */
262+
/** @var array<int,Input> $inputs */
277263
$inputs = $this->getClassAnnotations($refClass, Input::class, false);
278264
foreach ($inputs as $input) {
279265
$input->setClass($refClass->getName());
@@ -358,7 +344,7 @@ public function getDecorateAnnotation(ReflectionMethod $refMethod): ?Decorate
358344
public function getParameterAnnotations(ReflectionParameter $refParameter): ParameterAnnotations
359345
{
360346
$method = $refParameter->getDeclaringFunction();
361-
Assert::isInstanceOf($method, ReflectionMethod::class);
347+
assert($method instanceof ReflectionMethod);
362348
/** @var ParameterAnnotationInterface[] $parameterAnnotations */
363349
$parameterAnnotations = $this->getMethodAnnotations($method, ParameterAnnotationInterface::class);
364350
$name = $refParameter->getName();
@@ -385,13 +371,13 @@ public function getParameterAnnotationsPerParameter(array $refParameters): array
385371
$firstParam = reset($refParameters);
386372

387373
$method = $firstParam->getDeclaringFunction();
388-
Assert::isInstanceOf($method, ReflectionMethod::class);
374+
assert($method instanceof ReflectionMethod);
389375

390376
/** @var ParameterAnnotationInterface[] $parameterAnnotations */
391377
$parameterAnnotations = $this->getMethodAnnotations($method, ParameterAnnotationInterface::class);
392378

393379
/**
394-
* @var array<string, array<int, ParameterAnnotations>>
380+
* @var array<string, array<int,ParameterAnnotations>> $parameterAnnotationsPerParameter
395381
*/
396382
$parameterAnnotationsPerParameter = [];
397383
foreach ($parameterAnnotations as $parameterAnnotation) {
@@ -404,35 +390,36 @@ public function getParameterAnnotationsPerParameter(array $refParameters): array
404390
$parametersByKey[$refParameter->getName()] = true;
405391
}
406392
$diff = array_diff_key($parameterAnnotationsPerParameter, $parametersByKey);
407-
if (! empty($diff)) {
393+
if (count($diff) > 0) {
408394
foreach ($diff as $parameterName => $parameterAnnotations) {
409395
throw InvalidParameterException::parameterNotFound($parameterName, get_class($parameterAnnotations[0]), $method);
410396
}
411397
}
412398

413-
// Now, let's add PHP 8 parameter attributes
414-
if (PHP_MAJOR_VERSION >= 8) {
415-
foreach ($refParameters as $refParameter) {
416-
Assert::methodExists($refParameter, 'getAttributes');
417-
$attributes = $refParameter->getAttributes();
418-
$parameterAnnotationsPerParameter[$refParameter->getName()] = [...$parameterAnnotationsPerParameter[$refParameter->getName()] ??
399+
foreach ($refParameters as $refParameter) {
400+
$attributes = $refParameter->getAttributes();
401+
$parameterAnnotationsPerParameter[$refParameter->getName()] = [...$parameterAnnotationsPerParameter[$refParameter->getName()] ??
419402
[],
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-
];
429-
}
403+
...array_map(
404+
static function ($attribute) {
405+
return $attribute->newInstance();
406+
},
407+
array_filter($attributes, static function ($annotation): bool {
408+
return is_a($annotation->getName(), ParameterAnnotationInterface::class, true);
409+
})
410+
),
411+
];
430412
}
431413

432-
return array_map(static function (array $parameterAnnotations) {
433-
Assert::allIsInstanceOf($parameterAnnotations, ParameterAnnotationInterface::class);
434-
return new ParameterAnnotations($parameterAnnotations);
435-
}, $parameterAnnotationsPerParameter);
414+
return array_map(
415+
static function (array $parameterAnnotations): ParameterAnnotations {
416+
/**
417+
* @var ParameterAnnotationInterface[] $parameterAnnotations
418+
*/
419+
return new ParameterAnnotations($parameterAnnotations);
420+
},
421+
$parameterAnnotationsPerParameter
422+
);
436423
}
437424

438425
/**
@@ -473,21 +460,18 @@ public function getMethodAnnotations(ReflectionMethod $refMethod, string $annota
473460
$toAddAnnotations = array_filter($allAnnotations, static function ($annotation) use ($annotationClass): bool {
474461
return $annotation instanceof $annotationClass;
475462
});
476-
if (PHP_MAJOR_VERSION >= 8) {
477-
Assert::methodExists($refMethod, 'getAttributes');
478-
$attributes = $refMethod->getAttributes();
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-
];
490-
}
463+
$attributes = $refMethod->getAttributes();
464+
$toAddAnnotations = [
465+
...$toAddAnnotations,
466+
...array_map(
467+
static function ($attribute) {
468+
return $attribute->newInstance();
469+
},
470+
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
471+
return is_a($annotation->getName(), $annotationClass, true);
472+
})
473+
),
474+
];
491475
} catch (AnnotationException $e) {
492476
if ($this->mode === self::STRICT_MODE) {
493477
throw $e;
@@ -532,21 +516,18 @@ public function getPropertyAnnotations(ReflectionProperty $refProperty, string $
532516
$toAddAnnotations = array_filter($allAnnotations, static function ($annotation) use ($annotationClass): bool {
533517
return $annotation instanceof $annotationClass;
534518
});
535-
if (PHP_MAJOR_VERSION >= 8) {
536-
Assert::methodExists($refProperty, 'getAttributes');
537-
$attributes = $refProperty->getAttributes();
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-
];
549-
}
519+
$attributes = $refProperty->getAttributes();
520+
$toAddAnnotations = [
521+
...$toAddAnnotations,
522+
...array_map(
523+
static function ($attribute) {
524+
return $attribute->newInstance();
525+
},
526+
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
527+
return is_a($annotation->getName(), $annotationClass, true);
528+
})
529+
),
530+
];
550531
} catch (AnnotationException $e) {
551532
if ($this->mode === self::STRICT_MODE) {
552533
throw $e;

src/Annotations/ParameterAnnotations.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class ParameterAnnotations
1515
{
1616
/**
17-
* @param array<int, ParameterAnnotationInterface> $annotations
17+
* @param array<int,ParameterAnnotationInterface> $annotations
1818
*/
1919
public function __construct(private array $annotations)
2020
{
@@ -25,7 +25,7 @@ public function __construct(private array $annotations)
2525
*
2626
* @param class-string<T> $className
2727
*
28-
* @return array<int, T>
28+
* @return array<int,T>
2929
*
3030
* @template T of ParameterAnnotationInterface
3131
*/
@@ -61,7 +61,7 @@ public function getAnnotationByType(string $className): ?ParameterAnnotationInte
6161
}
6262

6363
/**
64-
* @return array<int, ParameterAnnotationInterface>
64+
* @return array<int,ParameterAnnotationInterface>
6565
*/
6666
public function getAllAnnotations(): array
6767
{

src/Annotations/UseInputType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(array|string $inputType = [])
4040
$values = ['inputType' => $values];
4141
}
4242
if (! isset($values['inputType'])) {
43-
throw new BadMethodCallException('The @UseInputType annotation must be passed an input type. For instance: "@UseInputType(for="$input", inputType="MyInputType")" in PHP 7+ or #[UseInputType("MyInputType")] in PHP 8+');
43+
throw new BadMethodCallException('The @UseInputType annotation must be passed an input type. For instance: #[UseInputType("MyInputType")]');
4444
}
4545
$this->inputType = $values['inputType'];
4646
if (! isset($values['for'])) {
@@ -53,7 +53,7 @@ public function __construct(array|string $inputType = [])
5353
public function getTarget(): string
5454
{
5555
if ($this->for === null) {
56-
throw new BadMethodCallException('The @UseInputType annotation must be passed a target and an input type. For instance: "@UseInputType(for="$input", inputType="MyInputType")" in PHP 7+ or #[UseInputType("MyInputType")] in PHP 8+');
56+
throw new BadMethodCallException('The @UseInputType annotation must be passed a target and an input type. For instance: #[UseInputType("MyInputType")]');
5757
}
5858
return $this->for;
5959
}

src/Containers/BasicAutoWiringContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
class BasicAutoWiringContainer implements ContainerInterface
1919
{
20-
/** @var object[] */
20+
/** @var array<string|int,object> */
2121
private array $values = [];
2222

2323
/**

0 commit comments

Comments
 (0)