Skip to content

Commit 52a5ee4

Browse files
committed
Scope changes
1 parent b650509 commit 52a5ee4

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
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;
@@ -1086,16 +1086,16 @@ private function resolveType(string $exprString, Expr $node): Type
10861086
return $this->initializerExprTypeResolver->getType($node, InitializerExprContext::fromScope($this));
10871087
} elseif ($node instanceof String_) {
10881088
return $this->initializerExprTypeResolver->getType($node, InitializerExprContext::fromScope($this));
1089-
} elseif ($node instanceof Node\Scalar\Encapsed) {
1089+
} elseif ($node instanceof Node\Scalar\InterpolatedString) {
10901090
$resultType = null;
1091-
10921091
foreach ($node->parts as $part) {
1093-
$partType = $part instanceof EncapsedStringPart
1094-
? new ConstantStringType($part->value)
1095-
: $this->getType($part)->toString();
1092+
if ($part instanceof InterpolatedStringPart) {
1093+
$partType = new ConstantStringType($part->value);
1094+
} else {
1095+
$partType = $this->getType($part);
1096+
}
10961097
if ($resultType === null) {
10971098
$resultType = $partType;
1098-
10991099
continue;
11001100
}
11011101

@@ -2999,9 +2999,6 @@ private function enterAnonymousFunctionWithoutReflection(
29992999
continue;
30003000
}
30013001
foreach ($variables as $variable) {
3002-
if (!$variable instanceof Variable) {
3003-
continue 2;
3004-
}
30053002
if (!is_string($variable->name)) {
30063003
continue 2;
30073004
}
@@ -4303,7 +4300,7 @@ private function processFinallyScopeVariableTypeHolders(
43034300
}
43044301

43054302
/**
4306-
* @param Expr\ClosureUse[] $byRefUses
4303+
* @param Node\ClosureUse[] $byRefUses
43074304
*/
43084305
public function processClosureScope(
43094306
self $closureScope,

src/Analyser/NodeScopeResolver.php

Lines changed: 16 additions & 21 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;
@@ -46,7 +46,6 @@
4646
use PhpParser\Node\Stmt\Return_;
4747
use PhpParser\Node\Stmt\Static_;
4848
use PhpParser\Node\Stmt\Switch_;
49-
use PhpParser\Node\Stmt\Throw_;
5049
use PhpParser\Node\Stmt\TryCatch;
5150
use PhpParser\Node\Stmt\Unset_;
5251
use PhpParser\Node\Stmt\While_;
@@ -406,7 +405,10 @@ private function processStmtNode(
406405
}
407406

408407
$stmtScope = $scope;
409-
if ($stmt instanceof Throw_ || $stmt instanceof Return_) {
408+
if ($stmt instanceof Node\Stmt\Expression && $stmt->expr instanceof Expr\Throw_) {
409+
$stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr->expr, $nodeCallback);
410+
}
411+
if ($stmt instanceof Return_) {
410412
$stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback);
411413
}
412414

@@ -775,13 +777,6 @@ private function processStmtNode(
775777
if ($stmt->type !== null) {
776778
$nodeCallback($stmt->type, $scope);
777779
}
778-
} elseif ($stmt instanceof Throw_) {
779-
$result = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep());
780-
$throwPoints = $result->getThrowPoints();
781-
$throwPoints[] = ThrowPoint::createExplicit($result->getScope(), $scope->getType($stmt->expr), $stmt, false);
782-
return new StatementResult($result->getScope(), $result->hasYield(), true, [
783-
new StatementExitPoint($stmt, $scope),
784-
], $throwPoints);
785780
} elseif ($stmt instanceof If_) {
786781
$conditionType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean();
787782
$ifAlwaysTrue = $conditionType->isTrue()->yes();
@@ -1309,7 +1304,7 @@ private function processStmtNode(
13091304
}
13101305
foreach ($branchScopeResult->getExitPoints() as $exitPoint) {
13111306
$finallyExitPoints[] = $exitPoint;
1312-
if ($exitPoint->getStatement() instanceof Throw_) {
1307+
if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) {
13131308
continue;
13141309
}
13151310
if ($finallyScope !== null) {
@@ -1457,7 +1452,7 @@ private function processStmtNode(
14571452
}
14581453
foreach ($catchScopeResult->getExitPoints() as $exitPoint) {
14591454
$finallyExitPoints[] = $exitPoint;
1460-
if ($exitPoint->getStatement() instanceof Throw_) {
1455+
if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) {
14611456
continue;
14621457
}
14631458
if ($finallyScope !== null) {
@@ -1727,7 +1722,7 @@ private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Clo
17271722
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
17281723
} elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) {
17291724
$scope = $this->lookForExpressionCallback($scope, $expr->class, $callback);
1730-
} elseif ($expr instanceof Array_ || $expr instanceof List_) {
1725+
} elseif ($expr instanceof List_) {
17311726
foreach ($expr->items as $item) {
17321727
if ($item === null) {
17331728
continue;
@@ -2466,10 +2461,13 @@ static function (): void {
24662461
$throwPoints = $result->getThrowPoints();
24672462
$scope = $result->getScope();
24682463
}
2469-
} elseif ($expr instanceof Node\Scalar\Encapsed) {
2464+
} elseif ($expr instanceof Node\Scalar\InterpolatedString) {
24702465
$hasYield = false;
24712466
$throwPoints = [];
24722467
foreach ($expr->parts as $part) {
2468+
if (!$part instanceof Expr) {
2469+
continue;
2470+
}
24732471
$result = $this->processExprNode($stmt, $part, $scope, $nodeCallback, $context->enterDeep());
24742472
$hasYield = $hasYield || $result->hasYield();
24752473
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
@@ -2869,7 +2867,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
28692867
} else {
28702868
$items = [];
28712869
foreach ($filteringExprs as $filteringExpr) {
2872-
$items[] = new ArrayItem($filteringExpr);
2870+
$items[] = new Node\ArrayItem($filteringExpr);
28732871
}
28742872
$filteringExpr = new FuncCall(
28752873
new Name\FullyQualified('in_array'),
@@ -3262,7 +3260,7 @@ private function getAssignedVariables(Expr $expr): array
32623260
return [];
32633261
}
32643262

3265-
if ($expr instanceof Expr\List_ || $expr instanceof Expr\Array_) {
3263+
if ($expr instanceof Expr\List_) {
32663264
$names = [];
32673265
foreach ($expr->items as $item) {
32683266
if ($item === null) {
@@ -4090,7 +4088,7 @@ static function (): void {
40904088
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
40914089
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
40924090
}
4093-
} elseif ($var instanceof List_ || $var instanceof Array_) {
4091+
} elseif ($var instanceof List_) {
40944092
$result = $processExprCallback($scope);
40954093
$hasYield = $result->hasYield();
40964094
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
@@ -4514,7 +4512,7 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
45144512
$methodAst = clone $stmt;
45154513
$stmts[$i] = $methodAst;
45164514
if (array_key_exists($methodName, $methodModifiers)) {
4517-
$methodAst->flags = ($methodAst->flags & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $methodModifiers[$methodName];
4515+
$methodAst->flags = ($methodAst->flags & ~ Modifiers::VISIBILITY_MASK) | $methodModifiers[$methodName];
45184516
}
45194517

45204518
if (!array_key_exists($methodName, $methodNames)) {
@@ -4600,9 +4598,6 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
46004598
foreach ($returnStatement->getExecutionEnds() as $executionEnd) {
46014599
$statementResult = $executionEnd->getStatementResult();
46024600
$endNode = $executionEnd->getNode();
4603-
if ($endNode instanceof Node\Stmt\Throw_) {
4604-
continue;
4605-
}
46064601
if ($endNode instanceof Node\Stmt\Expression) {
46074602
$exprType = $statementResult->getScope()->getType($endNode->expr);
46084603
if ($exprType instanceof NeverType && $exprType->isExplicit()) {

0 commit comments

Comments
 (0)