Skip to content

Commit 3bc3c84

Browse files
authored
Revert goto handling on ExplicitReturnNullRector (#7021)
* Revert "[CodeQuality] Properly handle Goto_ on ExplicitReturnNullRector take 3 (#7020)" This reverts commit 3dacd70. * Revert "[CodeQuality] Properly handle Goto_ on ExplicitReturnNullRector take 2 (#7019)" This reverts commit 0966124. * Revert "[CodeQuality] Properly handle Goto_ on ExplicitReturnNullRector (#7018)" This reverts commit c671491.
1 parent 3dacd70 commit 3bc3c84

8 files changed

Lines changed: 32 additions & 184 deletions

File tree

rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/break_on_switch.php.inc

Lines changed: 0 additions & 42 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned4.php.inc

Lines changed: 0 additions & 44 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/maybe_not_returned_before_label.php.inc

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;
4+
5+
final class SkipDoWhileMaybeReturned4
6+
{
7+
public function run(int $i)
8+
{
9+
do {
10+
if (rand(0, 1)) {
11+
goto execute;
12+
}
13+
14+
return 2;
15+
} while (++$i < 1);
16+
execute:
17+
echo 'test';
18+
}
19+
}

rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip_exit_too_early_before_label.php.inc

Lines changed: 0 additions & 18 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip_return_too_early_before_label.php.inc

Lines changed: 0 additions & 18 deletions
This file was deleted.

rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
use PhpParser\Node\Stmt\Class_;
1313
use PhpParser\Node\Stmt\ClassMethod;
1414
use PhpParser\Node\Stmt\Function_;
15+
use PhpParser\Node\Stmt\Goto_;
1516
use PhpParser\Node\Stmt\Return_;
1617
use PhpParser\NodeVisitor;
1718
use PHPStan\Type\NullType;
1819
use PHPStan\Type\UnionType;
1920
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2021
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
2122
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
23+
use Rector\PhpParser\Node\BetterNodeFinder;
2224
use Rector\Rector\AbstractRector;
2325
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
2426
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
@@ -37,6 +39,7 @@ public function __construct(
3739
private readonly TypeFactory $typeFactory,
3840
private readonly PhpDocTypeChanger $phpDocTypeChanger,
3941
private readonly ReturnTypeInferer $returnTypeInferer,
42+
private readonly BetterNodeFinder $betterNodeFinder,
4043
) {
4144
}
4245

@@ -111,6 +114,15 @@ public function refactor(Node $node): ?Node
111114
return null;
112115
}
113116

117+
$hasGoto = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped(
118+
$node,
119+
fn (Node $node): bool => $node instanceof Goto_
120+
);
121+
122+
if ($hasGoto) {
123+
return null;
124+
}
125+
114126
$hasChanged = false;
115127
$this->traverseNodesWithCallable((array) $node->stmts, static function (Node $node) use (
116128
&$hasChanged

rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use PhpParser\Node\Stmt\Function_;
2727
use PhpParser\Node\Stmt\Goto_;
2828
use PhpParser\Node\Stmt\If_;
29-
use PhpParser\Node\Stmt\Label;
3029
use PhpParser\Node\Stmt\Return_;
3130
use PhpParser\Node\Stmt\Switch_;
3231
use PhpParser\Node\Stmt\TryCatch;
@@ -84,17 +83,7 @@ public function hasSilentVoid(FunctionLike $functionLike): bool
8483
*/
8584
private function hasStmtsAlwaysReturnOrExit(array $stmts): bool
8685
{
87-
// early label check position key stmt
88-
// as label can be defined later after goto
89-
$hasAlwaysReturnOrExitAfterLabelPosition = null;
90-
foreach ($stmts as $key => $stmt) {
91-
if ($stmt instanceof Label && isset($stmts[$key + 1]) && $this->hasStmtsAlwaysReturnOrExit(array_slice($stmts, $key + 1))) {
92-
$hasAlwaysReturnOrExitAfterLabelPosition = $key;
93-
break;
94-
}
95-
}
96-
97-
foreach ($stmts as $key => $stmt) {
86+
foreach ($stmts as $stmt) {
9887
if ($this->neverFuncCallAnalyzer->isWithNeverTypeExpr($stmt)) {
9988
return true;
10089
}
@@ -103,10 +92,6 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool
10392
return true;
10493
}
10594

106-
if ($stmt instanceof Goto_ && $hasAlwaysReturnOrExitAfterLabelPosition < $key) {
107-
return true;
108-
}
109-
11095
// has switch with always return
11196
if ($stmt instanceof Switch_ && $this->isSwitchWithAlwaysReturnOrExit($stmt)) {
11297
return true;

0 commit comments

Comments
 (0)