Skip to content

Commit 3d5248f

Browse files
committed
use existing loop check
1 parent 750cefd commit 3d5248f

File tree

4 files changed

+6
-38
lines changed

4 files changed

+6
-38
lines changed

rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
namespace Rector\Php70\Rector\Break_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr\ArrowFunction;
9-
use PhpParser\Node\FunctionLike;
108
use PhpParser\Node\Stmt\Break_;
11-
use PhpParser\Node\Stmt\Class_;
129
use PhpParser\Node\Stmt\Return_;
13-
use PhpParser\Node\Stmt\Switch_;
1410
use PhpParser\NodeVisitor;
1511
use Rector\NodeNestingScope\ContextAnalyzer;
1612
use Rector\Rector\AbstractRector;
@@ -24,11 +20,6 @@
2420
*/
2521
final class BreakNotInLoopOrSwitchToReturnRector extends AbstractRector implements MinPhpVersionInterface
2622
{
27-
/**
28-
* @var string
29-
*/
30-
private const IS_BREAK_IN_SWITCH = 'is_break_in_switch';
31-
3223
public function __construct(
3324
private readonly ContextAnalyzer $contextAnalyzer
3425
) {
@@ -82,43 +73,19 @@ public function run()
8273
*/
8374
public function getNodeTypes(): array
8475
{
85-
return [Switch_::class, Break_::class];
76+
return [Break_::class];
8677
}
8778

8879
/**
89-
* @param Switch_|Break_ $node
80+
* @param Break_ $node
9081
* @return Return_|null|NodeVisitor::REMOVE_NODE
9182
*/
9283
public function refactor(Node $node): Return_|null|int
9384
{
94-
if ($node instanceof Switch_) {
95-
$this->traverseNodesWithCallable(
96-
$node->cases,
97-
static function (Node $subNode): ?int {
98-
if ($subNode instanceof Class_ || ($subNode instanceof FunctionLike && ! $subNode instanceof ArrowFunction)) {
99-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
100-
}
101-
102-
if (! $subNode instanceof Break_) {
103-
return null;
104-
}
105-
106-
$subNode->setAttribute(self::IS_BREAK_IN_SWITCH, true);
107-
return null;
108-
}
109-
);
110-
111-
return null;
112-
}
113-
11485
if ($this->contextAnalyzer->isInLoop($node)) {
11586
return null;
11687
}
11788

118-
if ($node->getAttribute(self::IS_BREAK_IN_SWITCH) === true) {
119-
return null;
120-
}
121-
12289
if ($this->contextAnalyzer->isInIf($node)) {
12390
return new Return_();
12491
}

src/NodeNestingScope/ContextAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ContextAnalyzer
1717
*/
1818
public function isInLoop(Node $node): bool
1919
{
20-
return $node->getAttribute(AttributeKey::IS_IN_LOOP) === true;
20+
return $node->getAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH) === true;
2121
}
2222

2323
/**

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ final class AttributeKey
172172
/**
173173
* @var string
174174
*/
175-
public const IS_IN_LOOP = 'is_in_loop';
175+
public const IS_IN_LOOP_OR_SWITCH = 'is_in_loop';
176176

177177
/**
178178
* @var string

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): v
162162
}
163163

164164
$stmts = $node instanceof Switch_ ? $node->cases : $node->stmts;
165+
165166
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
166167
$stmts,
167168
static function (Node $subNode): ?int {
@@ -170,7 +171,7 @@ static function (Node $subNode): ?int {
170171
}
171172

172173
if ($subNode instanceof If_ || $subNode instanceof Break_) {
173-
$subNode->setAttribute(AttributeKey::IS_IN_LOOP, true);
174+
$subNode->setAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH, true);
174175
}
175176

176177
return null;

0 commit comments

Comments
 (0)