mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-26 20:53:31 +00:00
[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:
parent
ce1c29184d
commit
e1188e9bff
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,11 +46,13 @@ final class ComplexNodeRemover
|
||||||
): bool {
|
): bool {
|
||||||
$propertyName = $this->nodeNameResolver->getName($property);
|
$propertyName = $this->nodeNameResolver->getName($property);
|
||||||
$totalPropertyFetch = $this->propertyFetchAnalyzer->countLocalPropertyFetchName($class, $propertyName);
|
$totalPropertyFetch = $this->propertyFetchAnalyzer->countLocalPropertyFetchName($class, $propertyName);
|
||||||
|
$expressions = [];
|
||||||
|
|
||||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use (
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use (
|
||||||
$removeAssignSideEffect,
|
$removeAssignSideEffect,
|
||||||
$propertyName,
|
$propertyName,
|
||||||
&$totalPropertyFetch
|
&$totalPropertyFetch,
|
||||||
|
&$expressions
|
||||||
): ?Node {
|
): ?Node {
|
||||||
// here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc.
|
// here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc.
|
||||||
if (! $node instanceof Expression) {
|
if (! $node instanceof Expression) {
|
||||||
|
@ -93,8 +95,7 @@ final class ComplexNodeRemover
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($totalPropertyFetch < $currentTotalPropertyFetch) {
|
if ($totalPropertyFetch < $currentTotalPropertyFetch) {
|
||||||
$this->nodeRemover->removeNode($node);
|
$expressions[] = $node;
|
||||||
return $node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@ -107,6 +108,7 @@ final class ComplexNodeRemover
|
||||||
|
|
||||||
$this->removeConstructorDependency($class, $propertyName);
|
$this->removeConstructorDependency($class, $propertyName);
|
||||||
|
|
||||||
|
$this->nodeRemover->removeNodes($expressions);
|
||||||
$this->nodeRemover->removeNode($property);
|
$this->nodeRemover->removeNode($property);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user