Skip to content

Commit 343d47b

Browse files
committed
Always pass around alternative node callback in NodeCallbackRequest
1 parent ec53bd2 commit 343d47b

File tree

11 files changed

+39
-77
lines changed

11 files changed

+39
-77
lines changed

build/PHPStan/Build/GeneratorYieldSendTypeExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Build;
44

55
use PhpParser\Node\Expr;
6-
use PHPStan\Analyser\Generator\AlternativeNodeCallbackRequest;
76
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
87
use PHPStan\Analyser\Generator\ExprAnalysisResult;
98
use PHPStan\Analyser\Generator\NodeCallbackRequest;
@@ -54,9 +53,6 @@ public function getType(Expr $expr, Scope $scope): ?Type
5453
if ((new ObjectType(NodeCallbackRequest::class))->isSuperTypeOf($valueType)->yes()) {
5554
return new NullType();
5655
}
57-
if ((new ObjectType(AlternativeNodeCallbackRequest::class))->isSuperTypeOf($valueType)->yes()) {
58-
return new NullType();
59-
}
6056
if ((new ObjectType(TypeExprRequest::class))->isSuperTypeOf($valueType)->yes()) {
6157
return new ObjectType(TypeExprResult::class);
6258
}

src/Analyser/Generator/AlternativeNodeCallbackRequest.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Analyser/Generator/ExprHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function supports(Expr $expr): bool;
2525
/**
2626
* @param T $expr
2727
* @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback
28-
* @return Generator<int, ExprAnalysisRequest|AlternativeNodeCallbackRequest|NodeCallbackRequest|TypeExprRequest, ExprAnalysisResult|TypeExprResult, ExprAnalysisResult>
28+
* @return Generator<int, ExprAnalysisRequest|NodeCallbackRequest|TypeExprRequest, ExprAnalysisResult|TypeExprResult, ExprAnalysisResult>
2929
*/
3030
public function analyseExpr(
3131
Stmt $stmt,

src/Analyser/Generator/ExprHandler/AssignHandler.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static function (GeneratorScope $scope) use ($stmt, $expr, $context, $alternativ
172172
yield from $processVarGen;
173173
$scope = $processVarGen->getReturn();
174174
if (!$varChangedScope) {
175-
$stmtVarGen = $this->processStmtVarAnnotation($scope, $stmt, null);
175+
$stmtVarGen = $this->processStmtVarAnnotation($scope, $stmt, null, $alternativeNodeCallback);
176176
yield from $stmtVarGen;
177177
$scope = $stmtVarGen->getReturn();
178178
}
@@ -277,9 +277,10 @@ private function processVarAnnotation(GeneratorScope $scope, array $variableName
277277
}
278278

279279
/**
280+
* @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback
280281
* @return Generator<int, ExprAnalysisRequest|NodeCallbackRequest|TypeExprRequest, ExprAnalysisResult|TypeExprResult, GeneratorScope>
281282
*/
282-
private function processStmtVarAnnotation(GeneratorScope $scope, Node\Stmt $stmt, ?Expr $defaultExpr): Generator
283+
private function processStmtVarAnnotation(GeneratorScope $scope, Node\Stmt $stmt, ?Expr $defaultExpr, ?callable $alternativeNodeCallback): Generator
283284
{
284285
$function = $scope->getFunction();
285286
$variableLessTags = [];
@@ -333,7 +334,7 @@ private function processStmtVarAnnotation(GeneratorScope $scope, Node\Stmt $stmt
333334
$variableNode = new Variable($name, $stmt->getAttributes());
334335
$originalType = $scope->getVariableType($name);
335336
if (!$originalType->equals($varTag->getType())) {
336-
yield new NodeCallbackRequest(new VarTagChangedExpressionTypeNode($varTag, $variableNode), $scope);
337+
yield new NodeCallbackRequest(new VarTagChangedExpressionTypeNode($varTag, $variableNode), $scope, $alternativeNodeCallback);
337338
}
338339

339340
$variableNodeResult = yield new ExprAnalysisRequest($stmt, $variableNode, $scope, ExpressionContext::createDeep(), new NoopNodeCallback());
@@ -379,7 +380,7 @@ private function processAssignVar(
379380
bool $enterExpressionAssign,
380381
): Generator
381382
{
382-
yield new NodeCallbackRequest($var, $enterExpressionAssign ? $scope->enterExpressionAssign($var) : $scope);
383+
yield new NodeCallbackRequest($var, $enterExpressionAssign ? $scope->enterExpressionAssign($var) : $scope, $alternativeNodeCallback);
383384

384385
$isAssignOp = $assignedExpr instanceof Expr\AssignOp && !$enterExpressionAssign;
385386
if ($var instanceof Variable && is_string($var->name)) {
@@ -451,7 +452,7 @@ private function processAssignVar(
451452
yield from $sureTypesGen;
452453
$conditionalExpressions = $sureTypesGen->getReturn();
453454

454-
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedExpr), $scopeBeforeAssignEval);
455+
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedExpr), $scopeBeforeAssignEval, $alternativeNodeCallback);
455456

456457
$assignVarGen = $scope->assignVariable($var->name, $type, $result->nativeType, TrinaryLogic::createYes());
457458
yield from $assignVarGen;
@@ -526,7 +527,7 @@ private function processAssignVar(
526527

527528
// Callback was already called for last dim at the beginning of the method.
528529
if ($key !== $lastDimKey) {
529-
yield new NodeCallbackRequest($dimFetch, $enterExpressionAssign ? $scope->enterExpressionAssign($dimFetch) : $scope);
530+
yield new NodeCallbackRequest($dimFetch, $enterExpressionAssign ? $scope->enterExpressionAssign($dimFetch) : $scope, $alternativeNodeCallback);
530531
}
531532

532533
if ($dimExpr === null) {
@@ -620,13 +621,13 @@ private function processAssignVar(
620621

621622
if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
622623
if ($var instanceof Variable && is_string($var->name)) {
623-
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval);
624+
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval, $alternativeNodeCallback);
624625
$assignVarGen = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
625626
yield from $assignVarGen;
626627
$scope = $assignVarGen->getReturn();
627628
} else {
628629
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
629-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval);
630+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval, $alternativeNodeCallback);
630631
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
631632
$scope = $scope->assignInitializedProperty((yield new TypeExprRequest($var->var))->type, $var->name->toString());
632633
}
@@ -641,9 +642,9 @@ private function processAssignVar(
641642
}
642643
} else {
643644
if ($var instanceof Variable) {
644-
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval);
645+
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval, $alternativeNodeCallback);
645646
} elseif ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
646-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval);
647+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval, $alternativeNodeCallback);
647648
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
648649
$scope = $scope->assignInitializedProperty((yield new TypeExprRequest($var->var))->type, $var->name->toString());
649650
}
@@ -721,7 +722,7 @@ private function processAssignVar(
721722
if ($propertyName !== null && $propertyHolderType->hasInstanceProperty($propertyName)->yes()) {
722723
$propertyReflection = $propertyHolderType->getInstanceProperty($propertyName, $scope);
723724
$assignedExprType = $result->type;
724-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
725+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval, $alternativeNodeCallback);
725726
if ($propertyReflection->canChangeTypeAfterAssignment()) {
726727
if ($propertyReflection->hasNativeType()) {
727728
$propertyNativeType = $propertyReflection->getNativeType();
@@ -773,7 +774,7 @@ private function processAssignVar(
773774
} else {
774775
// fallback
775776
$assignedExprType = $result->type;
776-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
777+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval, $alternativeNodeCallback);
777778
$assignExprGen = $scope->assignExpression($var, $assignedExprType, $result->nativeType);
778779
yield from $assignExprGen;
779780
$scope = $assignExprGen->getReturn();
@@ -838,7 +839,7 @@ private function processAssignVar(
838839
if ($propertyName !== null) {
839840
$propertyReflection = $scope->getStaticPropertyReflection($propertyHolderType, $propertyName);
840841
$assignedExprType = $result->type;
841-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
842+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval, $alternativeNodeCallback);
842843
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
843844
if ($propertyReflection->hasNativeType()) {
844845
$propertyNativeType = $propertyReflection->getNativeType();
@@ -875,7 +876,7 @@ private function processAssignVar(
875876
} else {
876877
// fallback
877878
$assignedExprType = $result->type;
878-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
879+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval, $alternativeNodeCallback);
879880
$assignExprGen = $scope->assignExpression($var, $assignedExprType, $result->nativeType);
880881
yield from $assignExprGen;
881882
$scope = $assignExprGen->getReturn();
@@ -911,7 +912,7 @@ private function processAssignVar(
911912
$itemScope = $itemScope->enterExpressionAssign($arrayItem->value);
912913
}
913914
$itemScope = $this->lookForSetAllowedUndefinedExpressions($itemScope, $arrayItem->value);
914-
yield new NodeCallbackRequest($arrayItem, $itemScope);
915+
yield new NodeCallbackRequest($arrayItem, $itemScope, $alternativeNodeCallback);
915916
if ($arrayItem->key !== null) {
916917
$keyResult = yield new ExprAnalysisRequest($stmt, $arrayItem->key, $itemScope, $context->enterDeep(), $alternativeNodeCallback);
917918
$hasYield = $hasYield || $keyResult->hasYield;
@@ -1051,13 +1052,13 @@ static function (GeneratorScope $scope): Generator {
10511052
}
10521053

10531054
if ($var instanceof Variable && is_string($var->name)) {
1054-
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedPropertyExpr), $scope);
1055+
yield new NodeCallbackRequest(new VariableAssignNode($var, $assignedPropertyExpr), $scope, $alternativeNodeCallback);
10551056
$assignVarGen = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
10561057
yield from $assignVarGen;
10571058
$scope = $assignVarGen->getReturn();
10581059
} else {
10591060
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
1060-
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope);
1061+
yield new NodeCallbackRequest(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope, $alternativeNodeCallback);
10611062
}
10621063
$assignExprGen = $scope->assignExpression(
10631064
$var,

src/Analyser/Generator/ExprHandler/LiteralArrayHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function analyseExpr(
5858
$isAlwaysTerminating = false;
5959

6060
foreach ($expr->items as $arrayItem) {
61-
yield new NodeCallbackRequest($arrayItem, $scope);
61+
yield new NodeCallbackRequest($arrayItem, $scope, $alternativeNodeCallback);
6262
$keyResult = null;
6363
if ($arrayItem->key !== null) {
6464
$keyResult = yield new ExprAnalysisRequest($stmt, $arrayItem->key, $scope, $context->enterDeep(), $alternativeNodeCallback);

src/Analyser/Generator/GeneratorNodeScopeResolver.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,17 @@ private function runTrampoline(
177177
$yielded = $gen->generator->current();
178178

179179
if ($yielded instanceof NodeCallbackRequest) {
180+
$alternativeNodeCallback = $yielded->alternativeNodeCallback;
180181
$this->invokeNodeCallback(
181182
$fibersStorage,
182183
$exprAnalysisResultStorage,
183184
$yielded->node,
184185
$yielded->scope,
185-
$nodeCallback,
186-
);
187-
188-
$gen->generator->next();
189-
continue;
190-
} elseif ($yielded instanceof AlternativeNodeCallbackRequest) {
191-
$alternativeNodeCallback = $yielded->nodeCallback;
192-
$this->invokeNodeCallback(
193-
$fibersStorage,
194-
$exprAnalysisResultStorage,
195-
$yielded->node,
196-
$yielded->scope,
197-
static function (Node $node, Scope $scope) use ($alternativeNodeCallback, $nodeCallback): void {
198-
$alternativeNodeCallback($node, $scope, $nodeCallback);
199-
},
186+
$alternativeNodeCallback !== null
187+
? static function (Node $node, Scope $scope) use ($alternativeNodeCallback, $nodeCallback): void {
188+
$alternativeNodeCallback($node, $scope, $nodeCallback);
189+
}
190+
: $nodeCallback,
200191
);
201192

202193
$gen->generator->next();
@@ -319,15 +310,11 @@ private function analyseStmts(array $stmts, GeneratorScope $scope, StatementCont
319310

320311
/**
321312
* @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback
322-
* @return Generator<int, ExprAnalysisRequest|StmtAnalysisRequest|StmtsAnalysisRequest|NodeCallbackRequest|AlternativeNodeCallbackRequest, ExprAnalysisResult|StmtAnalysisResult, StmtAnalysisResult>
313+
* @return Generator<int, ExprAnalysisRequest|StmtAnalysisRequest|StmtsAnalysisRequest|NodeCallbackRequest, ExprAnalysisResult|StmtAnalysisResult, StmtAnalysisResult>
323314
*/
324315
private function analyseStmt(Stmt $stmt, GeneratorScope $scope, StatementContext $context, ?callable $alternativeNodeCallback): Generator
325316
{
326-
if ($alternativeNodeCallback === null) {
327-
yield new NodeCallbackRequest($stmt, $scope);
328-
} else {
329-
yield new AlternativeNodeCallbackRequest($stmt, $scope, $alternativeNodeCallback);
330-
}
317+
yield new NodeCallbackRequest($stmt, $scope, $alternativeNodeCallback);
331318

332319
/**
333320
* @var StmtHandler<Stmt> $stmtHandler
@@ -348,7 +335,7 @@ private function analyseStmt(Stmt $stmt, GeneratorScope $scope, StatementContext
348335

349336
/**
350337
* @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback
351-
* @return Generator<int, TypeExprRequest|ExprAnalysisRequest|NodeCallbackRequest|AlternativeNodeCallbackRequest, ExprAnalysisResult, TypeExprResult|ExprAnalysisResult>
338+
* @return Generator<int, TypeExprRequest|ExprAnalysisRequest|NodeCallbackRequest, ExprAnalysisResult, TypeExprResult|ExprAnalysisResult>
352339
*/
353340
private function analyseExpr(ExprAnalysisResultStorage $storage, Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
354341
{
@@ -361,11 +348,7 @@ private function analyseExpr(ExprAnalysisResultStorage $storage, Stmt $stmt, Exp
361348
throw new ShouldNotHappenException(sprintf('Expr %s on line %d has already been analysed', $this->exprPrinter->printExpr($expr), $expr->getStartLine()));
362349
}
363350

364-
if ($alternativeNodeCallback === null) {
365-
yield new NodeCallbackRequest($expr, $scope);
366-
} else {
367-
yield new AlternativeNodeCallbackRequest($expr, $scope, $alternativeNodeCallback);
368-
}
351+
yield new NodeCallbackRequest($expr, $scope, $alternativeNodeCallback);
369352

370353
/**
371354
* @var ExprHandler<Expr> $exprHandler

src/Analyser/Generator/GeneratorScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3686,7 +3686,7 @@ public function getPhpVersion(): PhpVersions
36863686

36873687
public function invokeNodeCallback(Node $node): void
36883688
{
3689-
Fiber::suspend(new NodeCallbackRequest($node, $this));
3689+
Fiber::suspend(new NodeCallbackRequest($node, $this, null));
36903690
}
36913691

36923692
}

src/Analyser/Generator/IdentifiedGeneratorInStack.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ final class IdentifiedGeneratorInStack
1111
/**
1212
* @param (
1313
* Generator<int, StmtAnalysisRequest, StmtAnalysisResult, StmtAnalysisResult>| // analyseStmts
14-
* Generator<int, ExprAnalysisRequest|StmtAnalysisRequest|StmtsAnalysisRequest|NodeCallbackRequest|AlternativeNodeCallbackRequest, ExprAnalysisResult|StmtAnalysisResult, StmtAnalysisResult>| // analyseStmt
15-
* Generator<int, TypeExprRequest|ExprAnalysisRequest|NodeCallbackRequest|AlternativeNodeCallbackRequest, ExprAnalysisResult, TypeExprResult|ExprAnalysisResult>| // analyseExpr
14+
* Generator<int, ExprAnalysisRequest|StmtAnalysisRequest|StmtsAnalysisRequest|NodeCallbackRequest, ExprAnalysisResult|StmtAnalysisResult, StmtAnalysisResult>| // analyseStmt
15+
* Generator<int, TypeExprRequest|ExprAnalysisRequest|NodeCallbackRequest, ExprAnalysisResult, TypeExprResult|ExprAnalysisResult>| // analyseExpr
1616
* Generator<int, TypeExprRequest, TypeExprResult, TypeExprResult> // analyseExprForType
1717
* ) $generator
1818
* @param Node|Node[] $node

src/Analyser/Generator/NodeCallbackRequest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
final class NodeCallbackRequest
99
{
1010

11+
/**
12+
* @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback
13+
*/
1114
public function __construct(
1215
public readonly Node $node,
1316
public readonly Scope $scope,
17+
public readonly mixed $alternativeNodeCallback,
1418
)
1519
{
1620
}

src/Analyser/Generator/StmtHandler/DeclareHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function supports(Stmt $stmt): bool
3131
public function analyseStmt(Stmt $stmt, GeneratorScope $scope, StatementContext $context, ?callable $alternativeNodeCallback): Generator
3232
{
3333
foreach ($stmt->declares as $declare) {
34-
yield new NodeCallbackRequest($declare, $scope);
34+
yield new NodeCallbackRequest($declare, $scope, $alternativeNodeCallback);
3535
yield new ExprAnalysisRequest($stmt, $declare->value, $scope, ExpressionContext::createDeep(), $alternativeNodeCallback);
3636
if (
3737
$declare->key->name !== 'strict_types'

0 commit comments

Comments
 (0)