Skip to content

Commit 3b46948

Browse files
authored
Replace custom attributes with existing ones (#7732)
* use existing attribute * use existing loop check
1 parent 6efa094 commit 3b46948

File tree

5 files changed

+11
-54
lines changed

5 files changed

+11
-54
lines changed

rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Expr;
1010
use PhpParser\Node\Expr\ArrowFunction;
11-
use PhpParser\Node\Expr\Assign;
1211
use PhpParser\Node\Expr\CallLike;
1312
use PhpParser\Node\Expr\Closure;
1413
use PhpParser\Node\Expr\FuncCall;
@@ -26,6 +25,7 @@
2625
use PHPStan\Reflection\Native\NativeFunctionReflection;
2726
use PHPStan\Reflection\ParametersAcceptorSelector;
2827
use PHPStan\Type\CallableType;
28+
use Rector\NodeTypeResolver\Node\AttributeKey;
2929
use Rector\PhpParser\AstResolver;
3030
use Rector\PHPStan\ScopeFetcher;
3131
use Rector\Rector\AbstractRector;
@@ -45,11 +45,6 @@ final class FunctionLikeToFirstClassCallableRector extends AbstractRector implem
4545
*/
4646
private const HAS_CALLBACK_SIGNATURE_MULTI_PARAMS = 'has_callback_signature_multi_params';
4747

48-
/**
49-
* @var string
50-
*/
51-
private const IS_IN_ASSIGN = 'is_in_assign';
52-
5348
public function __construct(
5449
private readonly AstResolver $astResolver,
5550
private readonly ReflectionResolver $reflectionResolver
@@ -77,22 +72,14 @@ function ($parameter) {
7772

7873
public function getNodeTypes(): array
7974
{
80-
return [Assign::class, CallLike::class, ArrowFunction::class, Closure::class];
75+
return [CallLike::class, ArrowFunction::class, Closure::class];
8176
}
8277

8378
/**
8479
* @param CallLike|ArrowFunction|Closure $node
8580
*/
8681
public function refactor(Node $node): null|CallLike
8782
{
88-
if ($node instanceof Assign) {
89-
if ($node->expr instanceof Closure || $node->expr instanceof ArrowFunction) {
90-
$node->expr->setAttribute(self::IS_IN_ASSIGN, true);
91-
}
92-
93-
return null;
94-
}
95-
9683
if ($node instanceof CallLike) {
9784
if ($node->isFirstClassCallable()) {
9885
return null;
@@ -186,7 +173,9 @@ private function shouldSkip(
186173
return true;
187174
}
188175

189-
if ($node->getAttribute(self::IS_IN_ASSIGN) === true) {
176+
if ($node->getAttribute(AttributeKey::IS_ASSIGNED_TO) === true || $node->getAttribute(
177+
AttributeKey::IS_BEING_ASSIGNED
178+
)) {
190179
return true;
191180
}
192181

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)