Skip to content

Commit 5118b65

Browse files
committed
rector.
1 parent ae7fa00 commit 5118b65

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/Rector/If_/ThrowIfRector.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ public function refactor(Node $node): ?Node
105105
return $expression;
106106
}
107107

108-
private function isSafeToTransform(Throw_ $throwExpr, Expr $ifCondition): bool
108+
109+
private function isSafeToTransform(Throw_ $throw, Expr $expr): bool
109110
{
110111
$shouldTransform = true;
111112
$bannedNodeTypes = [MethodCall::class, StaticCall::class, FuncCall::class, ArrayDimFetch::class, PropertyFetch::class, StaticPropertyFetch::class];
112-
$this->traverseNodesWithCallable($throwExpr->expr, function (Node $node) use (&$shouldTransform, $bannedNodeTypes, $ifCondition): ?int {
113+
$this->traverseNodesWithCallable($throw->expr, function (Node $node) use (&$shouldTransform, $bannedNodeTypes, $expr): ?int {
113114
if (
114115
in_array($node::class, $bannedNodeTypes, true)
115-
|| $node instanceof Variable && !$this->isSafeToTransformWithVariableAccess($node, $ifCondition)
116+
|| $node instanceof Variable && !$this->isSafeToTransformWithVariableAccess($node, $expr)
116117
) {
117118
$shouldTransform = false;
118119

@@ -130,24 +131,24 @@ private function isSafeToTransform(Throw_ $throwExpr, Expr $ifCondition): bool
130131
* This method checks if the variable was assigned on the right side of a short-circuit logical operator (conjunction and disjunction).
131132
* 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.
132133
*/
133-
private function isSafeToTransformWithVariableAccess(Node $node, Expr $ifCondition): bool {
134+
private function isSafeToTransformWithVariableAccess(Variable $variable, Expr $expr): bool {
134135
$firstShortCircuitOperator = $this->betterNodeFinder->findFirst(
135-
$ifCondition,
136+
$expr,
136137
fn(Node $node): bool => $node instanceof BooleanAnd || $node instanceof BooleanOr
137138
);
138-
if ($firstShortCircuitOperator === null) {
139+
if (!$firstShortCircuitOperator instanceof Node) {
139140
return true;
140141
}
141-
assert($firstShortCircuitOperator instanceof BooleanAnd or $firstShortCircuitOperator instanceof BooleanOr);
142+
assert($firstShortCircuitOperator instanceof BooleanAnd || $firstShortCircuitOperator instanceof BooleanOr);
142143

143-
$varName = $this->getName($node);
144+
$varName = $this->getName($variable);
144145
$hasUnsafeAssignment = $this->betterNodeFinder->findFirst(
145146
$firstShortCircuitOperator->right, // only here short-circuit problem can happen
146-
fn(Node $n): bool => $n instanceof Assign
147-
&& $n->var instanceof Variable
148-
&& $this->getName($n->var) === $varName
147+
fn(Node $node): bool => $node instanceof Assign
148+
&& $node->var instanceof Variable
149+
&& $this->getName($node->var) === $varName
149150
);
150151

151-
return $hasUnsafeAssignment === null;
152+
return !$hasUnsafeAssignment instanceof Node;
152153
}
153154
}

0 commit comments

Comments
 (0)