Skip to content

Commit

Permalink
Allow resolving names on dynamic property fetch (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Oct 13, 2021
1 parent ba130af commit 78c6226
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -34,11 +33,6 @@ public function resolve(Node $node): ?string
return null;
}

// skip $some->$dynamicPropertyName
if ($parentNode instanceof PropertyFetch && $node === $parentNode->name) {
return null;
}

if ($node->name instanceof Expr) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

use Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Source\EliteManager;

class DynamicProperty
{
public function __construct(EliteManager $nonSense)
{
try {
$doesNotMatter = $this->$nonSense;
} catch (\Throwable $e) {}
}
}

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

use Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Source\EliteManager;

class DynamicProperty
{
public function __construct(EliteManager $eliteManager)
{
try {
$doesNotMatter = $this->$eliteManager;
} catch (\Throwable $e) {}
}
}

?>
5 changes: 5 additions & 0 deletions rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -106,6 +107,10 @@ private function shouldSkipPropertyFetch(PropertyFetch $propertyFetch): bool
return true;
}

if ($propertyFetch->name instanceof Variable) {
return true;
}

return $this->isPartOfClosureBindTo($propertyFetch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -85,6 +86,11 @@ public function refactor(Node $node): ?Node
continue;
}

// Ignore dynamically accessed properties ($o->$p)
if ($issetVar->name instanceof Variable) {
continue;
}

// has property PHP 7.4 type?
if ($this->hasPropertyTypeDeclaration($issetVar)) {
continue;
Expand Down

0 comments on commit 78c6226

Please sign in to comment.