Skip to content

Commit e7e3dfd

Browse files
committed
Scope changes
1 parent 038f0ca commit e7e3dfd

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

src/Analyser/MutatingScope.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
use PhpParser\Node\Expr\New_;
2222
use PhpParser\Node\Expr\PropertyFetch;
2323
use PhpParser\Node\Expr\Variable;
24+
use PhpParser\Node\InterpolatedStringPart;
2425
use PhpParser\Node\Name;
2526
use PhpParser\Node\Name\FullyQualified;
2627
use PhpParser\Node\Scalar\DNumber;
27-
use PhpParser\Node\Scalar\EncapsedStringPart;
2828
use PhpParser\Node\Scalar\LNumber;
2929
use PhpParser\Node\Scalar\String_;
3030
use PhpParser\NodeFinder;
@@ -1129,16 +1129,16 @@ private function resolveType(string $exprString, Expr $node): Type
11291129
return $this->initializerExprTypeResolver->getType($node, InitializerExprContext::fromScope($this));
11301130
} elseif ($node instanceof String_) {
11311131
return $this->initializerExprTypeResolver->getType($node, InitializerExprContext::fromScope($this));
1132-
} elseif ($node instanceof Node\Scalar\Encapsed) {
1132+
} elseif ($node instanceof Node\Scalar\InterpolatedString) {
11331133
$resultType = null;
1134-
11351134
foreach ($node->parts as $part) {
1136-
$partType = $part instanceof EncapsedStringPart
1137-
? new ConstantStringType($part->value)
1138-
: $this->getType($part)->toString();
1135+
if ($part instanceof InterpolatedStringPart) {
1136+
$partType = new ConstantStringType($part->value);
1137+
} else {
1138+
$partType = $this->getType($part);
1139+
}
11391140
if ($resultType === null) {
11401141
$resultType = $partType;
1141-
11421142
continue;
11431143
}
11441144

@@ -3455,9 +3455,6 @@ private function enterAnonymousFunctionWithoutReflection(
34553455
continue;
34563456
}
34573457
foreach ($variables as $variable) {
3458-
if (!$variable instanceof Variable) {
3459-
continue 2;
3460-
}
34613458
if (!is_string($variable->name)) {
34623459
continue 2;
34633460
}
@@ -4785,7 +4782,7 @@ private function processFinallyScopeVariableTypeHolders(
47854782
}
47864783

47874784
/**
4788-
* @param Expr\ClosureUse[] $byRefUses
4785+
* @param Node\ClosureUse[] $byRefUses
47894786
*/
47904787
public function processClosureScope(
47914788
self $closureScope,

src/Analyser/NodeScopeResolver.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
use Closure;
77
use DivisionByZeroError;
88
use PhpParser\Comment\Doc;
9+
use PhpParser\Modifiers;
910
use PhpParser\Node;
1011
use PhpParser\Node\Arg;
1112
use PhpParser\Node\AttributeGroup;
1213
use PhpParser\Node\Expr;
1314
use PhpParser\Node\Expr\Array_;
1415
use PhpParser\Node\Expr\ArrayDimFetch;
15-
use PhpParser\Node\Expr\ArrayItem;
1616
use PhpParser\Node\Expr\Assign;
1717
use PhpParser\Node\Expr\AssignRef;
1818
use PhpParser\Node\Expr\BinaryOp;
@@ -48,7 +48,6 @@
4848
use PhpParser\Node\Stmt\Return_;
4949
use PhpParser\Node\Stmt\Static_;
5050
use PhpParser\Node\Stmt\Switch_;
51-
use PhpParser\Node\Stmt\Throw_;
5251
use PhpParser\Node\Stmt\TryCatch;
5352
use PhpParser\Node\Stmt\Unset_;
5453
use PhpParser\Node\Stmt\While_;
@@ -474,7 +473,10 @@ private function processStmtNode(
474473
}
475474

476475
$stmtScope = $scope;
477-
if ($stmt instanceof Throw_ || $stmt instanceof Return_) {
476+
if ($stmt instanceof Node\Stmt\Expression && $stmt->expr instanceof Expr\Throw_) {
477+
$stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr->expr, $nodeCallback);
478+
}
479+
if ($stmt instanceof Return_) {
478480
$stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback);
479481
}
480482

@@ -922,14 +924,6 @@ private function processStmtNode(
922924
if ($stmt->type !== null) {
923925
$nodeCallback($stmt->type, $scope);
924926
}
925-
} elseif ($stmt instanceof Throw_) {
926-
$result = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep());
927-
$throwPoints = $result->getThrowPoints();
928-
$throwPoints[] = ThrowPoint::createExplicit($result->getScope(), $scope->getType($stmt->expr), $stmt, false);
929-
$impurePoints = $result->getImpurePoints();
930-
return new StatementResult($result->getScope(), $result->hasYield(), true, [
931-
new StatementExitPoint($stmt, $scope),
932-
], $throwPoints, $impurePoints);
933927
} elseif ($stmt instanceof If_) {
934928
$conditionType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean();
935929
$ifAlwaysTrue = $conditionType->isTrue()->yes();
@@ -1506,7 +1500,7 @@ private function processStmtNode(
15061500
}
15071501
foreach ($branchScopeResult->getExitPoints() as $exitPoint) {
15081502
$finallyExitPoints[] = $exitPoint;
1509-
if ($exitPoint->getStatement() instanceof Throw_) {
1503+
if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) {
15101504
continue;
15111505
}
15121506
if ($finallyScope !== null) {
@@ -1668,7 +1662,7 @@ private function processStmtNode(
16681662
}
16691663
foreach ($catchScopeResult->getExitPoints() as $exitPoint) {
16701664
$finallyExitPoints[] = $exitPoint;
1671-
if ($exitPoint->getStatement() instanceof Throw_) {
1665+
if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) {
16721666
continue;
16731667
}
16741668
if ($finallyScope !== null) {
@@ -2037,7 +2031,7 @@ private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Clo
20372031
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
20382032
} elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) {
20392033
$scope = $this->lookForExpressionCallback($scope, $expr->class, $callback);
2040-
} elseif ($expr instanceof Array_ || $expr instanceof List_) {
2034+
} elseif ($expr instanceof List_) {
20412035
foreach ($expr->items as $item) {
20422036
if ($item === null) {
20432037
continue;
@@ -2942,11 +2936,14 @@ static function (): void {
29422936
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
29432937
$scope = $result->getScope();
29442938
}
2945-
} elseif ($expr instanceof Node\Scalar\Encapsed) {
2939+
} elseif ($expr instanceof Node\Scalar\InterpolatedString) {
29462940
$hasYield = false;
29472941
$throwPoints = [];
29482942
$impurePoints = [];
29492943
foreach ($expr->parts as $part) {
2944+
if (!$part instanceof Expr) {
2945+
continue;
2946+
}
29502947
$result = $this->processExprNode($stmt, $part, $scope, $nodeCallback, $context->enterDeep());
29512948
$hasYield = $hasYield || $result->hasYield();
29522949
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
@@ -3630,7 +3627,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
36303627
} else {
36313628
$items = [];
36323629
foreach ($filteringExprs as $filteringExpr) {
3633-
$items[] = new ArrayItem($filteringExpr);
3630+
$items[] = new Node\ArrayItem($filteringExpr);
36343631
}
36353632
$filteringExpr = new FuncCall(
36363633
new Name\FullyQualified('in_array'),
@@ -4113,7 +4110,7 @@ private function getAssignedVariables(Expr $expr): array
41134110
return [];
41144111
}
41154112

4116-
if ($expr instanceof Expr\List_ || $expr instanceof Expr\Array_) {
4113+
if ($expr instanceof Expr\List_) {
41174114
$names = [];
41184115
foreach ($expr->items as $item) {
41194116
if ($item === null) {
@@ -5271,7 +5268,7 @@ static function (): void {
52715268
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
52725269
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
52735270
}
5274-
} elseif ($var instanceof List_ || $var instanceof Array_) {
5271+
} elseif ($var instanceof List_) {
52755272
$result = $processExprCallback($scope);
52765273
$hasYield = $result->hasYield();
52775274
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
@@ -5778,7 +5775,7 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
57785775
$methodAst = clone $stmt;
57795776
$stmts[$i] = $methodAst;
57805777
if (array_key_exists($methodName, $methodModifiers)) {
5781-
$methodAst->flags = ($methodAst->flags & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $methodModifiers[$methodName];
5778+
$methodAst->flags = ($methodAst->flags & ~ Modifiers::VISIBILITY_MASK) | $methodModifiers[$methodName];
57825779
}
57835780

57845781
if (!array_key_exists($methodName, $methodNames)) {
@@ -5867,9 +5864,6 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
58675864
foreach ($returnStatement->getExecutionEnds() as $executionEnd) {
58685865
$statementResult = $executionEnd->getStatementResult();
58695866
$endNode = $executionEnd->getNode();
5870-
if ($endNode instanceof Node\Stmt\Throw_) {
5871-
continue;
5872-
}
58735867
if ($endNode instanceof Node\Stmt\Expression) {
58745868
$exprType = $statementResult->getScope()->getType($endNode->expr);
58755869
if ($exprType instanceof NeverType && $exprType->isExplicit()) {

0 commit comments

Comments
 (0)