Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of key to remove property #4032

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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