From 2d5ce2209f4165dd360b63c398f807d1de322e79 Mon Sep 17 00:00:00 2001 From: Yurii Kuvshynov <141632421+fogrye@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:26:12 +0200 Subject: [PATCH] fix: code review --- src/AnnotationReader.php | 24 +++++++++++------------- src/Annotations/InjectUser.php | 5 ++--- src/FieldsBuilder.php | 8 ++++---- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/AnnotationReader.php b/src/AnnotationReader.php index 6bade37db..95efef942 100644 --- a/src/AnnotationReader.php +++ b/src/AnnotationReader.php @@ -44,10 +44,6 @@ class AnnotationReader /** @var array> */ private array $propertyAnnotationsCache = []; - public function __construct() - { - } - /** * Returns a class annotation. Does not look in the parent class. * @@ -63,13 +59,12 @@ public function __construct() private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass): object|null { $attribute = $refClass->getAttributes($annotationClass)[0] ?? null; - if ($attribute) { - $instance = $attribute->newInstance(); - assert($instance instanceof $annotationClass); - return $instance; + if (! $attribute) { + return null; } - - return null; + $instance = $attribute->newInstance(); + assert($instance instanceof $annotationClass); + return $instance; } /** @@ -85,11 +80,14 @@ private function getMethodAnnotation(ReflectionMethod $refMethod, string $annota } $attribute = $refMethod->getAttributes($annotationClass)[0] ?? null; - if ($attribute) { - return $this->methodAnnotationCache[$cacheKey] = $attribute->newInstance(); + if (! $attribute) { + $this->methodAnnotationCache[$cacheKey] = null; + + return null; } + $this->methodAnnotationCache[$cacheKey] = $attribute->newInstance(); - return $this->methodAnnotationCache[$cacheKey] = null; + return $this->methodAnnotationCache[$cacheKey]; } /** diff --git a/src/Annotations/InjectUser.php b/src/Annotations/InjectUser.php index a7e27fe91..bcdde944f 100644 --- a/src/Annotations/InjectUser.php +++ b/src/Annotations/InjectUser.php @@ -16,11 +16,10 @@ #[Attribute(Attribute::TARGET_PARAMETER)] class InjectUser implements ParameterAnnotationInterface { - /** @var string */ - private $for; + private string|null $for = null; /** @param array $values */ - public function __construct(array|null $values = []) + public function __construct(array $values = []) { if (! isset($values['for'])) { return; diff --git a/src/FieldsBuilder.php b/src/FieldsBuilder.php index dd0e58c37..de9901116 100644 --- a/src/FieldsBuilder.php +++ b/src/FieldsBuilder.php @@ -1152,10 +1152,10 @@ private function getInputFieldsByPropertyAnnotations( assert($type instanceof InputType); $forConstructorHydration = in_array($name, $constructerParameters); $resolver = $forConstructorHydration - ? new SourceConstructorParameterResolver( - $refProperty->getDeclaringClass()->getName(), - $refProperty->getName(), - ) + ? new SourceConstructorParameterResolver( + $refProperty->getDeclaringClass()->getName(), + $refProperty->getName(), + ) : new SourceInputPropertyResolver($refProperty); // setters and properties