Skip to content

Commit bb32485

Browse files
authored
Merge pull request #95 from ihor-sviziev/incorrect-argument-type
Fix incorrect argument type when `@param` is not declared for all parameters of a function
2 parents 54251ab + 0ea3012 commit bb32485

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/Reflection/ParameterReflection.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laminas\Code\Reflection;
44

5+
use Laminas\Code\Reflection\DocBlock\Tag\ParamTag;
56
use ReflectionClass;
67
use ReflectionMethod;
78
use ReflectionParameter;
@@ -92,10 +93,19 @@ public function detectType()
9293
return null;
9394
}
9495

95-
$params = $docBlock->getTags('param');
96+
/** @var ParamTag[] $params */
97+
$params = $docBlock->getTags('param');
98+
$paramTag = $params[$this->getPosition()] ?? null;
99+
$variableName = '$' . $this->getName();
96100

97-
if (isset($params[$this->getPosition()])) {
98-
return $params[$this->getPosition()]->getType();
101+
if ($paramTag && ('' === $paramTag->getVariableName() || $variableName === $paramTag->getVariableName())) {
102+
return $paramTag->getTypes()[0] ?? '';
103+
}
104+
105+
foreach ($params as $param) {
106+
if ($param->getVariableName() === $variableName) {
107+
return $param->getTypes()[0] ?? '';
108+
}
99109
}
100110

101111
return null;

test/Reflection/ParameterReflectionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ public function testTypeReturn($param, $type)
5858
self::assertEquals($type, $parameter->detectType());
5959
}
6060

61+
/**
62+
* This test covers type detection when not all params declared in phpDoc block
63+
*
64+
* @dataProvider paramTypeWithNotAllParamsDeclared
65+
*/
66+
public function testTypeReturnWithNotAllParamsDeclared(string $param, string $type): void
67+
{
68+
$parameter = new Reflection\ParameterReflection(
69+
[TestAsset\TestSampleClass5::class, 'methodWithNotAllParamsDeclared'],
70+
$param
71+
);
72+
self::assertEquals($type, $parameter->detectType());
73+
}
74+
6175
public function testCallableTypeHint()
6276
{
6377
$parameter = new Reflection\ParameterReflection(
@@ -82,6 +96,21 @@ public function paramType(): array
8296
];
8397
}
8498

99+
/**
100+
* @return string[][]
101+
* @psalm-return non-empty-list<array{non-empty-string, non-empty-string}>
102+
*/
103+
public function paramTypeWithNotAllParamsDeclared(): array
104+
{
105+
return [
106+
['one', 'string'],
107+
['two', 'string'],
108+
['three', 'int'],
109+
['four', 'string'],
110+
['five', 'string'],
111+
];
112+
}
113+
85114
/**
86115
* @group zendframework/zend-code#29
87116
* @dataProvider reflectionHints

test/Reflection/TestAsset/TestSampleClass5.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,21 @@ public function doSomethingElse($one, $two = 2, $three = 'three')
5252
{
5353
return 'mixedValue';
5454
}
55+
56+
/**
57+
* @param string|array|null $two
58+
* @param int|null $three
59+
* @param string|bool|int|float|array|null $five
60+
*
61+
* @return void
62+
*/
63+
public function methodWithNotAllParamsDeclared(
64+
string $one,
65+
$two = null,
66+
int $three = null,
67+
string $four = '',
68+
$five = null
69+
) {
70+
71+
}
5572
}

0 commit comments

Comments
 (0)