3333 */
3434class ThrowIfRector extends AbstractRector
3535{
36- public function __construct (private readonly BetterNodeFinder $ betterNodeFinder ) {
37- }
36+ public function __construct (private readonly BetterNodeFinder $ betterNodeFinder ) {}
3837
3938 public function getRuleDefinition (): RuleDefinition
4039 {
@@ -105,15 +104,14 @@ public function refactor(Node $node): ?Node
105104 return $ expression ;
106105 }
107106
108-
109107 private function isSafeToTransform (Throw_ $ throw , Expr $ expr ): bool
110108 {
111109 $ shouldTransform = true ;
112110 $ bannedNodeTypes = [MethodCall::class, StaticCall::class, FuncCall::class, ArrayDimFetch::class, PropertyFetch::class, StaticPropertyFetch::class];
113111 $ this ->traverseNodesWithCallable ($ throw ->expr , function (Node $ node ) use (&$ shouldTransform , $ bannedNodeTypes , $ expr ): ?int {
114112 if (
115113 in_array ($ node ::class, $ bannedNodeTypes , true )
116- || $ node instanceof Variable && !$ this ->isSafeToTransformWithVariableAccess ($ node , $ expr )
114+ || $ node instanceof Variable && ! $ this ->isSafeToTransformWithVariableAccess ($ node , $ expr )
117115 ) {
118116 $ shouldTransform = false ;
119117
@@ -131,24 +129,25 @@ private function isSafeToTransform(Throw_ $throw, Expr $expr): bool
131129 * This method checks if the variable was assigned on the right side of a short-circuit logical operator (conjunction and disjunction).
132130 * Note: The check is a little too strict, because such a variable may be initialized before the if-statement, and in such case it doesn't matter if it was assigned somewhere in the condition.
133131 */
134- private function isSafeToTransformWithVariableAccess (Variable $ variable , Expr $ expr ): bool {
132+ private function isSafeToTransformWithVariableAccess (Variable $ variable , Expr $ expr ): bool
133+ {
135134 $ firstShortCircuitOperator = $ this ->betterNodeFinder ->findFirst (
136135 $ expr ,
137- fn (Node $ node ): bool => $ node instanceof BooleanAnd || $ node instanceof BooleanOr
136+ fn (Node $ node ): bool => $ node instanceof BooleanAnd || $ node instanceof BooleanOr
138137 );
139- if (!$ firstShortCircuitOperator instanceof Node) {
138+ if (! $ firstShortCircuitOperator instanceof Node) {
140139 return true ;
141140 }
142141 assert ($ firstShortCircuitOperator instanceof BooleanAnd || $ firstShortCircuitOperator instanceof BooleanOr);
143142
144143 $ varName = $ this ->getName ($ variable );
145144 $ hasUnsafeAssignment = $ this ->betterNodeFinder ->findFirst (
146145 $ firstShortCircuitOperator ->right , // only here short-circuit problem can happen
147- fn (Node $ node ): bool => $ node instanceof Assign
146+ fn (Node $ node ): bool => $ node instanceof Assign
148147 && $ node ->var instanceof Variable
149148 && $ this ->getName ($ node ->var ) === $ varName
150149 );
151150
152- return !$ hasUnsafeAssignment instanceof Node;
151+ return ! $ hasUnsafeAssignment instanceof Node;
153152 }
154153}
0 commit comments