From 021be847a3acb7fbd75ead9363afe98bcbea99d9 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Thu, 29 Jul 2021 16:06:30 +0300 Subject: [PATCH 1/3] Incorrect argument type Add test coverage Signed-off-by: Ihor Sviziev --- test/Reflection/ParameterReflectionTest.php | 29 +++++++++++++++++++ .../Reflection/TestAsset/TestSampleClass5.php | 17 +++++++++++ 2 files changed, 46 insertions(+) diff --git a/test/Reflection/ParameterReflectionTest.php b/test/Reflection/ParameterReflectionTest.php index 53f066de..02d901af 100644 --- a/test/Reflection/ParameterReflectionTest.php +++ b/test/Reflection/ParameterReflectionTest.php @@ -58,6 +58,20 @@ public function testTypeReturn($param, $type) self::assertEquals($type, $parameter->detectType()); } + /** + * This test covers + * + * @dataProvider paramType2 + */ + public function testTypeReturn2(string $param, string $type): void + { + $parameter = new Reflection\ParameterReflection( + [TestAsset\TestSampleClass5::class, 'methodWithNotAllParamsDeclared'], + $param + ); + self::assertEquals($type, $parameter->detectType()); + } + public function testCallableTypeHint() { $parameter = new Reflection\ParameterReflection( @@ -82,6 +96,21 @@ public function paramType(): array ]; } + /** + * @return string[][] + * @psalm-return non-empty-list + */ + public function paramType2(): array + { + return [ + ['one', 'string'], + ['two', 'string'], + ['three', 'int'], + ['four', 'string'], + ['five', 'string'], + ]; + } + /** * @group zendframework/zend-code#29 * @dataProvider reflectionHints diff --git a/test/Reflection/TestAsset/TestSampleClass5.php b/test/Reflection/TestAsset/TestSampleClass5.php index b859af3e..abf21b02 100644 --- a/test/Reflection/TestAsset/TestSampleClass5.php +++ b/test/Reflection/TestAsset/TestSampleClass5.php @@ -52,4 +52,21 @@ public function doSomethingElse($one, $two = 2, $three = 'three') { return 'mixedValue'; } + + /** + * @param string|array|null $two + * @param int|null $three + * @param string|bool|int|float|array|null $five + * + * @return void + */ + public function methodWithNotAllParamsDeclared( + string $one, + $two = null, + int $three = null, + string $four = '', + $five = null + ) { + + } } From 913050e01f1aed5706e115a10bcc44139066a120 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Thu, 29 Jul 2021 16:06:58 +0300 Subject: [PATCH 2/3] Incorrect argument type Fix the issue Signed-off-by: Ihor Sviziev --- src/Reflection/ParameterReflection.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Reflection/ParameterReflection.php b/src/Reflection/ParameterReflection.php index 145ded27..44e9e8ad 100644 --- a/src/Reflection/ParameterReflection.php +++ b/src/Reflection/ParameterReflection.php @@ -2,6 +2,7 @@ namespace Laminas\Code\Reflection; +use Laminas\Code\Reflection\DocBlock\Tag\ParamTag; use ReflectionClass; use ReflectionMethod; use ReflectionParameter; @@ -92,10 +93,19 @@ public function detectType() return null; } - $params = $docBlock->getTags('param'); + /** @var ParamTag[] $params */ + $params = $docBlock->getTags('param'); + $paramTag = $params[$this->getPosition()] ?? null; + $variableName = '$' . $this->getName(); - if (isset($params[$this->getPosition()])) { - return $params[$this->getPosition()]->getType(); + if ($paramTag && ('' === $paramTag->getVariableName() || $variableName === $paramTag->getVariableName())) { + return $paramTag->getTypes()[0] ?? ''; + } + + foreach ($params as $param) { + if ($param->getVariableName() === $variableName) { + return $param->getTypes()[0] ?? ''; + } } return null; From 0ea30125ee4da61a6cd2aec635006b885d7b31e8 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Tue, 21 Sep 2021 15:33:59 +0200 Subject: [PATCH 3/3] Fix incorrect argument type Apply suggestions from code review Signed-off-by: Ihor Sviziev --- test/Reflection/ParameterReflectionTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Reflection/ParameterReflectionTest.php b/test/Reflection/ParameterReflectionTest.php index 02d901af..7d8c569d 100644 --- a/test/Reflection/ParameterReflectionTest.php +++ b/test/Reflection/ParameterReflectionTest.php @@ -59,11 +59,11 @@ public function testTypeReturn($param, $type) } /** - * This test covers + * This test covers type detection when not all params declared in phpDoc block * - * @dataProvider paramType2 + * @dataProvider paramTypeWithNotAllParamsDeclared */ - public function testTypeReturn2(string $param, string $type): void + public function testTypeReturnWithNotAllParamsDeclared(string $param, string $type): void { $parameter = new Reflection\ParameterReflection( [TestAsset\TestSampleClass5::class, 'methodWithNotAllParamsDeclared'], @@ -100,7 +100,7 @@ public function paramType(): array * @return string[][] * @psalm-return non-empty-list */ - public function paramType2(): array + public function paramTypeWithNotAllParamsDeclared(): array { return [ ['one', 'string'],