Skip to content

Commit

Permalink
[EarlyReturn] Skip with AssignOp on PreparedValueToEarlyReturnRector (
Browse files Browse the repository at this point in the history
…#5857)

* [EarlyReturn] Skip with AssignOp on PreparedValueToEarlyReturnRector

* [EarlyReturn] Skip with AssignOp on PreparedValueToEarlyReturnRector
  • Loading branch information
samsonasik committed May 3, 2024
1 parent 10558a8 commit 28e84d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

final class SkipAppendVariableValueBeforeIf
{
public function getTravelString(bool $shorten, int $range, string $metric, float $distance): string
{
$label = ($shorten) ? ($metric === 'time' ? ' hrs' : ' mi') : ($metric === 'time' ? ' hours' : ' miles');
$range = ($metric === 'time') ? round($range / 60, 1) : $range;

$travels = ($shorten) ? '' : 'up to ';
$travels .= $range . $label;

if ($distance > 3000) {
$travels = ($shorten) ? 'any' : 'nationwide';
}

return $travels;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
Expand Down Expand Up @@ -101,6 +102,10 @@ public function refactor(Node $node): ?StmtsAwareInterface
$initialAssignPosition = null;

foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof Expression && $stmt->expr instanceof AssignOp) {
return null;
}

if ($stmt instanceof If_) {
$ifs[$key] = $stmt;
continue;
Expand Down

0 comments on commit 28e84d2

Please sign in to comment.