Skip to content

Commit

Permalink
BUGFIX: Fix method param type expansion for partial annotation coverage
Browse files Browse the repository at this point in the history
Adjusts the behavior of `ReflectionService::getMethodParameters()` such that `@param` annotations are mapped to the corresponding method argument based in their _name_ instead of the _index_.

Fixes: #3423
  • Loading branch information
bwaidelich committed Dec 19, 2024
1 parent 0236a11 commit fd1ddfb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Neos.Flow/Classes/Reflection/ReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1930,10 +1930,11 @@ static function (\ReflectionNamedType $type) {
$parameterInformation[self::DATA_PARAMETER_DEFAULT_VALUE] = $parameter->getDefaultValue();
}
$paramAnnotations = $method->isTaggedWith('param') ? $method->getTagValues('param') : [];
if (isset($paramAnnotations[$parameter->getPosition()])) {
$explodedParameters = explode(' ', $paramAnnotations[$parameter->getPosition()]);
if (count($explodedParameters) >= 2) {
foreach ($paramAnnotations as $paramAnnotation) {
$explodedParameters = explode(' ', $paramAnnotation);
if (count($explodedParameters) >= 2 && $explodedParameters[1] === '$' . $parameter->getName()) {
$parameterType = $this->expandType($method->getDeclaringClass(), $explodedParameters[0]);
break;
}
}
if (!isset($parameterInformation[self::DATA_PARAMETER_TYPE]) && $parameterType !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ public function nullableClassName(SubEntity $parameter)
public function simpleType($parameter)
{
}

/**
* @param array<SubSubEntity> $param2 some description
*/
public function multipleParamsWithPartialAnnotationCoverage(SubEntity $param1, array $param2, SubSubSubEntity|null $param3 = null): void
{
}
}
48 changes: 48 additions & 0 deletions Neos.Flow/Tests/Functional/Reflection/ReflectionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/

use Neos\Flow\Reflection\ReflectionService;
use Neos\Flow\Tests\Functional\Reflection\Fixtures\Model\SubEntity;
use Neos\Flow\Tests\Functional\Reflection\Fixtures\Model\SubSubEntity;
use Neos\Flow\Tests\Functional\Reflection\Fixtures\Model\SubSubSubEntity;
use Neos\Flow\Tests\FunctionalTestCase;
use Neos\Flow\Tests\Functional\Reflection;
use Neos\Flow\Tests\Functional\Persistence;
Expand Down Expand Up @@ -197,6 +200,51 @@ public function methodParameterTypeExpansionWorksWithNullable()
self::assertSame($expectedType, $actualType);
}

/**
* @test
* @see https://github.com/neos/flow-development-collection/issues/3423
*/
public function methodParameterTypeExpansionWorksWithParamsWithPartialAnnotationCoverage()
{
$methodParameters = $this->reflectionService->getMethodParameters(Reflection\Fixtures\Model\EntityWithUseStatements::class, 'multipleParamsWithPartialAnnotationCoverage');
$expectedResult = [
'param1' => [
'position' => 0,
'optional' => false,
'type' => SubEntity::class,
'class' => SubEntity::class,
'array' => false,
'byReference' => false,
'allowsNull' => false,
'defaultValue' => null,
'scalarDeclaration' => false,
],
'param2' => [
'position' => 1,
'optional' => false,
'type' => 'array<' . SubSubEntity::class . '>',
'class' => null,
'array' => true,
'byReference' => false,
'allowsNull' => false,
'defaultValue' => NULL,
'scalarDeclaration' => false,
],
'param3' => [
'position' => 2,
'optional' => true,
'type' => SubSubSubEntity::class,
'class' => SubSubSubEntity::class,
'array' => false,
'byReference' => false,
'allowsNull' => true,
'defaultValue' => NULL,
'scalarDeclaration' => false,
],
];
self::assertSame($expectedResult, $methodParameters);
}

/**
* @test
*/
Expand Down

0 comments on commit fd1ddfb

Please sign in to comment.