diff --git a/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php b/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php index 40f08d2fa32..14a77a57aee 100644 --- a/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php +++ b/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php @@ -8,7 +8,6 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; -use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\FuncCall; @@ -26,6 +25,7 @@ use PHPStan\Reflection\Native\NativeFunctionReflection; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\CallableType; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\AstResolver; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; @@ -45,11 +45,6 @@ final class FunctionLikeToFirstClassCallableRector extends AbstractRector implem */ private const HAS_CALLBACK_SIGNATURE_MULTI_PARAMS = 'has_callback_signature_multi_params'; - /** - * @var string - */ - private const IS_IN_ASSIGN = 'is_in_assign'; - public function __construct( private readonly AstResolver $astResolver, private readonly ReflectionResolver $reflectionResolver @@ -77,7 +72,7 @@ function ($parameter) { public function getNodeTypes(): array { - return [Assign::class, CallLike::class, ArrowFunction::class, Closure::class]; + return [CallLike::class, ArrowFunction::class, Closure::class]; } /** @@ -85,14 +80,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): null|CallLike { - if ($node instanceof Assign) { - if ($node->expr instanceof Closure || $node->expr instanceof ArrowFunction) { - $node->expr->setAttribute(self::IS_IN_ASSIGN, true); - } - - return null; - } - if ($node instanceof CallLike) { if ($node->isFirstClassCallable()) { return null; @@ -186,7 +173,9 @@ private function shouldSkip( return true; } - if ($node->getAttribute(self::IS_IN_ASSIGN) === true) { + if ($node->getAttribute(AttributeKey::IS_ASSIGNED_TO) === true || $node->getAttribute( + AttributeKey::IS_BEING_ASSIGNED + )) { return true; } diff --git a/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php b/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php index 6430621ae6e..af5ebb098d5 100644 --- a/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php +++ b/rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php @@ -5,12 +5,8 @@ namespace Rector\Php70\Rector\Break_; use PhpParser\Node; -use PhpParser\Node\Expr\ArrowFunction; -use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt\Break_; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Switch_; use PhpParser\NodeVisitor; use Rector\NodeNestingScope\ContextAnalyzer; use Rector\Rector\AbstractRector; @@ -24,11 +20,6 @@ */ final class BreakNotInLoopOrSwitchToReturnRector extends AbstractRector implements MinPhpVersionInterface { - /** - * @var string - */ - private const IS_BREAK_IN_SWITCH = 'is_break_in_switch'; - public function __construct( private readonly ContextAnalyzer $contextAnalyzer ) { @@ -82,43 +73,19 @@ public function run() */ public function getNodeTypes(): array { - return [Switch_::class, Break_::class]; + return [Break_::class]; } /** - * @param Switch_|Break_ $node + * @param Break_ $node * @return Return_|null|NodeVisitor::REMOVE_NODE */ public function refactor(Node $node): Return_|null|int { - if ($node instanceof Switch_) { - $this->traverseNodesWithCallable( - $node->cases, - static function (Node $subNode): ?int { - if ($subNode instanceof Class_ || ($subNode instanceof FunctionLike && ! $subNode instanceof ArrowFunction)) { - return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; - } - - if (! $subNode instanceof Break_) { - return null; - } - - $subNode->setAttribute(self::IS_BREAK_IN_SWITCH, true); - return null; - } - ); - - return null; - } - if ($this->contextAnalyzer->isInLoop($node)) { return null; } - if ($node->getAttribute(self::IS_BREAK_IN_SWITCH) === true) { - return null; - } - if ($this->contextAnalyzer->isInIf($node)) { return new Return_(); } diff --git a/src/NodeNestingScope/ContextAnalyzer.php b/src/NodeNestingScope/ContextAnalyzer.php index bbc38871353..ece022ee036 100644 --- a/src/NodeNestingScope/ContextAnalyzer.php +++ b/src/NodeNestingScope/ContextAnalyzer.php @@ -17,7 +17,7 @@ final class ContextAnalyzer */ public function isInLoop(Node $node): bool { - return $node->getAttribute(AttributeKey::IS_IN_LOOP) === true; + return $node->getAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH) === true; } /** diff --git a/src/NodeTypeResolver/Node/AttributeKey.php b/src/NodeTypeResolver/Node/AttributeKey.php index c65ee8824f3..46f9c922463 100644 --- a/src/NodeTypeResolver/Node/AttributeKey.php +++ b/src/NodeTypeResolver/Node/AttributeKey.php @@ -172,7 +172,7 @@ final class AttributeKey /** * @var string */ - public const IS_IN_LOOP = 'is_in_loop'; + public const IS_IN_LOOP_OR_SWITCH = 'is_in_loop'; /** * @var string diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index f09c46e8065..0785dcd718c 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -162,6 +162,7 @@ private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): v } $stmts = $node instanceof Switch_ ? $node->cases : $node->stmts; + $this->simpleCallableNodeTraverser->traverseNodesWithCallable( $stmts, static function (Node $subNode): ?int { @@ -170,7 +171,7 @@ static function (Node $subNode): ?int { } if ($subNode instanceof If_ || $subNode instanceof Break_) { - $subNode->setAttribute(AttributeKey::IS_IN_LOOP, true); + $subNode->setAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH, true); } return null;