diff --git a/packages/static-type-mapper/src/PhpParser/NameNodeMapper.php b/packages/static-type-mapper/src/PhpParser/NameNodeMapper.php index 079a84fd49ac..3fa886b5ab19 100644 --- a/packages/static-type-mapper/src/PhpParser/NameNodeMapper.php +++ b/packages/static-type-mapper/src/PhpParser/NameNodeMapper.php @@ -7,8 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Name; use PHPStan\Type\MixedType; +use PHPStan\Type\StaticType; use PHPStan\Type\Type; use Rector\NodeTypeResolver\ClassExistenceStaticHelper; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PHPStan\Type\FullyQualifiedObjectType; use Rector\PSR4\Collector\RenamedClassesCollector; use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface; @@ -41,6 +43,11 @@ public function mapToPHPStan(Node $node): Type return new FullyQualifiedObjectType($name); } + if ($name === 'static') { + $className = (string) $node->getAttribute(AttributeKey::CLASS_NAME); + return new StaticType($className); + } + return new MixedType(); } diff --git a/rules/downgrade-php71/src/Contract/Rector/DowngradeReturnDeclarationRectorInterface.php b/rules/downgrade-php71/src/Contract/Rector/DowngradeReturnDeclarationRectorInterface.php index 2a6b20e86a8b..9d3f9459f089 100644 --- a/rules/downgrade-php71/src/Contract/Rector/DowngradeReturnDeclarationRectorInterface.php +++ b/rules/downgrade-php71/src/Contract/Rector/DowngradeReturnDeclarationRectorInterface.php @@ -8,8 +8,5 @@ interface DowngradeReturnDeclarationRectorInterface { - /** - * Indicate if the return declaration must be removed - */ public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool; } diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/AbstractDowngradeReturnDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/AbstractDowngradeReturnDeclarationRector.php index cdf067271653..8d912f56199e 100644 --- a/rules/downgrade-php71/src/Rector/FunctionLike/AbstractDowngradeReturnDeclarationRector.php +++ b/rules/downgrade-php71/src/Rector/FunctionLike/AbstractDowngradeReturnDeclarationRector.php @@ -37,7 +37,7 @@ public function refactor(Node $node): ?Node return null; } - $this->addDocBlockReturn($node); + $this->decorateFunctionLikeWithReturnTagValueNode($node); $node->returnType = null; @@ -47,7 +47,7 @@ public function refactor(Node $node): ?Node /** * @param ClassMethod|Function_ $functionLike */ - private function addDocBlockReturn(FunctionLike $functionLike): void + private function decorateFunctionLikeWithReturnTagValueNode(FunctionLike $functionLike): void { /** @var PhpDocInfo|null $phpDocInfo */ $phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO); diff --git a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php index 07f71711560c..45f175cbfaa2 100644 --- a/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php +++ b/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php @@ -64,11 +64,6 @@ public function getResponseOrNothing(bool $flag) */ public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool { - if ($functionLike->returnType === null) { - return false; - } - - // Check it is the union type return $functionLike->returnType instanceof NullableType; } } diff --git a/rules/downgrade-php74/src/Rector/Property/AbstractDowngradeTypedPropertyRector.php b/rules/downgrade-php74/src/Rector/Property/AbstractDowngradeTypedPropertyRector.php index f9b0503f27ad..055c3e9f6ad6 100644 --- a/rules/downgrade-php74/src/Rector/Property/AbstractDowngradeTypedPropertyRector.php +++ b/rules/downgrade-php74/src/Rector/Property/AbstractDowngradeTypedPropertyRector.php @@ -34,24 +34,26 @@ public function refactor(Node $node): ?Node return null; } - $this->decorateWithDocBlock($node); + $this->decoratePropertyWithDocBlock($node, $node->type); $node->type = null; return $node; } - private function decorateWithDocBlock(Node $node): void + private function decoratePropertyWithDocBlock(Property $property, Node $typeNode): void { /** @var PhpDocInfo|null $phpDocInfo */ - $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); + $phpDocInfo = $property->getAttribute(AttributeKey::PHP_DOC_INFO); if ($phpDocInfo === null) { - $phpDocInfo = $this->phpDocInfoFactory->createEmpty($node); + $phpDocInfo = $this->phpDocInfoFactory->createEmpty($property); } - if ($phpDocInfo->getVarTagValueNode() === null) { - $newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->type); - $phpDocInfo->changeVarType($newType); + if ($phpDocInfo->getVarTagValueNode() !== null) { + return; } + + $newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($typeNode); + $phpDocInfo->changeVarType($newType); } } diff --git a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/DowngradeReturnStaticTypeDeclarationRectorTest.php b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/DowngradeReturnStaticTypeDeclarationRectorTest.php index 1cb6a62a8485..abe096ded513 100644 --- a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/DowngradeReturnStaticTypeDeclarationRectorTest.php +++ b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/DowngradeReturnStaticTypeDeclarationRectorTest.php @@ -33,6 +33,6 @@ protected function getRectorClass(): string protected function getPhpVersion(): int { - return PhpVersionFeature::MIXED_TYPE - 1; + return PhpVersionFeature::STATIC_RETURN_TYPE; } } diff --git a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/docblock_exists.php.inc b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/docblock_exists.php.inc index 2808bc09559e..ec2c82080c3a 100644 --- a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/docblock_exists.php.inc +++ b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/docblock_exists.php.inc @@ -25,7 +25,7 @@ namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeReturnStaticT class DocBlockExists { /** * This property is the best one - * @return static + * @return $this */ public function getAnything() { diff --git a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/fixture.php.inc b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/fixture.php.inc index c5b52fcbef11..8ecb07d01c8c 100644 --- a/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/fixture.php.inc +++ b/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture/fixture.php.inc @@ -23,7 +23,7 @@ namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeReturnStaticT class SomeClass { /** - * @return static + * @return $this */ public function getAnything() {