Skip to content

Commit

Permalink
[Downgrade] [PHP 8.0] Fix static type (#4800)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 6, 2020
1 parent dc61d2f commit 3b22532
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
7 changes: 7 additions & 0 deletions packages/static-type-mapper/src/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@

interface DowngradeReturnDeclarationRectorInterface
{
/**
* Indicate if the return declaration must be removed
*/
public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function refactor(Node $node): ?Node
return null;
}

$this->addDocBlockReturn($node);
$this->decorateFunctionLikeWithReturnTagValueNode($node);

$node->returnType = null;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ protected function getRectorClass(): string

protected function getPhpVersion(): int
{
return PhpVersionFeature::MIXED_TYPE - 1;
return PhpVersionFeature::STATIC_RETURN_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Rector\DowngradePhp80\Tests\Rector\FunctionLike\DowngradeReturnStaticT
class SomeClass
{
/**
* @return static
* @return $this
*/
public function getAnything()
{
Expand Down

0 comments on commit 3b22532

Please sign in to comment.