diff --git a/composer.json b/composer.json index 34055f384ea0..bb2af18b65e6 100644 --- a/composer.json +++ b/composer.json @@ -48,16 +48,16 @@ "symfony/finder": "^4.4.8|^5.1", "symfony/http-kernel": "^4.4.8|^5.1", "symfony/process": "^4.4.8|^5.1", - "symplify/astral": "^9.0.34", - "symplify/autowire-array-parameter": "^9.0.34", - "symplify/console-color-diff": "^9.0.34", - "symplify/package-builder": "^9.0.34", - "symplify/rule-doc-generator": "^9.0.34", - "symplify/set-config-resolver": "^9.0.34", - "symplify/simple-php-doc-parser": "^9.0", - "symplify/skipper": "^9.0.34", - "symplify/smart-file-system": "^9.0.34", - "symplify/symfony-php-config": "^9.0.34", + "symplify/astral": "^9.0.48", + "symplify/autowire-array-parameter": "^9.0.48", + "symplify/console-color-diff": "^9.0.48", + "symplify/package-builder": "^9.0.48", + "symplify/rule-doc-generator": "^9.0.48", + "symplify/set-config-resolver": "^9.0.48", + "symplify/simple-php-doc-parser": "^9.0.48", + "symplify/skipper": "^9.0.48", + "symplify/smart-file-system": "^9.0.48", + "symplify/symfony-php-config": "^9.0.48", "webmozart/assert": "^1.9" }, "require-dev": { @@ -72,12 +72,12 @@ "sebastian/diff": "^4.0.4", "symfony/security-core": "^5.2", "symfony/security-http": "^5.2", - "symplify/changelog-linker": "^9.0.34", - "symplify/coding-standard": "^9.0.34", - "symplify/easy-coding-standard": "^9.0.34", - "symplify/easy-testing": "^9.0.34", - "symplify/phpstan-extensions": "^9.0.34", - "symplify/phpstan-rules": "^9.0.34", + "symplify/changelog-linker": "^9.0.48", + "symplify/coding-standard": "^9.0.48", + "symplify/easy-coding-standard": "^9.0.48", + "symplify/easy-testing": "^9.0.48", + "symplify/phpstan-extensions": "^9.0.48", + "symplify/phpstan-rules": "^9.0.48", "tracy/tracy": "^2.7" }, "replace": { diff --git a/rules/generics/src/TagValueNodeFactory/MethodTagValueNodeFactory.php b/rules/generics/src/TagValueNodeFactory/MethodTagValueNodeFactory.php index 76dfc2a34bc4..0ca4a6c5d08e 100644 --- a/rules/generics/src/TagValueNodeFactory/MethodTagValueNodeFactory.php +++ b/rules/generics/src/TagValueNodeFactory/MethodTagValueNodeFactory.php @@ -7,6 +7,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueParameterNode; use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; +use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; @@ -48,30 +49,7 @@ public function createFromMethodReflectionAndReturnTagValueNode( $classReflection = $methodReflection->getDeclaringClass(); $templateTypeMap = $classReflection->getTemplateTypeMap(); - $returnTagTypeNode = $returnTagValueNode->type; - - if ($returnTagValueNode->type instanceof UnionTypeNode) { - $resolvedTypes = []; - foreach ($returnTagValueNode->type->types as $unionedTypeNode) { - if (! $unionedTypeNode instanceof IdentifierTypeNode) { - continue; - } - - $resolvedTypes[] = $this->resolveIdentifierTypeNode( - $unionedTypeNode, - $templateTypeMap, - $unionedTypeNode - ); - } - - $returnTagTypeNode = new UnionTypeNode($resolvedTypes); - } elseif ($returnTagValueNode->type instanceof IdentifierTypeNode) { - $returnTagTypeNode = $this->resolveIdentifierTypeNode( - $returnTagValueNode->type, - $templateTypeMap, - $returnTagTypeNode - ); - } + $returnTagTypeNode = $this->resolveReturnTagTypeNode($returnTagValueNode, $templateTypeMap); return new MethodTagValueNode( false, @@ -99,6 +77,22 @@ private function resolveStringParameters(array $parameterReflections): array return $stringParameters; } + private function resolveReturnTagTypeNode(ReturnTagValueNode $returnTagValueNode, TemplateTypeMap $templateTypeMap) + { + $returnTagTypeNode = $returnTagValueNode->type; + + if ($returnTagValueNode->type instanceof UnionTypeNode) { + $returnTagTypeNode = $this->resolveUnionTypeNode($returnTagValueNode->type, $templateTypeMap); + } elseif ($returnTagValueNode->type instanceof IdentifierTypeNode) { + $returnTagTypeNode = $this->resolveIdentifierTypeNode( + $returnTagValueNode->type, + $templateTypeMap, + $returnTagTypeNode + ); + } + return $returnTagTypeNode; + } + private function resolveIdentifierTypeNode( IdentifierTypeNode $identifierTypeNode, TemplateTypeMap $templateTypeMap, @@ -114,4 +108,28 @@ private function resolveIdentifierTypeNode( return $fallbackTypeNode; } + + private function resolveUnionTypeNode(UnionTypeNode $unionTypeNode, TemplateTypeMap $templateTypeMap): UnionTypeNode + { + $resolvedTypes = []; + foreach ($unionTypeNode->types as $unionedTypeNode) { + if ($unionedTypeNode instanceof ArrayTypeNode) { + $resolvedType = $this->resolveIdentifierTypeNode( + $unionedTypeNode->type, + $templateTypeMap, + $unionedTypeNode + ); + + $resolvedTypes[] = new ArrayTypeNode($resolvedType); + } elseif ($unionedTypeNode instanceof IdentifierTypeNode) { + $resolvedTypes[] = $this->resolveIdentifierTypeNode( + $unionedTypeNode, + $templateTypeMap, + $unionedTypeNode + ); + } + } + + return new UnionTypeNode($resolvedTypes); + } } diff --git a/rules/generics/tests/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc b/rules/generics/tests/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc new file mode 100644 index 000000000000..86641e0f8f55 --- /dev/null +++ b/rules/generics/tests/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc @@ -0,0 +1,38 @@ + + */ +final class ArrayLikeNullable extends AbstractGenericMaybeArrayRepository +{ +} + +?> +----- + + * @method \Rector\Generics\Tests\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject[]|null findAllMaybe(string $some, int $type, $unknown) + */ +final class ArrayLikeNullable extends AbstractGenericMaybeArrayRepository +{ +} + +?> diff --git a/rules/generics/tests/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericMaybeArrayRepository.php b/rules/generics/tests/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericMaybeArrayRepository.php new file mode 100644 index 000000000000..401c5e60cf0f --- /dev/null +++ b/rules/generics/tests/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericMaybeArrayRepository.php @@ -0,0 +1,18 @@ +