mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 02:36:52 +01:00
[Downgrade] [PHP 8.0] Fix static type (#4800)
This commit is contained in:
parent
dc61d2fecf
commit
3b22532794
@ -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();
|
||||
}
|
||||
|
||||
|
@ -8,8 +8,5 @@ use PhpParser\Node\FunctionLike;
|
||||
|
||||
interface DowngradeReturnDeclarationRectorInterface
|
||||
{
|
||||
/**
|
||||
* Indicate if the return declaration must be removed
|
||||
*/
|
||||
public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,6 @@ final class DowngradeReturnStaticTypeDeclarationRectorTest extends AbstractRecto
|
||||
|
||||
protected function getPhpVersion(): int
|
||||
{
|
||||
return PhpVersionFeature::MIXED_TYPE - 1;
|
||||
return PhpVersionFeature::STATIC_RETURN_TYPE;
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeReturnStaticT
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @return static
|
||||
* @return $this
|
||||
*/
|
||||
public function getAnything()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user