Skip to content

Commit

Permalink
Make use of key to remove property (#4032)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 30, 2023
1 parent 46d2aac commit 73c1ac4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Expand Up @@ -87,26 +87,27 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$hasChanged = false;

foreach ($node->stmts as $property) {
if (! $property instanceof Property) {
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Property) {
continue;
}

if ($this->shouldSkipProperty($property)) {
if ($this->shouldSkipProperty($stmt)) {
continue;
}

if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property, $scope)) {
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $stmt, $scope)) {
continue;
}

// use different variable to avoid re-assign back $hasRemoved to false
// when already asssigned to true
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages(
$node,
$property,
$stmt,
$this->removeAssignSideEffect,
$scope
$scope,
$key
);

if ($isRemoved) {
Expand Down
5 changes: 3 additions & 2 deletions rules/Removing/NodeManipulator/ComplexNodeRemover.php
Expand Up @@ -43,7 +43,8 @@ public function removePropertyAndUsages(
Class_ $class,
Property $property,
bool $removeAssignSideEffect,
Scope $scope
Scope $scope,
int $propertyStmtKey,
): bool {
$propertyName = $this->nodeNameResolver->getName($property);
$totalPropertyFetch = $this->propertyFetchAnalyzer->countLocalPropertyFetchName($class, $propertyName);
Expand Down Expand Up @@ -114,7 +115,7 @@ public function removePropertyAndUsages(
$this->nodeRemover->removeNode($expression);
}

$this->nodeRemover->removeNode($property);
unset($class->stmts[$propertyStmtKey]);

return true;
}
Expand Down

0 comments on commit 73c1ac4

Please sign in to comment.