[NodeNameResolver] Remove NodeNameResolver::isLocalPropertyFetchNamed() method, already handled at PropertyFetchAnalyzer::isLocalPropertyFetchName() (#2263)

* [NodeNameResolver] Remove NodeNameResolver::isLocalPropertyFetchNamed() method, already handled at PropertyFetchAnalyzer::isLocalPropertyFetchName()

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* phpstan

* final touch: comment

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-05-08 15:43:06 +07:00 committed by GitHub
parent d89c23267c
commit e341cc26fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 39 deletions

View File

@ -7,7 +7,6 @@ namespace Rector\NodeNameResolver;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
@ -165,27 +164,6 @@ final class NodeNameResolver
return $names;
}
public function isLocalPropertyFetchNamed(Node $node, string $name): bool
{
if (! $node instanceof PropertyFetch) {
return false;
}
if ($node->var instanceof MethodCall) {
return false;
}
if (! $this->isName($node->var, 'this')) {
return false;
}
if ($node->name instanceof Expr) {
return false;
}
return $this->isName($node->name, $name);
}
/**
* Ends with ucname
* Starts with adjective, e.g. (Post $firstPost, Post $secondPost)

View File

@ -10,14 +10,14 @@ use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\VarLikeIdentifier;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
final class PropertyFetchRenamer
{
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly NodeNameResolver $nodeNameResolver
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}
@ -27,23 +27,15 @@ final class PropertyFetchRenamer
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$classLike,
function (Node $node) use ($currentName, $expectedName): ?Node {
if ($node instanceof PropertyFetch && $this->nodeNameResolver->isLocalPropertyFetchNamed(
$node,
$currentName
)) {
$node->name = new Identifier($expectedName);
return $node;
}
if (! $node instanceof StaticPropertyFetch) {
if (! $this->propertyFetchAnalyzer->isLocalPropertyFetchName($node, $currentName)) {
return null;
}
if (! $this->nodeNameResolver->isName($node->name, $currentName)) {
return null;
}
/** @var StaticPropertyFetch|PropertyFetch $node */
$node->name = $node instanceof PropertyFetch
? new Identifier($expectedName)
: new VarLikeIdentifier($expectedName);
$node->name = new VarLikeIdentifier($expectedName);
return $node;
}
);

View File

@ -6,6 +6,7 @@ namespace Rector\Php80\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
@ -16,6 +17,7 @@ use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\ValueObject\MethodName;
@ -35,7 +37,8 @@ final class PromotedPropertyCandidateResolver
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly TypeComparator $typeComparator,
private readonly TypeFactory $typeFactory
private readonly TypeFactory $typeFactory,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}
@ -87,7 +90,13 @@ final class PromotedPropertyCandidateResolver
}
$assign = $stmt;
if (! $this->nodeNameResolver->isLocalPropertyFetchNamed($assign->var, $propertyName)) {
// promoted property must use non-static property only
if (! $assign->var instanceof PropertyFetch) {
continue;
}
if (! $this->propertyFetchAnalyzer->isLocalPropertyFetchName($assign->var, $propertyName)) {
continue;
}