Skip to content

Commit 64870b4

Browse files
committed
Refactor ExprHandler - add NodeScopeResolver as first parameter
1 parent 6a7d9e0 commit 64870b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+152
-367
lines changed

src/Analyser/ExprHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function supports(Expr $expr): bool;
2222
* @param callable(Node $node, Scope $scope): void $nodeCallback
2323
*/
2424
public function processExpr(
25+
NodeScopeResolver $nodeScopeResolver,
2526
Stmt $stmt,
2627
Expr $expr,
2728
MutatingScope $scope,

src/Analyser/ExprHandler/ArrayDimFetchHandler.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,27 @@
2525
final class ArrayDimFetchHandler implements ExprHandler
2626
{
2727

28-
public function __construct(
29-
private NodeScopeResolver $nodeScopeResolver,
30-
)
31-
{
32-
}
33-
3428
public function supports(Expr $expr): bool
3529
{
3630
return $expr instanceof ArrayDimFetch;
3731
}
3832

39-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
33+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
4034
{
4135
$hasYield = false;
4236
$throwPoints = [];
4337
$impurePoints = [];
4438
$isAlwaysTerminating = false;
4539
if ($expr->dim !== null) {
46-
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->dim, $scope, $storage, $nodeCallback, $context->enterDeep());
40+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->dim, $scope, $storage, $nodeCallback, $context->enterDeep());
4741
$hasYield = $result->hasYield();
4842
$throwPoints = $result->getThrowPoints();
4943
$impurePoints = $result->getImpurePoints();
5044
$isAlwaysTerminating = $result->isAlwaysTerminating();
5145
$scope = $result->getScope();
5246
}
5347

54-
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep());
48+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep());
5549
$hasYield = $hasYield || $result->hasYield();
5650
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
5751
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
@@ -60,7 +54,7 @@ public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, Expres
6054

6155
$varType = $scope->getType($expr->var);
6256
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
63-
$throwPoints = array_merge($throwPoints, $this->nodeScopeResolver->processExprNode(
57+
$throwPoints = array_merge($throwPoints, $nodeScopeResolver->processExprNode(
6458
$stmt,
6559
new MethodCall($expr->var, 'offsetGet'),
6660
$scope,

src/Analyser/ExprHandler/ArrayHandler.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@
2323
final class ArrayHandler implements ExprHandler
2424
{
2525

26-
public function __construct(
27-
private NodeScopeResolver $nodeScopeResolver,
28-
)
29-
{
30-
}
31-
3226
public function supports(Expr $expr): bool
3327
{
3428
return $expr instanceof Array_;
3529
}
3630

37-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
31+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
3832
{
3933
$itemNodes = [];
4034
$hasYield = false;
@@ -43,24 +37,24 @@ public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, Expres
4337
$isAlwaysTerminating = false;
4438
foreach ($expr->items as $arrayItem) {
4539
$itemNodes[] = new LiteralArrayItem($scope, $arrayItem);
46-
$this->nodeScopeResolver->callNodeCallback($nodeCallback, $arrayItem, $scope, $storage);
40+
$nodeScopeResolver->callNodeCallback($nodeCallback, $arrayItem, $scope, $storage);
4741
if ($arrayItem->key !== null) {
48-
$keyResult = $this->nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $scope, $storage, $nodeCallback, $context->enterDeep());
42+
$keyResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $scope, $storage, $nodeCallback, $context->enterDeep());
4943
$hasYield = $hasYield || $keyResult->hasYield();
5044
$throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints());
5145
$impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints());
5246
$isAlwaysTerminating = $isAlwaysTerminating || $keyResult->isAlwaysTerminating();
5347
$scope = $keyResult->getScope();
5448
}
5549

56-
$valueResult = $this->nodeScopeResolver->processExprNode($stmt, $arrayItem->value, $scope, $storage, $nodeCallback, $context->enterDeep());
50+
$valueResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->value, $scope, $storage, $nodeCallback, $context->enterDeep());
5751
$hasYield = $hasYield || $valueResult->hasYield();
5852
$throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints());
5953
$impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints());
6054
$isAlwaysTerminating = $isAlwaysTerminating || $valueResult->isAlwaysTerminating();
6155
$scope = $valueResult->getScope();
6256
}
63-
$this->nodeScopeResolver->callNodeCallback($nodeCallback, new LiteralArrayNode($expr, $itemNodes), $scope, $storage);
57+
$nodeScopeResolver->callNodeCallback($nodeCallback, new LiteralArrayNode($expr, $itemNodes), $scope, $storage);
6458

6559
return new ExpressionResult(
6660
$scope,

src/Analyser/ExprHandler/AssignHandler.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,13 @@
2929
final class AssignHandler implements ExprHandler
3030
{
3131

32-
public function __construct(
33-
private NodeScopeResolver $nodeScopeResolver,
34-
)
35-
{
36-
}
37-
3832
public function supports(Expr $expr): bool
3933
{
4034
return $expr instanceof Assign || $expr instanceof AssignRef;
4135
}
4236

43-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
37+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
4438
{
45-
$nodeScopeResolver = $this->nodeScopeResolver;
4639
$result = $nodeScopeResolver->processAssignVar(
4740
$scope,
4841
$storage,

src/Analyser/ExprHandler/AssignOpHandler.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,13 @@
2727
final class AssignOpHandler implements ExprHandler
2828
{
2929

30-
public function __construct(
31-
private NodeScopeResolver $nodeScopeResolver,
32-
)
33-
{
34-
}
35-
3630
public function supports(Expr $expr): bool
3731
{
3832
return $expr instanceof AssignOp;
3933
}
4034

41-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
35+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
4236
{
43-
$nodeScopeResolver = $this->nodeScopeResolver;
4437
$result = $nodeScopeResolver->processAssignVar(
4538
$scope,
4639
$storage,

src/Analyser/ExprHandler/BinaryOpHandler.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
final class BinaryOpHandler implements ExprHandler
2828
{
2929

30-
public function __construct(
31-
private NodeScopeResolver $nodeScopeResolver,
32-
)
33-
{
34-
}
35-
3630
public function supports(Expr $expr): bool
3731
{
3832
return $expr instanceof BinaryOp
@@ -44,15 +38,15 @@ public function supports(Expr $expr): bool
4438
&& !$expr instanceof BinaryOp\Pipe;
4539
}
4640

47-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
41+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
4842
{
49-
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
43+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
5044
$scope = $result->getScope();
5145
$hasYield = $result->hasYield();
5246
$throwPoints = $result->getThrowPoints();
5347
$impurePoints = $result->getImpurePoints();
5448
$isAlwaysTerminating = $result->isAlwaysTerminating();
55-
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->right, $scope, $storage, $nodeCallback, $context->enterDeep());
49+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->right, $scope, $storage, $nodeCallback, $context->enterDeep());
5650
if (
5751
($expr instanceof BinaryOp\Div || $expr instanceof BinaryOp\Mod) &&
5852
!$scope->getType($expr->right)->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no()

src/Analyser/ExprHandler/BitwiseNotHandler.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,14 @@
2020
final class BitwiseNotHandler implements ExprHandler
2121
{
2222

23-
public function __construct(
24-
private NodeScopeResolver $nodeScopeResolver,
25-
)
26-
{
27-
}
28-
2923
public function supports(Expr $expr): bool
3024
{
3125
return $expr instanceof BitwiseNot;
3226
}
3327

34-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
28+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
3529
{
36-
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
30+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
3731
$scope = $result->getScope();
3832

3933
return new ExpressionResult(

src/Analyser/ExprHandler/BooleanAndHandler.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,24 @@
2424
final class BooleanAndHandler implements ExprHandler
2525
{
2626

27-
public function __construct(
28-
private NodeScopeResolver $nodeScopeResolver,
29-
)
30-
{
31-
}
32-
3327
public function supports(Expr $expr): bool
3428
{
3529
return $expr instanceof BooleanAnd || $expr instanceof LogicalAnd;
3630
}
3731

38-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
32+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
3933
{
40-
$leftResult = $this->nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
34+
$leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
4135
$leftTruthyScope = $leftResult->getTruthyScope();
42-
$rightResult = $this->nodeScopeResolver->processExprNode($stmt, $expr->right, $leftTruthyScope, $storage, $nodeCallback, $context);
36+
$rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftTruthyScope, $storage, $nodeCallback, $context);
4337
$rightExprType = $rightResult->getScope()->getType($expr->right);
4438
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
4539
$leftMergedWithRightScope = $leftResult->getFalseyScope();
4640
} else {
4741
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
4842
}
4943

50-
$this->nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanAndNode($expr, $leftTruthyScope), $scope, $storage, $context);
44+
$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanAndNode($expr, $leftTruthyScope), $scope, $storage, $context);
5145

5246
return new ExpressionResult(
5347
$leftMergedWithRightScope,

src/Analyser/ExprHandler/BooleanNotHandler.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,14 @@
2020
final class BooleanNotHandler implements ExprHandler
2121
{
2222

23-
public function __construct(
24-
private NodeScopeResolver $nodeScopeResolver,
25-
)
26-
{
27-
}
28-
2923
public function supports(Expr $expr): bool
3024
{
3125
return $expr instanceof BooleanNot;
3226
}
3327

34-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
28+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
3529
{
36-
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
30+
$result = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
3731
$scope = $result->getScope();
3832

3933
return new ExpressionResult(

src/Analyser/ExprHandler/BooleanOrHandler.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,24 @@
2424
final class BooleanOrHandler implements ExprHandler
2525
{
2626

27-
public function __construct(
28-
private NodeScopeResolver $nodeScopeResolver,
29-
)
30-
{
31-
}
32-
3327
public function supports(Expr $expr): bool
3428
{
3529
return $expr instanceof BooleanOr || $expr instanceof LogicalOr;
3630
}
3731

38-
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
32+
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
3933
{
40-
$leftResult = $this->nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
34+
$leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
4135
$leftFalseyScope = $leftResult->getFalseyScope();
42-
$rightResult = $this->nodeScopeResolver->processExprNode($stmt, $expr->right, $leftFalseyScope, $storage, $nodeCallback, $context);
36+
$rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftFalseyScope, $storage, $nodeCallback, $context);
4337
$rightExprType = $rightResult->getScope()->getType($expr->right);
4438
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
4539
$leftMergedWithRightScope = $leftResult->getTruthyScope();
4640
} else {
4741
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
4842
}
4943

50-
$this->nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanOrNode($expr, $leftFalseyScope), $scope, $storage, $context);
44+
$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanOrNode($expr, $leftFalseyScope), $scope, $storage, $context);
5145

5246
return new ExpressionResult(
5347
$leftMergedWithRightScope,

0 commit comments

Comments
 (0)