From b7e98ec3b392cb7293592e9b850fd8899d48164c Mon Sep 17 00:00:00 2001 From: Pavel Kvach Date: Fri, 2 Feb 2024 23:39:42 +0200 Subject: [PATCH] [EarlyReturn] Skip append variable in PreparedValueToEarlyReturnRector (#8390) --- .../skip_append_variable_value_in_if.php.inc | 20 +++++++++ .../PreparedValueToEarlyReturnRector.php | 44 +++++++++++++++---- 2 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 rules-tests/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector/Fixture/skip_append_variable_value_in_if.php.inc diff --git a/rules-tests/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector/Fixture/skip_append_variable_value_in_if.php.inc b/rules-tests/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector/Fixture/skip_append_variable_value_in_if.php.inc new file mode 100644 index 00000000000..c3d7bdef1bd --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector/Fixture/skip_append_variable_value_in_if.php.inc @@ -0,0 +1,20 @@ + diff --git a/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php b/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php index ec496fe3a98..fb7cf613428 100644 --- a/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php @@ -94,23 +94,23 @@ public function refactor(Node $node): ?StmtsAwareInterface return null; } - /** @var BareSingleAssignIf[] $bareSingleAssignIfs */ - $bareSingleAssignIfs = []; + /** @var If_[] $ifs */ + $ifs = []; $initialAssign = null; $initialAssignPosition = null; foreach ($node->stmts as $key => $stmt) { - $bareSingleAssignIf = $this->matchBareSingleAssignIf($stmt, $key, $node); - - if ($bareSingleAssignIf instanceof BareSingleAssignIf) { - $bareSingleAssignIfs[] = $bareSingleAssignIf; + if ($stmt instanceof If_) { + $ifs[$key] = $stmt; continue; } if ($stmt instanceof Expression && $stmt->expr instanceof Assign) { $initialAssign = $stmt->expr; $initialAssignPosition = $key; + $ifs = []; + continue; } if (! $stmt instanceof Return_) { @@ -132,18 +132,24 @@ public function refactor(Node $node): ?StmtsAwareInterface return null; } - if ($bareSingleAssignIfs === []) { + $matchingBareSingleAssignIfs = $this->getMatchingBareSingleAssignIfs($ifs, $node); + + if ($matchingBareSingleAssignIfs === []) { return null; } - if (! $this->isVariableSharedInAssignIfsAndReturn($bareSingleAssignIfs, $return->expr, $initialAssign)) { + if (! $this->isVariableSharedInAssignIfsAndReturn( + $matchingBareSingleAssignIfs, + $return->expr, + $initialAssign + )) { return null; } return $this->refactorToDirectReturns( $node, $initialAssignPosition, - $bareSingleAssignIfs, + $matchingBareSingleAssignIfs, $initialAssign, $return ); @@ -152,6 +158,26 @@ public function refactor(Node $node): ?StmtsAwareInterface return null; } + /** + * @param If_[] $ifs + * @return BareSingleAssignIf[] + */ + private function getMatchingBareSingleAssignIfs(array $ifs, StmtsAwareInterface $stmtsAware): array + { + $bareSingleAssignIfs = []; + foreach ($ifs as $key => $if) { + $bareSingleAssignIf = $this->matchBareSingleAssignIf($if, $key, $stmtsAware); + + if (! $bareSingleAssignIf instanceof BareSingleAssignIf) { + return []; + } + + $bareSingleAssignIfs[] = $bareSingleAssignIf; + } + + return $bareSingleAssignIfs; + } + /** * @param BareSingleAssignIf[] $bareSingleAssignIfs */