[Php81] Skip has unset property fetch on ReadOnlyPropertyRector (#1953)

Co-authored-by: Bertrand <bertrand.jamin@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-03-22 03:06:38 +07:00 committed by GitHub
parent a0178a72d4
commit e7c028396b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;
final class SkipUnsetPropertyFetch
{
public function __construct(private array $data) {}
public function run()
{
unset($this->data['item']);
}
}
?>

View File

@ -25,6 +25,7 @@ use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Unset_;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@ -167,16 +168,18 @@ final class PropertyManipulator
// skip for constructor? it is allowed to set value in constructor method
$classMethod = $this->betterNodeFinder->findParentType($propertyFetch, ClassMethod::class);
if ($classMethod instanceof ClassMethod && $this->isInlineStmtWithConstructMethod(
$propertyFetch,
$classMethod
)) {
if ($this->isInlineStmtWithConstructMethod($propertyFetch, $classMethod)) {
continue;
}
if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
return true;
}
$isInUnset = (bool) $this->betterNodeFinder->findParentType($propertyFetch, Unset_::class);
if ($isInUnset) {
return true;
}
}
return false;
@ -225,8 +228,12 @@ final class PropertyManipulator
private function isInlineStmtWithConstructMethod(
PropertyFetch|StaticPropertyFetch $propertyFetch,
ClassMethod $classMethod
?ClassMethod $classMethod
): bool {
if (! $classMethod instanceof ClassMethod) {
return false;
}
if (! $this->nodeNameResolver->isName($classMethod->name, MethodName::CONSTRUCT)) {
return false;
}