Skip to content

Commit

Permalink
[NodeManipulator] Remove parent lookup on PropertyManipulator (#4274)
Browse files Browse the repository at this point in the history
* [NodeManipulator] Remove parent lookup on PropertyManipulator

* [NodeManipulator] Remove parent lookup on PropertyManipulator

* [NodeManipulator] Remove parent lookup on PropertyManipulator
  • Loading branch information
samsonasik committed Jun 18, 2023
1 parent 3806bb8 commit 18436f1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function isPropertyChangeableExceptConstructor(
}

$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $propertyOrParam);
$classMethod = $class->getMethod(MethodName::CONSTRUCT);

foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch, $scope)) {
Expand All @@ -98,9 +99,8 @@ public function isPropertyChangeableExceptConstructor(

// skip for constructor? it is allowed to set value in constructor method
$propertyName = (string) $this->nodeNameResolver->getName($propertyFetch);
$classMethod = $this->betterNodeFinder->findParentType($propertyFetch, ClassMethod::class);

if ($this->isPropertyAssignedOnlyInConstructor($class, $propertyName, $classMethod)) {
if ($this->isPropertyAssignedOnlyInConstructor($class, $propertyName, $propertyFetch, $classMethod)) {
continue;
}

Expand Down Expand Up @@ -162,14 +162,20 @@ public function isUsedByTrait(ClassReflection $classReflection, string $property
private function isPropertyAssignedOnlyInConstructor(
Class_ $class,
string $propertyName,
StaticPropertyFetch|PropertyFetch $propertyFetch,
?ClassMethod $classMethod
): bool {
if (! $classMethod instanceof ClassMethod) {
return false;
}

$node = $this->betterNodeFinder->findFirst(
(array) $classMethod->stmts,
static fn (Node $subNode): bool => ($subNode instanceof PropertyFetch || $subNode instanceof StaticPropertyFetch) && $subNode === $propertyFetch
);

// there is property unset in Test class, so only check on __construct
if (! $this->nodeNameResolver->isName($classMethod->name, MethodName::CONSTRUCT)) {
if (! $node instanceof Node) {
return false;
}

Expand Down

0 comments on commit 18436f1

Please sign in to comment.