Repository navigation
Expand file tree
/
Copy pathNodeScopeResolver.php
More file actions
1289 lines (1172 loc) · 46.4 KB
/
Copy pathNodeScopeResolver.php
File metadata and controls
1289 lines (1172 loc) · 46.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use Closure;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\NodeFinder;
use PHPStan\Analyser\ExprHandler\Helper\NonNullabilityHelper;
use PHPStan\Analyser\Generics\TemplateArgumentConstraints;
use PHPStan\Analyser\Generics\TemplateArgumentFrame;
use PHPStan\Analyser\Generics\TemplateArgumentObserver;
use PHPStan\Analyser\Generics\TemplateArgumentStats;
use PHPStan\Dependency\Dependencies;
use PHPStan\DependencyInjection\AutowiredExtensions;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\ExtensionsCollection;
use PHPStan\File\FileHelper;
use PHPStan\Node\FunctionCallableNode;
use PHPStan\Node\FunctionCallExpressionNode;
use PHPStan\Node\InstantiationCallableNode;
use PHPStan\Node\MethodCallableNode;
use PHPStan\Node\MethodCallExpressionNode;
use PHPStan\Node\StaticMethodCallableNode;
use PHPStan\Node\StaticMethodCallExpressionNode;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use function array_merge;
use function array_pop;
use function get_class;
use function getenv;
use function is_array;
use function is_string;
use function spl_object_id;
use function sprintf;
#[AutowiredService]
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/NodeScopeResolver.cpp')]
class NodeScopeResolver
{
public const LOOP_SCOPE_ITERATIONS = 3;
public const GENERALIZE_AFTER_ITERATION = 1;
/** @var array<string, true> filePath(string) => bool(true) */
private array $analysedFiles = [];
/**
* When processing a synthetic node on demand, real AST
* nodes contained in it were already processed and must not be processed again.
*/
protected bool $returnStoredExpressionResults = false;
/**
* Consume-stored mode: a walk that deliberately re-enters an
* already-walked subtree (the nullsafe plain twin re-walking its
* receiver) consumes this node's stored result unconditionally instead
* of re-processing it - node callbacks fired during the original walk.
* Only this exact node is consumed: the rest of the twin (arguments,
* a dynamic name) is walked normally, even when the storage was seeded
* with results of an earlier loop convergence pass.
*/
private ?Expr $consumeStoredExpression = null;
private ?NonNullabilityHelper $nonNullabilityHelper = null;
/**
* Engine-feeding gatherer frames (return statements, execution ends,
* impure points, ...), innermost last. callNodeCallback() feeds every
* frame the raw walk scope at the emission position - gatherers are
* engine code and never ask about types, and their arrays are read as
* soon as the enclosing body walk returns.
*
* @var list<callable(Node, Scope): void>
*/
private array $nodeGatherers = [];
/** Whether the PHPSTAN_GUARD_NW diagnostic is enabled (cached from the env). */
public static bool $guardNewWorld = false;
/**
* spl_object_id => true of every Expr in the file's parsed AST. Populated
* only when the PHPSTAN_GUARD_NW diagnostic is enabled, so the guards can
* tell a real AST node from a node a rule built during analysis (which
* legitimately resolves on demand). Static so MutatingScope can read it.
*
* @var array<int, true>
*/
public static array $guardRealExprIds = [];
/**
* spl_object_id => true of every Expr already processed by processExprNode
* in the current file. Used by the MutatingScope::getType guard to detect a
* real AST node whose type is asked before it was processed.
*
* @var array<int, true>
*/
public static array $guardProcessedExprIds = [];
/**
* @param ExtensionsCollection<PerFileAnalysisResettable> $perFileAnalysisResettables
*/
public function __construct(
private readonly Container $container,
private readonly TemplateArgumentObserver $templateArgumentObserver,
private readonly FileHelper $fileHelper,
#[AutowiredExtensions(of: PerFileAnalysisResettable::class)]
private readonly ExtensionsCollection $perFileAnalysisResettables,
private readonly ExpressionResultFactory $expressionResultFactory,
private readonly StatementsHandler $statementsHandler,
)
{
self::$guardNewWorld = getenv('PHPSTAN_GUARD_NW') === '1';
TemplateArgumentStats::enableFromEnvironment();
}
/**
* The lookups (isAnalysedFile()) are keyed by normalized paths, so the
* given paths are normalized here - a caller-provided unnormalized path
* (mixed directory separators on Windows) must not silently skip the
* in-class-context analysis of a trait.
*
* @api
* @param string[] $files
*/
public function setAnalysedFiles(array $files): void
{
$analysedFiles = [];
foreach ($files as $file) {
$analysedFiles[$this->fileHelper->normalizePath($file)] = true;
}
$this->analysedFiles = $analysedFiles;
}
/**
* Releases the previous file's node-keyed captures: the parser cache
* retains ASTs, so node-keyed cache entries never die on
* their own and would hold that file's whole result graph alive.
*
* Called at the per-file boundary (FileAnalyser), NOT in processNodes():
* extensions start nested processNodes() walks mid-file (phpstan-doctrine
* parsing a query-builder method, rule tooling re-analysing a callee) and
* wiping the per-file caches there forces the outer file to rebuild them -
* closure types re-converge, narrowing memos recompute.
*/
private function getNonNullabilityHelper(): NonNullabilityHelper
{
return $this->nonNullabilityHelper ??= $this->container->getByType(NonNullabilityHelper::class);
}
public function resetPerFileAnalysisState(): void
{
foreach ($this->perFileAnalysisResettables->getAll() as $resettableService) {
$resettableService->resetFileAnalysisState();
}
}
/**
* @api
* @param Node[] $nodes
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processNodes(
array $nodes,
MutatingScope $scope,
callable $nodeCallback,
): void
{
$this->processFileNodes($nodes, $scope, $nodeCallback);
}
/**
* processNodes() for the analysis of a file: returns what the file depends on besides itself.
*
* @param Node[] $nodes
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processFileNodes(
array $nodes,
MutatingScope $scope,
callable $nodeCallback,
): ?Dependencies
{
$scope = $scope->toWalkScope();
if (self::$guardNewWorld) {
self::$guardRealExprIds = [];
self::$guardProcessedExprIds = [];
foreach ((new NodeFinder())->findInstanceOf($nodes, Expr::class) as $realExpr) {
self::$guardRealExprIds[spl_object_id($realExpr)] = true;
}
}
$expressionResultStorage = new ExpressionResultStorage();
$scope->pushExpressionResultStorage($expressionResultStorage);
// a fresh walk an extension starts mid-analysis must not feed the
// interrupted walk's gatherer frames (see processStmtNodes())
$gatherers = $this->suspendNodeGatherers();
try {
return $this->statementsHandler->processNodesWithStorage($this, $nodes, $scope, $expressionResultStorage, $nodeCallback);
} finally {
$this->restoreNodeGatherers($gatherers);
$scope->popExpressionResultStorage();
}
}
public function storeExpressionResult(ExpressionResultStorage $storage, Expr $expr, ExpressionResult $expressionResult): void
{
if (self::$guardNewWorld) {
self::$guardProcessedExprIds[spl_object_id($expr)] = true;
}
// handlers are answered from stored results in both worlds
$storage->storeExpressionResult($expr, $expressionResult);
}
/**
* Narrows a scope by a (often synthetic) control-flow condition the new-world
* way: resolve its narrowing through the scope's on-demand dispatcher and apply
* it via applySpecifiedTypes, instead of the old-world filterBy*Value().
*/
public function narrowScopeWithCondition(MutatingScope $scope, Expr $expr, TypeSpecifierContext $context): MutatingScope
{
$specifiedTypes = $scope->specifyTypesOfNewWorldHandlerNode($expr, $context);
return $scope->applySpecifiedTypes($specifiedTypes);
}
/**
* @api
* @param Node\Stmt[] $stmts
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processStmtNodes(
Node $parentNode,
array $stmts,
MutatingScope $scope,
callable $nodeCallback,
StatementContext $context,
): StatementResult
{
// a rule may pass the scope it was handed - the rule-facing NodeCallbackScope -
// as the walk's initial scope; the walk must anchor its results to the
// state-identical MutatingScope or their consumption re-enters the
// rule-facing ask paths
$scope = $scope->toWalkScope();
$storage = new ExpressionResultStorage();
$scope->pushExpressionResultStorage($storage);
// a fresh walk an extension starts mid-analysis must not feed the
// interrupted walk's gatherer frames - they describe the body walk
// that was interrupted, not the nested one
$gatherers = $this->suspendNodeGatherers();
try {
return $this->processStmtNodesInternal(
$parentNode,
$stmts,
$scope,
$storage,
$nodeCallback,
$context,
)->toPublic();
} finally {
$this->restoreNodeGatherers($gatherers);
$scope->popExpressionResultStorage();
}
}
/**
* The final walk of a loop body against its last convergence pass (see
* StatementsHandler::processLoopFinalStmtNodes()).
*
* @param Node\Stmt[] $stmts
* @param callable(Node $node, Scope $scope): void $nodeCallback
* @param array{array<int, array{StatementListWalkState, int}>, RecordingNodeCallback} $lastPass
*/
public function processLoopFinalStmtNodes(
Node $parentNode,
array $stmts,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
StatementContext $context,
array $lastPass,
): InternalStatementResult
{
$pushStorage = $scope->getCurrentExpressionResultStorage() !== $storage;
if ($pushStorage) {
$scope->pushExpressionResultStorage($storage);
}
try {
return $this->statementsHandler->processLoopFinalStmtNodes($this, $parentNode, $stmts, $scope, $storage, $nodeCallback, $context, $lastPass);
} finally {
if ($pushStorage) {
$scope->popExpressionResultStorage();
}
}
}
/**
* One loop convergence pass over $stmts, re-walking only what differs from
* $previous, the previous pass (see StatementsHandler::processLoopPassStmtNodes()).
*
* @param Node\Stmt[] $stmts
* @param callable(Node $node, Scope $scope): void $nodeCallback
* @param array{array<int, array{StatementListWalkState, int}>, ?RecordingNodeCallback}|null $previous
* @param array{array<int, array{StatementListWalkState, int}>, ?RecordingNodeCallback}|null $record
*/
public function processLoopPassStmtNodes(
Node $parentNode,
array $stmts,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
StatementContext $context,
?array $previous,
?array &$record,
): InternalStatementResult
{
$pushStorage = $scope->getCurrentExpressionResultStorage() !== $storage;
if ($pushStorage) {
$scope->pushExpressionResultStorage($storage);
}
try {
return $this->statementsHandler->processLoopPassStmtNodes($this, $parentNode, $stmts, $scope, $storage, $nodeCallback, $context, $previous, $record);
} finally {
if ($pushStorage) {
$scope->popExpressionResultStorage();
}
}
}
/**
* @param Node\Stmt[] $stmts
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processStmtNodesInternal(
Node $parentNode,
array $stmts,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
StatementContext $context,
): InternalStatementResult
{
// make the storage this walk writes into scope-visible: loop-convergence
// passes (including the closure by-ref convergence, which calls this
// method directly) thread a throwaway duplicate that would otherwise
// never reach the storage stack, so every in-pass ask
// (applySpecifiedTypes pricing, rules via Scope::getType) would miss the
// pass's own results and re-process real nodes on demand
$pushStorage = $scope->getCurrentExpressionResultStorage() !== $storage;
if ($pushStorage) {
$scope->pushExpressionResultStorage($storage);
}
try {
return $this->statementsHandler->doProcessStmtNodes($this, $parentNode, $stmts, $scope, $storage, $nodeCallback, $context);
} finally {
if ($pushStorage) {
$scope->popExpressionResultStorage();
}
}
}
/**
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processStmtNode(
Node\Stmt $stmt,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
StatementContext $context,
): InternalStatementResult
{
$overridingThrowPoints = null;
if (
!$stmt instanceof Static_
&& !$stmt instanceof Node\Stmt\Global_
&& !$stmt instanceof Node\Stmt\Property
&& !$stmt instanceof Node\Stmt\ClassConst
&& !$stmt instanceof Node\Stmt\Const_
&& !$stmt instanceof Node\Stmt\ClassLike
&& !$stmt instanceof Node\Stmt\Function_
&& !$stmt instanceof Node\Stmt\ClassMethod
) {
if (!$stmt instanceof Foreach_) {
$scope = $this->statementsHandler->processStmtVarAnnotation($this, $scope, $storage, $stmt, null, $nodeCallback);
}
$overridingThrowPoints = $this->statementsHandler->getOverridingThrowPoints($stmt, $scope);
}
$varTagDependencies = $this->statementsHandler->getVarTagDependencies($stmt, $scope);
if ($stmt instanceof Node\Stmt\ClassMethod) {
// a trait method the using class overrides is not analysed here at all -
// decided before the node callback is emitted; the override can be the
// class's own method, another trait's, or an engine-provided one like
// enum cases(), from() and tryFrom(), so it is told by the declaring
// class of the native method, not by the kind of its method reflection
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
if ($scope->isInTrait()) {
$nativeClassReflection = $scope->getClassReflection()->getNativeReflection();
$methodName = $stmt->name->toString();
if (
$nativeClassReflection->hasMethod($methodName)
&& $nativeClassReflection->getMethod($methodName)->getBetterReflection()->getDeclaringClass()->getName() !== $scope->getTraitReflection()->getName()
) {
return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: [], impurePoints: []);
}
}
}
// Statements whose work is processing their expressions emit their node
// callback AFTER that processing, inside their branches below, with the
// entry scope - a synchronously invoked rule (the plain resolver,
// PHP < 8.1) then finds the expressions' results in the storage instead
// of re-walking them on demand, mirroring processExprNodeInternal().
$deferredStmtCallback = $stmt instanceof Return_ || $stmt instanceof Node\Stmt\Expression || $stmt instanceof Echo_
|| $stmt instanceof If_ || $stmt instanceof Switch_ || $stmt instanceof Foreach_
|| $stmt instanceof Node\Stmt\Unset_ || $stmt instanceof Node\Stmt\ClassConst
|| $stmt instanceof Node\Stmt\Const_ || $stmt instanceof Node\Stmt\While_
|| $stmt instanceof Node\Stmt\Do_;
if (!$deferredStmtCallback) {
$this->callNodeCallback($nodeCallback, $stmt, $scope, $storage);
}
$stmtHandler = StmtHandlerRegistry::resolve($stmt, $this->container);
if ($stmtHandler !== null) {
$stmtResult = $stmtHandler->processStmt($this, $stmt, $scope, $storage, $nodeCallback, $context);
if ($varTagDependencies !== null) {
$this->statementsHandler->emitRedundantVarTagNodes($this, $scope, $storage, $stmt, $nodeCallback);
$stmtResult = $stmtResult->withDependencies(Dependencies::merge($stmtResult->getDependencies(), $varTagDependencies));
}
if ($overridingThrowPoints !== null) {
// the overriding throw points use the scope before the statement,
// so the variable flow throws before the statement does its work
$overridingThrowFlows = [];
foreach ($overridingThrowPoints as $overridingThrowPoint) {
$overridingThrowFlows[] = VariableFlow::throwing($overridingThrowPoint->getType(), true, $overridingThrowPoint->canContainAnyThrowable());
}
return new InternalStatementResult(
$stmtResult->getScope(),
hasYield: $stmtResult->hasYield(),
isAlwaysTerminating: $stmtResult->isAlwaysTerminating(),
exitPoints: $stmtResult->getExitPoints(),
throwPoints: $overridingThrowPoints,
impurePoints: $stmtResult->getImpurePoints(),
endStatements: $stmtResult->getEndStatements(),
variableFlow: VariableFlow::sequence(...$overridingThrowFlows, ...[$stmtResult->getVariableFlow()]),
dependencies: $stmtResult->getDependencies(),
);
}
return $stmtResult;
}
// statements with no analysis of their own (e.g. HaltCompiler)
return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: $overridingThrowPoints ?? [], impurePoints: [], dependencies: $varTagDependencies);
}
public function isAnalysedFile(string $fileName): bool
{
return isset($this->analysedFiles[$fileName]);
}
/** Whether an on-demand walk answers already processed real nodes from their stored results (see processExprOnDemand()). */
public function isReturningStoredExpressionResults(): bool
{
return $this->returnStoredExpressionResults;
}
public function lookForSetAllowedUndefinedExpressions(MutatingScope $scope, Expr $expr): MutatingScope
{
return $this->lookForExpressionCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->setAllowedUndefinedExpression($expr));
}
public function lookForUnsetAllowedUndefinedExpressions(MutatingScope $scope, Expr $expr): MutatingScope
{
return $this->lookForExpressionCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->unsetAllowedUndefinedExpression($expr));
}
/**
* @param Closure(MutatingScope $scope, Expr $expr): MutatingScope $callback
*/
private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Closure $callback): MutatingScope
{
if (!$expr instanceof ArrayDimFetch || $expr->dim !== null) {
$scope = $callback($scope, $expr);
}
if ($expr instanceof ArrayDimFetch) {
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch || $expr instanceof Expr\NullsafeMethodCall) {
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) {
$scope = $this->lookForExpressionCallback($scope, $expr->class, $callback);
} elseif ($expr instanceof List_) {
foreach ($expr->items as $item) {
if ($item === null) {
continue;
}
$scope = $this->lookForExpressionCallback($scope, $item->value, $callback);
}
}
return $scope;
}
/**
* Processes an expression outside the normal AST traversal - e.g. a synthetic
* node a rule or extension asks about. Real AST nodes contained in it return
* their already-stored results instead of being processed again. New results
* are stored into the given storage - pass a duplicate to keep them isolated.
*/
/**
* Processes an expression whose already-walked receiver must be CONSUMED
* from its stored result instead of re-walked: the nullsafe handlers
* process the receiver once (real callbacks) and then walk the plain twin,
* whose receiver answers from storage, re-anchored to the twin's
* (ensured) scope.
*
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processExprNodeConsumingStored(Node\Stmt $stmt, Expr $expr, Expr $consumedExpr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$previous = $this->consumeStoredExpression;
$this->consumeStoredExpression = $consumedExpr;
try {
return $this->processExprNode($stmt, $expr, $scope, $storage, $nodeCallback, $context);
} finally {
$this->consumeStoredExpression = $previous;
}
}
public function processExprOnDemand(Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage): ExpressionResult
{
// A node no handler supports - a virtual node (BooleanOrNode, ...) a
// rule asked the type of - degrades to mixed, mirroring
// MutatingScope::resolveType()'s fallback. The main walk's unhandled
// throw stays: real source nodes must have a handler.
if (
ExprHandlerRegistry::resolve($expr, $this->container) === null
&& !($expr instanceof Expr\CallLike && $expr->isFirstClassCallable())
) {
$mixed = new MixedType();
return $this->expressionResultFactory->create(
$scope,
beforeScope: $scope,
expr: $expr,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
typeCallback: null,
specifyTypesCallback: SpecifiedTypes::emptySpecifyCallback(),
type: $mixed,
nativeType: $mixed,
);
}
// save/restore, never reset: on-demand walks nest (a typeCallback
// evaluated mid-walk prices another synthetic node) and a hard reset
// would turn stored-result consumption off for the rest of the outer
// walk - re-processing every remaining subtree and bypassing the
// closure-argument consume guards in ArgumentsHandler::processArgs()
$previous = $this->returnStoredExpressionResults;
$this->returnStoredExpressionResults = true;
$scope->pushExpressionResultStorage($storage);
try {
return $this->processExprNode(
new Node\Stmt\Expression($expr),
$expr,
$scope,
$storage,
new NoopNodeCallback(),
ExpressionContext::createTopLevel(resolveTemplateArguments: false),
);
} finally {
$scope->popExpressionResultStorage();
$this->returnStoredExpressionResults = $previous;
}
}
/**
* The stored ExpressionResult of a node processExprNode() already processed
* into the given storage - the caller asserts the processing order by
* holding the very storage it processed the node into (a scope-based lookup
* would miss loop-convergence storages, which are never scope-visible).
* Throws when the node has no stored result.
*/
public function readStoredResult(Expr $expr, ExpressionResultStorage $storage): ExpressionResult
{
$result = $storage->findExpressionResult($expr);
if ($result === null) {
throw new ShouldNotHappenException(sprintf(
'%s on line %d has no stored ExpressionResult - it was not processed by processExprNode().',
get_class($expr),
$expr->getStartLine(),
));
}
return $result;
}
/**
* The type, on the given scope, of a node that may or may not have a stored
* ExpressionResult. Every call site of this method is UNDECIDED about whether
* the node was already analysed - each should eventually either consume the
* node's ExpressionResult where it was processed or be a synthetic node
* (processSyntheticOnDemand()).
*/
public function readTypeOfMaybeStored(Expr $expr, MutatingScope $scope): Type
{
$storage = $scope->getCurrentExpressionResultStorage();
$result = $storage !== null ? $storage->findExpressionResult($expr) : null;
if ($result !== null) {
return $result->getTypeOnScope($scope, $scope->nativeTypesPromoted);
}
return $this->readScopeStateOrSyntheticType($expr, $scope);
}
/**
* The type the scope itself knows for the expression, without any node
* processing: a string-named variable read is scope state (mirrors
* VariableHandler's typeCallback), and a type tracked for the whole
* expression answers directly - an on-demand walk would return that very
* holder anyway (the fresh result's beforeScope is the asking scope),
* after paying the walk. Null when the scope has no answer; the caller
* decides whether that means a synthetic walk (processSyntheticOnDemand())
* or an invariant violation.
*/
public function findScopeStateType(Expr $expr, MutatingScope $scope): ?Type
{
if ($expr instanceof Expr\Variable && is_string($expr->name)) {
if ($scope->hasVariableType($expr->name)->no()) {
return new ErrorType();
}
return $scope->getVariableType($expr->name);
}
// a literal is position-independent: the scope prices it without a walk,
// so an argument the walk reaches only later (an IIFE's or a pipe's
// operand) never has to be processed ahead of its turn
if ($expr instanceof Node\Scalar\String_ || $expr instanceof Node\Scalar\Int_ || $expr instanceof Node\Scalar\Float_) {
return $scope->getStateType($expr);
}
// A variable whose name is an expression ($$name) never reaches the read
// above, and the scope tracks it like any other expression - so it belongs
// here rather than falling through to a walk.
if (
!$expr instanceof Expr\Closure
&& !$expr instanceof Expr\ArrowFunction
&& $scope->hasExpressionType($expr)->yes()
) {
return TypeUtils::resolveLateResolvableTypes($scope->getTrackedExpressionType($expr));
}
return null;
}
/**
* The type, on the given scope, of a node the caller knows has no stored
* ExpressionResult in its walk: scope state (variable read / tracked
* holder) answers without a walk, anything else is priced as a synthetic
* node.
*/
public function readScopeStateOrSyntheticType(Expr $expr, MutatingScope $scope): Type
{
return $this->findScopeStateType($expr, $scope) ?? $this->processSyntheticOnDemand($expr, $scope)->getTypeOnScope($scope, $scope->nativeTypesPromoted);
}
/**
* The type the scope knows for an expression the caller has already pinned as
* tracked there (hasExpressionType() yes, or a string-named variable). Unlike
* readScopeStateOrSyntheticType() this never falls back to a synthetic walk -
* the caller decided that the scope answers.
*/
public function requireScopeStateType(Expr $expr, MutatingScope $scope): Type
{
$type = $this->findScopeStateType($expr, $scope);
if ($type === null) {
throw new ShouldNotHappenException(sprintf(
'%s on line %d is not tracked on the scope it was pinned as tracked on.',
get_class($expr),
$expr->getStartLine(),
));
}
return $type;
}
/**
* Fires the PHPSTAN_GUARD_NW diagnostic when a real (non-synthetic) AST node
* reaches an on-demand pricing path without having been processed and stored
* by processExprNode() first. Mirrors the guard in MutatingScope::getType():
* such a node should be answered from its stored ExpressionResult, never
* re-priced as if it were synthetic. Dormant unless PHPSTAN_GUARD_NW=1.
*/
private function guardAgainstUnprocessedRealNode(Expr $expr, string $caller): void
{
if (
!self::$guardNewWorld
|| !isset(self::$guardRealExprIds[spl_object_id($expr)])
|| isset(self::$guardProcessedExprIds[spl_object_id($expr)])
) {
return;
}
throw new ShouldNotHappenException(sprintf(
'%s() asked about non-synthetic %s on line %d before it was processed by processExprNode() - it should consume the node\'s ExpressionResult instead.',
$caller,
get_class($expr),
$expr->getStartLine(),
));
}
/**
* Processes a synthetic node (one an ExprHandler built itself) on a duplicate
* of the storage of the analysis currently in progress, mirroring
* MutatingScope::resolveTypeOfNewWorldHandlerNode(): the duplicate isolates
* the synthetic node's own stored result from the live storage while its real
* subnodes still resolve from the fallback.
*/
public function processSyntheticOnDemand(Expr $expr, MutatingScope $scope): ExpressionResult
{
$this->guardAgainstUnprocessedRealNode($expr, __FUNCTION__);
$current = $scope->getCurrentExpressionResultStorage() ?? new ExpressionResultStorage();
return $this->processExprOnDemand($expr, $scope, $current->duplicate());
}
/**
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function processExprNode(
Node\Stmt $stmt,
Expr $expr,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
ExpressionContext $context,
): ExpressionResult
{
$consume = $this->consumeStoredExpression === $expr;
if ($this->returnStoredExpressionResults || $consume) {
$storedResult = $storage->findExpressionResult($expr);
// a stored result only answers when the current scope agrees with its
// evaluation position on the variables the expression reads - a
// counterfactual walk (an extension re-binding a variable and pricing
// a real subtree, e.g. array_filter's per-element callback evaluation)
// re-processes the node on its own scope instead. In CONSUME mode the
// divergence is intentional (an ensured-non-null device) and the
// stored result is consumed unconditionally, re-anchored below.
if ($storedResult !== null && ($consume || $storedResult->askScopeVariableStateMatches($scope, $scope->nativeTypesPromoted))) {
// a foreign-position answer must not thread its original walk
// scopes into THIS walk - re-anchor it to the asking position so
// subsequent operands keep evaluating on the asking scope
if ($storedResult->getBeforeScope() === $scope) {
return $storedResult;
}
$reanchored = $storedResult->atAskPosition($scope);
if ($consume) {
// the re-anchored view IS this walk's result for the node
// (the nullsafe twin's receiver at the ensured position) -
// store it so later asks (rules' storage reads) see the same
// result the twin walk itself consumed, exactly like the
// receiver walked inside the twin used to be stored
$this->storeExpressionResult($storage, $expr, $reanchored);
}
return $reanchored;
}
}
return $this->processExprNodeInternal($stmt, $expr, $scope, $storage, $nodeCallback, $context);
}
/**
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
private function processExprNodeInternal(
Node\Stmt $stmt,
Expr $expr,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
ExpressionContext $context,
): ExpressionResult
{
if ($expr instanceof Expr\CallLike && $expr->isFirstClassCallable()) {
if ($expr instanceof FuncCall) {
$newExpr = new FunctionCallableNode($expr->name, $expr);
} elseif ($expr instanceof MethodCall) {
$newExpr = new MethodCallableNode($expr->var, $expr->name, $expr);
} elseif ($expr instanceof StaticCall) {
$newExpr = new StaticMethodCallableNode($expr->class, $expr->name, $expr);
} elseif ($expr instanceof New_ && !$expr->class instanceof Class_) {
$newExpr = new InstantiationCallableNode($expr->class, $expr);
} else {
throw new ShouldNotHappenException();
}
$newExprResult = $this->processExprNode($stmt, $newExpr, $scope, $storage, $nodeCallback, $context);
$expressionResult = $this->expressionResultFactory->create(
$newExprResult->getScope(),
beforeScope: $scope,
expr: $expr,
hasYield: $newExprResult->hasYield(),
isAlwaysTerminating: $newExprResult->isAlwaysTerminating(),
throwPoints: $newExprResult->getThrowPoints(),
impurePoints: $newExprResult->getImpurePoints(),
variableFlow: $newExprResult->getVariableFlow(),
// the first-class callable closure type lives on the *CallableNode
// result; delegate so getType() of the original CallLike answers from it
typeCallback: static fn (bool $nativeTypesPromoted): Type => ($nativeTypesPromoted ? $newExprResult->getNativeType() : $newExprResult->getType()),
specifyTypesCallback: SpecifiedTypes::emptySpecifyCallback(),
dependencies: $newExprResult->getDependencies(),
);
$this->storeExpressionResult($storage, $expr, $expressionResult);
return $expressionResult;
}
$exprHandler = ExprHandlerRegistry::resolve($expr, $this->container);
if ($exprHandler !== null) {
$expressionResult = $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context);
// a chain link an enclosing isset/empty/?? could not device ahead
// of its walk (an untracked call) is deviced now, from the type
// the walk produced
$expressionResult = $this->getNonNullabilityHelper()->applyPendingEnsure($expr, $expressionResult);
$this->storeExpressionResult($storage, $expr, $expressionResult);
// Force potential producers before collecting the body's constraints.
// Type reads only construct markers; they never register sites as a side effect.
$frame = $scope->getCurrentTemplateArgumentFrame();
if (
$frame !== null && $frame->isObserving()
&& $expr instanceof Expr\CallLike
) {
$constraints = $this->templateArgumentObserver->collectSites($expressionResult->getType());
$expressionResult = $expressionResult->withScope($expressionResult->getScope()->addTemplateArgumentConstraints($constraints));
$this->storeExpressionResult($storage, $expr, $expressionResult);
}
// The node's own callback fires AFTER its result is stored, with the
// scope captured before processing. Rules observe the same (scope,
// answer) pair as at a pre-order emission - previously a pre-order
// rule parks on its first ask and resumes at this store anyway - but
// a synchronously invoked rule (the plain resolver, PHP < 8.1) now
// finds the node's and its subtree's results in the storage instead
// of re-walking them on demand.
$this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $storage, $context);
// the call is now processed and stored; emit a virtual node so
// impossible-check rules read its specified types from the result
// instead of asking the scope before the call node is processed
if ($expr instanceof FuncCall) {
$this->callNodeCallbackWithExpression($nodeCallback, new FunctionCallExpressionNode($expr, $expressionResult, $expressionResult->getArgsResult()), $scope, $storage, $context);
} elseif ($expr instanceof MethodCall) {
$this->callNodeCallbackWithExpression($nodeCallback, new MethodCallExpressionNode($expr, $expressionResult, $expressionResult->getArgsResult()), $scope, $storage, $context);
} elseif ($expr instanceof StaticCall) {
$this->callNodeCallbackWithExpression($nodeCallback, new StaticMethodCallExpressionNode($expr, $expressionResult, $expressionResult->getArgsResult()), $scope, $storage, $context);
}
return $expressionResult;
}
throw new ShouldNotHappenException(sprintf('Unhandled expr: %s', get_class($expr)));
}
/**
* Unlike a method call, a property read defaults to pure: only a hook we're
* certain about and that is certainly side-effecting makes the read impure.
*
* The reset is assumed pure as reporting those would make accessing them
* unreasonably annoying.
*
* @param 'get'|'set' $hookName
* @return ImpurePoint[]
*/
public function getImpurePointsFromPropertyHook(
MutatingScope $scope,
PropertyFetch $propertyFetch,
PhpPropertyReflection $propertyReflection,
string $hookName,
): array
{
if ($this->isPropertyHookBackingValueAccess($scope, $propertyFetch)) {
return [];
}
if (!$propertyReflection->hasHook($hookName)) {
return [];
}
if (!$propertyReflection->getHook($hookName)->hasSideEffects()->yes()) {
return [];
}
return [
new ImpurePoint(
$scope,
$propertyFetch,
'propertyHookCall',
sprintf(
'call to %s hook of property %s::$%s',
$hookName,
$propertyReflection->getDeclaringClass()->getDisplayName(),
$propertyReflection->getName(),
),
true,
),
];
}
/**
* Inside a hook of the same property, $this->prop is the backing value, not
* a re-entrant hook call.
*/
private function isPropertyHookBackingValueAccess(MutatingScope $scope, PropertyFetch $propertyFetch): bool
{
$scopeFunction = $scope->getFunction();
return $scopeFunction instanceof PhpMethodFromParserNodeReflection
&& $scopeFunction->isPropertyHook()
&& $propertyFetch->var instanceof Variable
&& $propertyFetch->var->name === 'this'
&& $propertyFetch->name instanceof Identifier
&& $propertyFetch->name->toString() === $scopeFunction->getHookedPropertyName();
}
/**
* @return string[]
*/
public function getAssignedVariables(Expr $expr): array
{
if ($expr instanceof Expr\Variable) {
if (is_string($expr->name)) {
return [$expr->name];
}
return [];
}
if ($expr instanceof Expr\List_) {
$names = [];
foreach ($expr->items as $item) {
if ($item === null) {
continue;
}
$names = array_merge($names, $this->getAssignedVariables($item->value));
}
return $names;
}
if ($expr instanceof ArrayDimFetch) {
return $this->getAssignedVariables($expr->var);
}
return [];
}
private const REPLAYABLE_BODY_ATTRIBUTE = 'convergenceReplayableBody';
private const CONTEXT_SENSITIVE_STATEMENT_ATTRIBUTE = 'convergenceContextSensitiveStatement';
/**
* Whether a recorded convergence pass over the loop body can replace the
* final walk. A pass runs at deep statement context, the final walk at top
* level - constructs that analyse differently between the two (nested
* loop/label fixpoints run only at top level, statement-level classes are
* skipped at deep context) disqualify the body. Closure bodies process
* context-independently and are not traversed.
*