Skip to content

Commit

Permalink
Merge pull request #95 from ihor-sviziev/incorrect-argument-type
Browse files Browse the repository at this point in the history
Fix incorrect argument type when `@param` is not declared for all parameters of a function
  • Loading branch information
Ocramius authored Sep 21, 2021
2 parents 54251ab + 0ea3012 commit bb32485
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Reflection/ParameterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laminas\Code\Reflection;

use Laminas\Code\Reflection\DocBlock\Tag\ParamTag;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions test/Reflection/ParameterReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public function testTypeReturn($param, $type)
self::assertEquals($type, $parameter->detectType());
}

/**
* This test covers type detection when not all params declared in phpDoc block
*
* @dataProvider paramTypeWithNotAllParamsDeclared
*/
public function testTypeReturnWithNotAllParamsDeclared(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(
Expand All @@ -82,6 +96,21 @@ public function paramType(): array
];
}

/**
* @return string[][]
* @psalm-return non-empty-list<array{non-empty-string, non-empty-string}>
*/
public function paramTypeWithNotAllParamsDeclared(): array
{
return [
['one', 'string'],
['two', 'string'],
['three', 'int'],
['four', 'string'],
['five', 'string'],
];
}

/**
* @group zendframework/zend-code#29
* @dataProvider reflectionHints
Expand Down
17 changes: 17 additions & 0 deletions test/Reflection/TestAsset/TestSampleClass5.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {

}
}

0 comments on commit bb32485

Please sign in to comment.