Skip to content

Commit

Permalink
[Privatization] Skip Property unsetted on ChangeReadOnlyPropertyWithD…
Browse files Browse the repository at this point in the history
…efaultValueToConstantRector (#2981)
  • Loading branch information
samsonasik committed Oct 9, 2022
1 parent 8392cf7 commit 68a3490
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public function isRead(PropertyFetch | StaticPropertyFetch $node): bool
return false;
}

return ! $this->isArrayDimFetchInImpureFunction($parentNode, $node);
if (! $this->isArrayDimFetchInImpureFunction($parentNode, $node)) {
return $this->isNotInsideIssetUnset($parentNode);
}

return false;
}

private function isArrayDimFetchInImpureFunction(ArrayDimFetch $arrayDimFetch, Node $node): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector\Fixture;

final class SkipUnsettedParentArrayDimFetch {
private static $array = ['foo' => 'bar'];

public function run() {
unset(self::$array['foo']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector\Fixture;

final class SkipUnsettedPropertyFetch {
private $array = ['foo' => 'bar'];

public function run() {
unset($this->array);
}
}
2 changes: 1 addition & 1 deletion src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private function isChangeableContext(PropertyFetch | StaticPropertyFetch $proper
return ! $this->readWritePropertyAnalyzer->isRead($propertyFetch);
}

return false;
return $parentNode instanceof Unset_;
}

private function isFoundByRefParam(MethodCall | StaticCall $node): bool
Expand Down

0 comments on commit 68a3490

Please sign in to comment.