-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Privatization] Add ParentPropertyLookupGuard service (#2152)
* [Privatization] Add ParentPropertyLookupGuard service * no extends is allowed * handle parent class not autoloaded * handle parent class not autoloaded
- Loading branch information
1 parent
366c994
commit c3adc6b
Showing
5 changed files
with
144 additions
and
107 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...tor/Property/PrivatizeFinalClassPropertyRector/Fixture/keep_parent_not_autoloaded.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture; | ||
|
||
final class KeepParentNotAutoloaded extends NotAutoloadedParent | ||
{ | ||
protected $value = 100; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
rules/Privatization/Guard/ParentPropertyLookupGuard.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Privatization\Guard; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticPropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\Reflection\ClassReflection; | ||
use Rector\Core\Enum\ObjectReference; | ||
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer; | ||
use Rector\Core\PhpParser\AstResolver; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Core\Reflection\ReflectionResolver; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class ParentPropertyLookupGuard | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
private readonly ReflectionResolver $reflectionResolver, | ||
private readonly NodeNameResolver $nodeNameResolver, | ||
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer, | ||
private readonly AstResolver $astResolver | ||
) { | ||
} | ||
|
||
public function isLegal(Property $property): bool | ||
{ | ||
$class = $this->betterNodeFinder->findParentType($property, Class_::class); | ||
|
||
if (! $class instanceof Class_) { | ||
return false; | ||
} | ||
|
||
if ($class->extends === null) { | ||
return true; | ||
} | ||
|
||
$classReflection = $this->reflectionResolver->resolveClassReflection($property); | ||
if (! $classReflection instanceof ClassReflection) { | ||
return false; | ||
} | ||
|
||
$propertyName = $this->nodeNameResolver->getName($property); | ||
$className = $classReflection->getName(); | ||
|
||
$parents = $classReflection->getParents(); | ||
|
||
// parent class not autoloaded | ||
if ($parents === []) { | ||
return false; | ||
} | ||
|
||
foreach ($parents as $parent) { | ||
if ($parent->hasProperty($propertyName)) { | ||
return false; | ||
} | ||
|
||
if ($this->isFoundInParentClassMethods($parent, $propertyName, $className)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function isFoundInParentClassMethods( | ||
ClassReflection $parentClassReflection, | ||
string $propertyName, | ||
string $className | ||
): bool { | ||
$classLike = $this->astResolver->resolveClassFromName($parentClassReflection->getName()); | ||
if (! $classLike instanceof Class_) { | ||
return false; | ||
} | ||
|
||
$methods = $classLike->getMethods(); | ||
foreach ($methods as $method) { | ||
$isFound = $this->isFoundInMethodStmts((array) $method->stmts, $propertyName, $className); | ||
if ($isFound) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param Stmt[] $stmts | ||
*/ | ||
private function isFoundInMethodStmts(array $stmts, string $propertyName, string $className): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findFirst($stmts, function (Node $subNode) use ( | ||
$propertyName, | ||
$className | ||
): bool { | ||
if (! $this->propertyFetchAnalyzer->isPropertyFetch($subNode)) { | ||
return false; | ||
} | ||
|
||
/** @var PropertyFetch|StaticPropertyFetch $subNode */ | ||
if ($subNode instanceof PropertyFetch) { | ||
if (! $subNode->var instanceof Variable) { | ||
return false; | ||
} | ||
|
||
if (! $this->nodeNameResolver->isName($subNode->var, 'this')) { | ||
return false; | ||
} | ||
|
||
return $this->nodeNameResolver->isName($subNode, $propertyName); | ||
} | ||
|
||
if (! $this->nodeNameResolver->isNames( | ||
$subNode->class, | ||
[ObjectReference::SELF()->getValue(), ObjectReference::STATIC()->getValue(), $className] | ||
)) { | ||
return false; | ||
} | ||
|
||
return $this->nodeNameResolver->isName($subNode->name, $propertyName); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters