From 436bd791bf256ca59334f2746e70a58d581d8574 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 22 Dec 2023 13:39:27 +0100 Subject: [PATCH] Scope changes rebasable --- src/Analyser/NodeScopeResolver.php | 75 ++++++++++++++---------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 19abfc6a8c..95db99f1e2 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -45,7 +45,6 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Static_; -use PhpParser\Node\Stmt\StaticVar; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\Throw_; use PhpParser\Node\Stmt\TryCatch; @@ -1549,28 +1548,23 @@ private function processStmtNode( $vars = []; foreach ($stmt->vars as $var) { - $scope = $this->processStmtNode($var, $scope, $nodeCallback, $context)->getScope(); if (!is_string($var->var->name)) { - continue; + throw new ShouldNotHappenException(); } + if ($var->default !== null) { + $this->processExprNode($var->default, $scope, $nodeCallback, ExpressionContext::createDeep()); + } + + $scope = $scope->enterExpressionAssign($var->var); + $this->processExprNode($var->var, $scope, $nodeCallback, ExpressionContext::createDeep()); + $scope = $scope->exitExpressionAssign($var->var); + + $scope = $scope->assignVariable($var->var->name, new MixedType(), new MixedType()); $vars[] = $var->var->name; } $scope = $this->processVarAnnotation($scope, $vars, $stmt); - } elseif ($stmt instanceof StaticVar) { - $hasYield = false; - $throwPoints = []; - if (!is_string($stmt->var->name)) { - throw new ShouldNotHappenException(); - } - if ($stmt->default !== null) { - $this->processExprNode($stmt->default, $scope, $nodeCallback, ExpressionContext::createDeep()); - } - $scope = $scope->enterExpressionAssign($stmt->var); - $this->processExprNode($stmt->var, $scope, $nodeCallback, ExpressionContext::createDeep()); - $scope = $scope->exitExpressionAssign($stmt->var); - $scope = $scope->assignVariable($stmt->var->name, new MixedType(), new MixedType()); } elseif ($stmt instanceof Node\Stmt\Const_) { $hasYield = false; $throwPoints = []; @@ -2462,10 +2456,6 @@ static function (): void { } } elseif ($expr instanceof Expr\Closure) { return $this->processClosureNode($expr, $scope, $nodeCallback, $context, null); - } elseif ($expr instanceof Expr\ClosureUse) { - $this->processExprNode($expr->var, $scope, $nodeCallback, $context); - $hasYield = false; - $throwPoints = []; } elseif ($expr instanceof Expr\ArrowFunction) { return $this->processArrowFunctionNode($expr, $scope, $nodeCallback, $context, null); } elseif ($expr instanceof ErrorSuppress) { @@ -2514,25 +2504,20 @@ static function (): void { if ($arrayItem === null) { continue; } - $result = $this->processExprNode($arrayItem, $scope, $nodeCallback, $context->enterDeep()); - $hasYield = $hasYield || $result->hasYield(); - $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); - $scope = $result->getScope(); + $nodeCallback($arrayItem, $scope); + if ($arrayItem->key !== null) { + $keyResult = $this->processExprNode($arrayItem->key, $scope, $nodeCallback, $context->enterDeep()); + $hasYield = $hasYield || $keyResult->hasYield(); + $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); + $scope = $keyResult->getScope(); + } + + $valueResult = $this->processExprNode($arrayItem->value, $scope, $nodeCallback, $context->enterDeep()); + $hasYield = $hasYield || $valueResult->hasYield(); + $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); + $scope = $valueResult->getScope(); } $nodeCallback(new LiteralArrayNode($expr, $itemNodes), $scope); - } elseif ($expr instanceof ArrayItem) { - $hasYield = false; - $throwPoints = []; - if ($expr->key !== null) { - $result = $this->processExprNode($expr->key, $scope, $nodeCallback, $context->enterDeep()); - $hasYield = $result->hasYield(); - $throwPoints = $result->getThrowPoints(); - $scope = $result->getScope(); - } - $result = $this->processExprNode($expr->value, $scope, $nodeCallback, $context->enterDeep()); - $hasYield = $hasYield || $result->hasYield(); - $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); - $scope = $result->getScope(); } elseif ($expr instanceof BooleanAnd || $expr instanceof BinaryOp\LogicalAnd) { $leftResult = $this->processExprNode($expr->left, $scope, $nodeCallback, $context->enterDeep()); $rightResult = $this->processExprNode($expr->right, $leftResult->getTruthyScope(), $nodeCallback, $context); @@ -3409,7 +3394,7 @@ private function processClosureNode( $scope = $scope->assignVariable($inAssignRightSideVariableName, $variableType, $variableNativeType); } } - $this->processExprNode($use, $useScope, $nodeCallback, $context); + $this->processExprNode($use->var, $useScope, $nodeCallback, $context); if (!$use->byRef) { continue; } @@ -4116,9 +4101,17 @@ static function (): void { $itemScope = $itemScope->enterExpressionAssign($arrayItem->value); } $itemScope = $this->lookForSetAllowedUndefinedExpressions($itemScope, $arrayItem->value); - $itemResult = $this->processExprNode($arrayItem, $itemScope, $nodeCallback, $context->enterDeep()); - $hasYield = $hasYield || $itemResult->hasYield(); - $throwPoints = array_merge($throwPoints, $itemResult->getThrowPoints()); + $nodeCallback($arrayItem, $itemScope); + if ($arrayItem->key !== null) { + $keyResult = $this->processExprNode($arrayItem->key, $itemScope, $nodeCallback, $context->enterDeep()); + $hasYield = $hasYield || $keyResult->hasYield(); + $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); + $itemScope = $keyResult->getScope(); + } + + $valueResult = $this->processExprNode($arrayItem->value, $itemScope, $nodeCallback, $context->enterDeep()); + $hasYield = $hasYield || $valueResult->hasYield(); + $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); if ($arrayItem->key === null) { $dimExpr = new Node\Scalar\LNumber($i);