@@ -1336,9 +1336,9 @@ private function processStmtNode(
13361336 $ hasYield = false ;
13371337 $ throwPoints = [];
13381338 foreach ($ stmt ->vars as $ var ) {
1339- $ scope = $ this ->lookForEnterAllowedUndefinedVariable ($ scope , $ var, true );
1339+ $ scope = $ this ->lookForSetAllowedUndefinedExpressions ($ scope , $ var );
13401340 $ scope = $ this ->processExprNode ($ var , $ scope , $ nodeCallback , ExpressionContext::createDeep ())->getScope ();
1341- $ scope = $ this ->lookForExitAllowedUndefinedVariable ($ scope , $ var );
1341+ $ scope = $ this ->lookForUnsetAllowedUndefinedExpressions ($ scope , $ var );
13421342 $ scope = $ scope ->unsetExpression ($ var );
13431343 }
13441344 } elseif ($ stmt instanceof Node \Stmt \Use_) {
@@ -1355,9 +1355,9 @@ private function processStmtNode(
13551355 if (!$ var instanceof Variable) {
13561356 throw new ShouldNotHappenException ();
13571357 }
1358- $ scope = $ this ->lookForEnterAllowedUndefinedVariable ($ scope , $ var, true );
1358+ $ scope = $ this ->lookForSetAllowedUndefinedExpressions ($ scope , $ var );
13591359 $ this ->processExprNode ($ var , $ scope , $ nodeCallback , ExpressionContext::createDeep ());
1360- $ scope = $ this ->lookForExitAllowedUndefinedVariable ($ scope , $ var );
1360+ $ scope = $ this ->lookForUnsetAllowedUndefinedExpressions ($ scope , $ var );
13611361
13621362 if (!is_string ($ var ->name )) {
13631363 continue ;
@@ -1513,56 +1513,40 @@ private function createAstClassReflection(Node\Stmt\ClassLike $stmt, string $cla
15131513 );
15141514 }
15151515
1516- private function lookForEnterAllowedUndefinedVariable (MutatingScope $ scope , Expr $ expr, bool $ isAllowed ): MutatingScope
1516+ private function lookForSetAllowedUndefinedExpressions (MutatingScope $ scope , Expr $ expr ): MutatingScope
15171517 {
1518- if (!$ expr instanceof ArrayDimFetch || $ expr ->dim !== null ) {
1519- $ scope = $ scope ->setAllowedUndefinedExpression ($ expr , $ isAllowed );
1520- }
1521- if (!$ expr instanceof Variable) {
1522- return $ this ->lookForVariableCallback ($ scope , $ expr , static fn (MutatingScope $ scope , Expr $ expr ): MutatingScope => $ scope ->setAllowedUndefinedExpression ($ expr , $ isAllowed ));
1523- }
1524-
1525- return $ scope ;
1518+ return $ this ->lookForExpressionCallback ($ scope , $ expr , static fn (MutatingScope $ scope , Expr $ expr ): MutatingScope => $ scope ->setAllowedUndefinedExpression ($ expr ));
15261519 }
15271520
1528- private function lookForExitAllowedUndefinedVariable (MutatingScope $ scope , Expr $ expr ): MutatingScope
1521+ private function lookForUnsetAllowedUndefinedExpressions (MutatingScope $ scope , Expr $ expr ): MutatingScope
15291522 {
1530- if (!$ expr instanceof ArrayDimFetch || $ expr ->dim !== null ) {
1531- $ scope = $ scope ->unsetAllowedUndefinedExpression ($ expr );
1532- }
1533- if (!$ expr instanceof Variable) {
1534- return $ this ->lookForVariableCallback ($ scope , $ expr , static fn (MutatingScope $ scope , Expr $ expr ): MutatingScope => $ scope ->unsetAllowedUndefinedExpression ($ expr ));
1535- }
1536-
1537- return $ scope ;
1523+ return $ this ->lookForExpressionCallback ($ scope , $ expr , static fn (MutatingScope $ scope , Expr $ expr ): MutatingScope => $ scope ->unsetAllowedUndefinedExpression ($ expr ));
15381524 }
15391525
15401526 /**
15411527 * @param Closure(MutatingScope $scope, Expr $expr): MutatingScope $callback
15421528 */
1543- private function lookForVariableCallback (MutatingScope $ scope , Expr $ expr , Closure $ callback ): MutatingScope
1529+ private function lookForExpressionCallback (MutatingScope $ scope , Expr $ expr , Closure $ callback ): MutatingScope
15441530 {
1545- if ($ expr instanceof Variable ) {
1531+ if (! $ expr instanceof ArrayDimFetch || $ expr -> dim !== null ) {
15461532 $ scope = $ callback ($ scope , $ expr );
1547- } elseif ($ expr instanceof ArrayDimFetch) {
1548- if ($ expr ->dim !== null ) {
1549- $ scope = $ callback ($ scope , $ expr );
1550- }
1533+ }
15511534
1552- $ scope = $ this ->lookForVariableCallback ($ scope , $ expr ->var , $ callback );
1535+ if ($ expr instanceof ArrayDimFetch) {
1536+ $ scope = $ this ->lookForExpressionCallback ($ scope , $ expr ->var , $ callback );
15531537 } elseif ($ expr instanceof PropertyFetch || $ expr instanceof Expr \NullsafePropertyFetch) {
1554- $ scope = $ this ->lookForVariableCallback ($ scope , $ expr ->var , $ callback );
1538+ $ scope = $ this ->lookForExpressionCallback ($ scope , $ expr ->var , $ callback );
15551539 } elseif ($ expr instanceof StaticPropertyFetch) {
15561540 if ($ expr ->class instanceof Expr) {
1557- $ scope = $ this ->lookForVariableCallback ($ scope , $ expr ->class , $ callback );
1541+ $ scope = $ this ->lookForExpressionCallback ($ scope , $ expr ->class , $ callback );
15581542 }
15591543 } elseif ($ expr instanceof Array_ || $ expr instanceof List_) {
15601544 foreach ($ expr ->items as $ item ) {
15611545 if ($ item === null ) {
15621546 continue ;
15631547 }
15641548
1565- $ scope = $ this ->lookForVariableCallback ($ scope , $ item ->value , $ callback );
1549+ $ scope = $ this ->lookForExpressionCallback ($ scope , $ item ->value , $ callback );
15661550 }
15671551 }
15681552
@@ -2321,10 +2305,10 @@ static function (?Type $offsetType, Type $valueType) use (&$arrayType): void {
23212305 );
23222306 } elseif ($ expr instanceof Coalesce) {
23232307 $ nonNullabilityResult = $ this ->ensureNonNullability ($ scope , $ expr ->left , false );
2324- $ condScope = $ this ->lookForEnterAllowedUndefinedVariable ($ nonNullabilityResult ->getScope (), $ expr ->left , true );
2308+ $ condScope = $ this ->lookForSetAllowedUndefinedExpressions ($ nonNullabilityResult ->getScope (), $ expr ->left );
23252309 $ condResult = $ this ->processExprNode ($ expr ->left , $ condScope , $ nodeCallback , $ context ->enterDeep ());
23262310 $ scope = $ this ->revertNonNullability ($ condResult ->getScope (), $ nonNullabilityResult ->getSpecifiedExpressions ());
2327- $ scope = $ this ->lookForExitAllowedUndefinedVariable ($ scope , $ expr ->left );
2311+ $ scope = $ this ->lookForUnsetAllowedUndefinedExpressions ($ scope , $ expr ->left );
23282312
23292313 $ rightScope = $ scope ->filterByFalseyValue (new Expr \Isset_ ([$ expr ->left ]));
23302314 $ rightResult = $ this ->processExprNode ($ expr ->right , $ rightScope , $ nodeCallback , $ context ->enterDeep ());
@@ -2395,26 +2379,26 @@ static function (?Type $offsetType, Type $valueType) use (&$arrayType): void {
23952379 }
23962380 } elseif ($ expr instanceof Expr \Empty_) {
23972381 $ nonNullabilityResult = $ this ->ensureNonNullability ($ scope , $ expr ->expr , true );
2398- $ scope = $ this ->lookForEnterAllowedUndefinedVariable ($ nonNullabilityResult ->getScope (), $ expr ->expr , true );
2382+ $ scope = $ this ->lookForSetAllowedUndefinedExpressions ($ nonNullabilityResult ->getScope (), $ expr ->expr );
23992383 $ result = $ this ->processExprNode ($ expr ->expr , $ scope , $ nodeCallback , $ context ->enterDeep ());
24002384 $ scope = $ result ->getScope ();
24012385 $ hasYield = $ result ->hasYield ();
24022386 $ throwPoints = $ result ->getThrowPoints ();
24032387 $ scope = $ this ->revertNonNullability ($ scope , $ nonNullabilityResult ->getSpecifiedExpressions ());
2404- $ scope = $ this ->lookForExitAllowedUndefinedVariable ($ scope , $ expr ->expr );
2388+ $ scope = $ this ->lookForUnsetAllowedUndefinedExpressions ($ scope , $ expr ->expr );
24052389 } elseif ($ expr instanceof Expr \Isset_) {
24062390 $ hasYield = false ;
24072391 $ throwPoints = [];
24082392 $ nonNullabilityResults = [];
24092393 foreach ($ expr ->vars as $ var ) {
24102394 $ nonNullabilityResult = $ this ->ensureNonNullability ($ scope , $ var , true );
2411- $ scope = $ this ->lookForEnterAllowedUndefinedVariable ($ nonNullabilityResult ->getScope (), $ var, true );
2395+ $ scope = $ this ->lookForSetAllowedUndefinedExpressions ($ nonNullabilityResult ->getScope (), $ var );
24122396 $ result = $ this ->processExprNode ($ var , $ scope , $ nodeCallback , $ context ->enterDeep ());
24132397 $ scope = $ result ->getScope ();
24142398 $ hasYield = $ hasYield || $ result ->hasYield ();
24152399 $ throwPoints = array_merge ($ throwPoints , $ result ->getThrowPoints ());
24162400 $ nonNullabilityResults [] = $ nonNullabilityResult ;
2417- $ scope = $ this ->lookForExitAllowedUndefinedVariable ($ scope , $ var );
2401+ $ scope = $ this ->lookForUnsetAllowedUndefinedExpressions ($ scope , $ var );
24182402 }
24192403 foreach (array_reverse ($ nonNullabilityResults ) as $ nonNullabilityResult ) {
24202404 $ scope = $ this ->revertNonNullability ($ scope , $ nonNullabilityResult ->getSpecifiedExpressions ());
@@ -3466,7 +3450,7 @@ static function (): void {
34663450 if ($ arrayItem ->value instanceof ArrayDimFetch && $ arrayItem ->value ->dim === null ) {
34673451 $ itemScope = $ itemScope ->enterExpressionAssign ($ arrayItem ->value );
34683452 }
3469- $ itemScope = $ this ->lookForEnterAllowedUndefinedVariable ($ itemScope , $ arrayItem ->value , true );
3453+ $ itemScope = $ this ->lookForSetAllowedUndefinedExpressions ($ itemScope , $ arrayItem ->value );
34703454 $ itemResult = $ this ->processExprNode ($ arrayItem , $ itemScope , $ nodeCallback , $ context ->enterDeep ());
34713455 $ hasYield = $ hasYield || $ itemResult ->hasYield ();
34723456 $ throwPoints = array_merge ($ throwPoints , $ itemResult ->getThrowPoints ());
0 commit comments