Skip to content

Commit

Permalink
[Privatization] Skip variable assign append on ChangeReadOnlyVariable…
Browse files Browse the repository at this point in the history
…WithDefaultValueToConstantRector (#3687)

* fix: checking for other modifying types

* fix: added fixture

* fix: changed class name

* fix: checking if parent class is null

* fix: checking for all types of assigning
  • Loading branch information
mickeytodd authored and samsonasik committed May 8, 2023
1 parent 1898942 commit 4a340be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector\Fixture;

final class SkipVariableArrayAppend
{
/**
* @var string[]
*/
private const VALUES = ['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e'];

public function run()
{
$arr = [];
foreach (self::VALUES as $key => $value) {
$arr[$value] ??= 0;
$arr[$value] += $key;
}
return $arr;
}
}
2 changes: 1 addition & 1 deletion src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function isLeftPartOfAssign(Node $node): bool
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
}

if ($parentNode instanceof Assign) {
if ($parentNode instanceof Assign || $parentNode instanceof AssignOp || $parentNode instanceof PreDec || $parentNode instanceof PreInc || $parentNode instanceof PostDec || $parentNode instanceof PostInc) {
return $parentNode->var === $previousParent;
}
}
Expand Down

0 comments on commit 4a340be

Please sign in to comment.