[Downgrade] [PHP 8.0] Fix static type (#4800)

This commit is contained in:
Tomas Votruba 2020-12-06 01:05:21 +01:00 committed by GitHub
parent dc61d2fecf
commit 3b22532794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 20 deletions

View File

@ -7,8 +7,10 @@ namespace Rector\StaticTypeMapper\PhpParser;
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 @@ final class NameNodeMapper implements PhpParserNodeMapperInterface
return new FullyQualifiedObjectType($name);
}
if ($name === 'static') {
$className = (string) $node->getAttribute(AttributeKey::CLASS_NAME);
return new StaticType($className);
}
return new MixedType();
}

View File

@ -8,8 +8,5 @@ use PhpParser\Node\FunctionLike;
interface DowngradeReturnDeclarationRectorInterface
{
/**
* Indicate if the return declaration must be removed
*/
public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool;
}

View File

@ -37,7 +37,7 @@ abstract class AbstractDowngradeReturnDeclarationRector extends AbstractRector i
return null;
}
$this->addDocBlockReturn($node);
$this->decorateFunctionLikeWithReturnTagValueNode($node);
$node->returnType = null;
@ -47,7 +47,7 @@ abstract class AbstractDowngradeReturnDeclarationRector extends AbstractRector i
/**
* @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);

View File

@ -64,11 +64,6 @@ CODE_SAMPLE
*/
public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool
{
if ($functionLike->returnType === null) {
return false;
}
// Check it is the union type
return $functionLike->returnType instanceof NullableType;
}
}

View File

@ -34,24 +34,26 @@ abstract class AbstractDowngradeTypedPropertyRector extends AbstractRector imple
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);
}
}

View File

@ -33,6 +33,6 @@ final class DowngradeReturnStaticTypeDeclarationRectorTest extends AbstractRecto
protected function getPhpVersion(): int
{
return PhpVersionFeature::MIXED_TYPE - 1;
return PhpVersionFeature::STATIC_RETURN_TYPE;
}
}

View File

@ -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()
{

View File

@ -23,7 +23,7 @@ namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeReturnStaticT
class SomeClass
{
/**
* @return static
* @return $this
*/
public function getAnything()
{