[DeadCode] Skip has return reassign Coalesce Op on RemoveUnusedPrivatePropertyRector (#2477)

* [DeadCode] Skip has return reassign Coalesce Op on RemoveUnusedPrivatePropertyRector

* Fixed 🎉
This commit is contained in:
Abdul Malik Ikhsan 2022-06-11 20:58:17 +07:00 committed by GitHub
parent ce1c29184d
commit e1188e9bff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;
use stdClass;
final class SkipHasReturnReassignWhenEmpty
{
private array $data = [];
public function init(string $key, stdClass $stdClass): void
{
$this->data[$key] = $stdClass;
}
public function execute(string $key)
{
return $this->data[$key] ??= new stdClass();
}
}

View File

@ -46,11 +46,13 @@ final class ComplexNodeRemover
): bool {
$propertyName = $this->nodeNameResolver->getName($property);
$totalPropertyFetch = $this->propertyFetchAnalyzer->countLocalPropertyFetchName($class, $propertyName);
$expressions = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use (
$removeAssignSideEffect,
$propertyName,
&$totalPropertyFetch
&$totalPropertyFetch,
&$expressions
): ?Node {
// here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc.
if (! $node instanceof Expression) {
@ -93,8 +95,7 @@ final class ComplexNodeRemover
}
if ($totalPropertyFetch < $currentTotalPropertyFetch) {
$this->nodeRemover->removeNode($node);
return $node;
$expressions[] = $node;
}
return null;
@ -107,6 +108,7 @@ final class ComplexNodeRemover
$this->removeConstructorDependency($class, $propertyName);
$this->nodeRemover->removeNodes($expressions);
$this->nodeRemover->removeNode($property);
return true;