Skip to content

Commit

Permalink
[StaticTypeMapper] Add ParentStaticType support to NameNodeMapper for…
Browse files Browse the repository at this point in the history
… "parent" return type (#765)
  • Loading branch information
samsonasik committed Aug 26, 2021
1 parent fff52c3 commit 5b5b94b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
7 changes: 6 additions & 1 deletion packages/StaticTypeMapper/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;

final class NameNodeMapper implements PhpParserNodeMapperInterface
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public function mapToPHPStan(Node $node): Type
return new FullyQualifiedObjectType($name);
}

if (in_array($name, ['static', 'self'], true)) {
if (in_array($name, ['static', 'self', 'parent'], true)) {
return $this->createClassReferenceType($node, $name);
}

Expand Down Expand Up @@ -78,6 +79,10 @@ private function createClassReferenceType(Name $name, string $reference): MixedT
return new StaticType($className);
}

if ($reference === 'parent') {
return new ParentStaticType($className);
}

if ($this->reflectionProvider->hasClass($className)) {
$classReflection = $this->reflectionProvider->getClass($className);
return new ThisType($classReflection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
namespace Rector\DowngradePhp70\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp71\TypeDeclaration\PhpDocFromTypeDeclarationDecorator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -23,7 +21,7 @@
final class DowngradeParentTypeDeclarationRector extends AbstractRector
{
public function __construct(
private PhpDocTypeChanger $phpDocTypeChanger,
private PhpDocFromTypeDeclarationDecorator $phpDocFromTypeDeclarationDecorator,
private ReflectionProvider $reflectionProvider
) {
}
Expand Down Expand Up @@ -82,37 +80,10 @@ public function foo()
*/
public function refactor(Node $node): ?Node
{
if (! $node->returnType instanceof Name) {
return null;
}

if (! $this->nodeNameResolver->isName($node->returnType, 'parent')) {
return null;
}

$node->returnType = null;

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
if ($phpDocInfo->hasByType(ReturnTagValueNode::class)) {
return $node;
}

$parentStaticType = $this->getType($node);

if (! $parentStaticType instanceof ParentStaticType) {
return $node;
}

$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $parentStaticType);
return $node;
}

private function getType(ClassMethod $classMethod): ?ParentStaticType
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope === null) {
// in a trait
$className = $classMethod->getAttribute(AttributeKey::CLASS_NAME);
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
$classReflection = $this->reflectionProvider->getClass($className);
} else {
$classReflection = $scope->getClassReflection();
Expand All @@ -122,6 +93,12 @@ private function getType(ClassMethod $classMethod): ?ParentStaticType
return null;
}

return new ParentStaticType($classReflection);
$parentStaticType = new ParentStaticType($classReflection);

if (! $this->phpDocFromTypeDeclarationDecorator->decorateReturnWithSpecificType($node, $parentStaticType)) {
return null;
}

return $node;
}
}

0 comments on commit 5b5b94b

Please sign in to comment.