Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EarlyReturn] Skip append variable in PreparedValueToEarlyReturnRector (#8390) #5545

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector\Fixture;

final class SkipAppendVariableValueInIf
{
function run(): string
{
$var = 'a';
if (rand(0, 1)) {
$var = 'b' ;
}
if (rand(0, 2)) {
$var .= 'c' ;
}
return $var;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand All @@ -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
);
Expand All @@ -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
*/
Expand Down
Loading