diff --git a/rules-tests/CodeQuality/Rector/Return_/SimplifyUselessVariableRector/Fixture/skip_variable_used_with_date_time_immutable.php.inc b/rules-tests/CodeQuality/Rector/Return_/SimplifyUselessVariableRector/Fixture/skip_variable_used_with_date_time_immutable.php.inc new file mode 100644 index 00000000000..d4c9b4e27d1 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Return_/SimplifyUselessVariableRector/Fixture/skip_variable_used_with_date_time_immutable.php.inc @@ -0,0 +1,16 @@ +setTime(14, 0, 0); + + return $dateTime; + } +} diff --git a/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php b/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php index 90959e2a175..6bef8c67ec6 100644 --- a/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php @@ -203,7 +203,7 @@ private function shouldSkipExactlyReturnDateTime(ClassMethod $classMethod): bool return false; } - if (! $this->callAnalyzer->isNewInstance($this->betterNodeFinder, $return->expr)) { + if (! $this->callAnalyzer->isNewInstance($return->expr)) { return false; } diff --git a/rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php b/rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php index b922c615db8..fe903a6718e 100644 --- a/rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php +++ b/rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PHPStan\Type\MixedType; +use Rector\Core\NodeAnalyzer\CallAnalyzer; use Rector\Core\NodeAnalyzer\VariableAnalyzer; use Rector\Core\PhpParser\Node\AssignAndBinaryMap; use Rector\Core\Rector\AbstractRector; @@ -28,7 +29,8 @@ final class SimplifyUselessVariableRector extends AbstractRector { public function __construct( private AssignAndBinaryMap $assignAndBinaryMap, - private VariableAnalyzer $variableAnalyzer + private VariableAnalyzer $variableAnalyzer, + private CallAnalyzer $callAnalyzer ) { } @@ -69,13 +71,10 @@ public function refactor(Node $node): ?Node return null; } - $previousNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE); - if (! $previousNode instanceof Expression) { - return null; - } - - /** @var AssignOp|Assign $previousNode */ - $previousNode = $previousNode->expr; + /** @var Expression $previousExpression */ + $previousExpression = $node->getAttribute(AttributeKey::PREVIOUS_NODE); + /** @var Assign|AssignOp $previousNode */ + $previousNode = $previousExpression->expr; $previousVariableNode = $previousNode->var; if ($this->hasSomeComment($previousVariableNode)) { @@ -131,10 +130,6 @@ private function shouldSkip(Return_ $return): bool $variableNode = $return->expr; $previousExpression = $return->getAttribute(AttributeKey::PREVIOUS_NODE); - if (! $previousExpression instanceof Node) { - return true; - } - if (! $previousExpression instanceof Expression) { return true; } @@ -154,7 +149,11 @@ private function shouldSkip(Return_ $return): bool return true; } - return $this->variableAnalyzer->isStaticOrGlobal($variableNode); + if ($this->variableAnalyzer->isStaticOrGlobal($variableNode)) { + return true; + } + + return $this->callAnalyzer->isNewInstance($previousNode->var); } private function hasSomeComment(Expr $expr): bool diff --git a/src/NodeAnalyzer/CallAnalyzer.php b/src/NodeAnalyzer/CallAnalyzer.php index bc2c2d45ba7..883b04d42b6 100644 --- a/src/NodeAnalyzer/CallAnalyzer.php +++ b/src/NodeAnalyzer/CallAnalyzer.php @@ -17,6 +17,7 @@ use PhpParser\Node\Stmt\If_; use Rector\Core\PhpParser\Comparing\NodeComparator; use Rector\Core\PhpParser\Node\BetterNodeFinder; +use Symfony\Contracts\Service\Attribute\Required; final class CallAnalyzer { @@ -25,11 +26,19 @@ final class CallAnalyzer */ private const OBJECT_CALL_TYPES = [MethodCall::class, NullsafeMethodCall::class, StaticCall::class]; + private BetterNodeFinder $betterNodeFinder; + public function __construct( private NodeComparator $nodeComparator ) { } + #[Required] + public function autowireCallAnalyzer(BetterNodeFinder $betterNodeFinder): void + { + $this->betterNodeFinder = $betterNodeFinder; + } + public function isObjectCall(Expr $expr): bool { if ($expr instanceof BooleanNot) { @@ -66,16 +75,13 @@ public function doesIfHasObjectCall(array $ifs): bool return false; } - /** - * Inject BetterNodeFinder due Circular reference - */ - public function isNewInstance(BetterNodeFinder $betterNodeFinder, Expr $expr): bool + public function isNewInstance(Expr $expr): bool { if ($expr instanceof Clone_ || $expr instanceof New_) { return true; } - return (bool) $betterNodeFinder->findFirstPreviousOfNode($expr, function (Node $node) use ($expr): bool { + return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($expr, function (Node $node) use ($expr): bool { if (! $node instanceof Assign) { return false; }