Skip to content

Commit 0966124

Browse files
[CodeQuality] Properly handle Goto_ on ExplicitReturnNullRector take 2 (#7019)
* [CodeQuality] More use case of Goto_ with Label_ on end statement * [CodeQuality] Properly handle Goto_ on ExplicitReturnNullRector take 2 * eol * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent c671491 commit 0966124

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;
4+
5+
final class BreakOnSwitch
6+
{
7+
public function run($var) {
8+
switch($var) {
9+
case 'test':
10+
goto result;
11+
default:
12+
return false;
13+
}
14+
15+
result:
16+
echo 'test';
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;
25+
26+
final class BreakOnSwitch
27+
{
28+
public function run($var) {
29+
switch($var) {
30+
case 'test':
31+
goto result;
32+
default:
33+
return false;
34+
}
35+
36+
result:
37+
echo 'test';
38+
return null;
39+
}
40+
}
41+
42+
?>

rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PhpParser\Node\Stmt\Function_;
2727
use PhpParser\Node\Stmt\Goto_;
2828
use PhpParser\Node\Stmt\If_;
29+
use PhpParser\Node\Stmt\Label;
2930
use PhpParser\Node\Stmt\Return_;
3031
use PhpParser\Node\Stmt\Switch_;
3132
use PhpParser\Node\Stmt\TryCatch;
@@ -83,6 +84,14 @@ public function hasSilentVoid(FunctionLike $functionLike): bool
8384
*/
8485
private function hasStmtsAlwaysReturnOrExit(array $stmts): bool
8586
{
87+
// early label check
88+
// as label can be defined later after goto
89+
foreach ($stmts as $key => $stmt) {
90+
if ($stmt instanceof Label && isset($stmts[$key + 1])) {
91+
return $this->hasStmtsAlwaysReturnOrExit(array_slice($stmts, $key + 1));
92+
}
93+
}
94+
8695
foreach ($stmts as $stmt) {
8796
if ($this->neverFuncCallAnalyzer->isWithNeverTypeExpr($stmt)) {
8897
return true;

0 commit comments

Comments
 (0)