From cb42f25b474bc9f23a774c4519348a8659de3e47 Mon Sep 17 00:00:00 2001 From: W0rma Date: Sat, 18 Dec 2021 20:32:32 +0100 Subject: [PATCH] Make annotation reader optional --- Request/ParamReader.php | 25 ++++++++++++++++--------- Resources/config/request.xml | 2 +- Tests/Request/ParamReaderTest.php | 3 +-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Request/ParamReader.php b/Request/ParamReader.php index 378330cba..0c1062985 100644 --- a/Request/ParamReader.php +++ b/Request/ParamReader.php @@ -23,9 +23,12 @@ */ final class ParamReader implements ParamReaderInterface { + /** + * @var Reader|null + */ private $annotationReader; - public function __construct(Reader $annotationReader) + public function __construct(?Reader $annotationReader = null) { $this->annotationReader = $annotationReader; } @@ -55,10 +58,12 @@ public function getParamsFromMethod(\ReflectionMethod $method): array $annotations = $this->getParamsFromAttributes($method); } - $annotations = array_merge( - $annotations, - $this->annotationReader->getMethodAnnotations($method) ?? [] - ); + if (null !== $this->annotationReader) { + $annotations = array_merge( + $annotations, + $this->annotationReader->getMethodAnnotations($method) ?? [] + ); + } return $this->getParamsFromAnnotationArray($annotations); } @@ -73,10 +78,12 @@ public function getParamsFromClass(\ReflectionClass $class): array $annotations = $this->getParamsFromAttributes($class); } - $annotations = array_merge( - $annotations, - $this->annotationReader->getClassAnnotations($class) ?? [] - ); + if (null !== $this->annotationReader) { + $annotations = array_merge( + $annotations, + $this->annotationReader->getClassAnnotations($class) ?? [] + ); + } return $this->getParamsFromAnnotationArray($annotations); } diff --git a/Resources/config/request.xml b/Resources/config/request.xml index 627695803..d55542764 100644 --- a/Resources/config/request.xml +++ b/Resources/config/request.xml @@ -16,7 +16,7 @@ - + diff --git a/Tests/Request/ParamReaderTest.php b/Tests/Request/ParamReaderTest.php index 07d17d148..70e2408b7 100644 --- a/Tests/Request/ParamReaderTest.php +++ b/Tests/Request/ParamReaderTest.php @@ -98,8 +98,7 @@ public function testReadsOnlyParamAnnotations() */ public function testReadsAttributes() { - $annotationReader = $this->getMockBuilder(AnnotationReader::class)->getMock(); - $paramReader = new ParamReader($annotationReader); + $paramReader = new ParamReader(); $params = $paramReader->read(new \ReflectionClass(ParamsAnnotatedController::class), 'getArticlesAttributesAction'); $this->assertCount(6, $params);