Skip to content

Commit

Permalink
[Php81] Skip on Array Destructuring on ReadOnlyPropertyRector (#3030)
Browse files Browse the repository at this point in the history
Co-authored-by: Elias Häußler <elias@haeussler.dev>
  • Loading branch information
samsonasik and eliashaeussler committed Nov 2, 2022
1 parent 6902b17 commit 92a5c65
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class SkipPropetyOnArrayDestructuring
{
public function __construct(
private ?string $accessToken = null,
private ?string $owner = null,
private ?string $name = null,
) {
}

public function withVcs(string $value)
{
$clone = clone $this;
$clone->accessToken = $value;
[$clone->owner, $clone->name] = explode('/', $value, 2);

return $clone;
}
}

?>
21 changes: 21 additions & 0 deletions src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -79,6 +81,10 @@ public function isLeftPartOfAssign(Node $node): bool
return true;
}

if ($this->isOnArrayDestructuring($parentNode)) {
return true;
}

// traverse up to array dim fetches
if ($parentNode instanceof ArrayDimFetch) {
$previousParent = $parentNode;
Expand Down Expand Up @@ -128,4 +134,19 @@ public function resolveAssignsToLocalPropertyFetches(FunctionLike $functionLike)
return $this->isLeftPartOfAssign($node);
});
}

private function isOnArrayDestructuring(?Node $parentNode): bool
{
if (! $parentNode instanceof ArrayItem) {
return false;
}

$parentArrayItem = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentArrayItem instanceof Array_) {
return false;
}

$node = $parentArrayItem->getAttribute(AttributeKey::PARENT_NODE);
return $node instanceof Assign && $node->var === $parentArrayItem;
}
}

0 comments on commit 92a5c65

Please sign in to comment.