Repository navigation
Expand file tree
/
Copy pathFuncCallHandler.php
More file actions
1311 lines (1203 loc) · 53.8 KB
/
Copy pathFuncCallHandler.php
File metadata and controls
1311 lines (1203 loc) · 53.8 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\ExprHandler;
use Closure;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ArgsResult;
use PHPStan\Analyser\ArgumentsHandler;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\ClosureProcessor;
use PHPStan\Analyser\ConditionalTypeResolver;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultFactory;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\ExprHandler\Helper\ClosureTypeResolver;
use PHPStan\Analyser\ExprHandler\Helper\DefaultNarrowingHelper;
use PHPStan\Analyser\ExprHandler\Helper\DynamicReturnTypeStoragePrimer;
use PHPStan\Analyser\ExprHandler\Helper\EarlyTerminatingCallHelper;
use PHPStan\Analyser\ExprHandler\Helper\FuncCallScopeEffectsHelper;
use PHPStan\Analyser\Generics\ClosureSignatureInference;
use PHPStan\Analyser\Generics\TemplateArgumentFrame;
use PHPStan\Analyser\ImpurePoint;
use PHPStan\Analyser\InternalThrowPoint;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Analyser\VariableFlow;
use PHPStan\Analyser\VariableFlowBuilder;
use PHPStan\Dependency\Dependencies;
use PHPStan\Dependency\DependencyTypes;
use PHPStan\DependencyInjection\AutowiredExtensions;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\ExtensionsCollection;
use PHPStan\Node\ClosureReturnStatementsNode;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Parser\ImmediatelyInvokedClosureVisitor;
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\ExtendedParametersAcceptor;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Comparison\ImpossibleCheckTypeHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\DynamicReturnTypeExtensionRegistry;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
use PHPStan\Type\Generic\UnresolvedTemplateArgumentType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Throwable;
use WeakReference;
use function array_key_first;
use function array_keys;
use function array_map;
use function array_merge;
use function count;
use function in_array;
use function is_string;
/**
* @implements ExprHandler<FuncCall>
*/
#[AutowiredService]
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/FuncCallHandler.cpp')]
final class FuncCallHandler implements ExprHandler
{
/**
* @param ExtensionsCollection<DynamicFunctionThrowTypeExtension> $dynamicFunctionThrowTypeExtensions
*/
public function __construct(
private ReflectionProvider $reflectionProvider,
#[AutowiredExtensions(of: DynamicFunctionThrowTypeExtension::class)]
private ExtensionsCollection $dynamicFunctionThrowTypeExtensions,
private DynamicReturnTypeExtensionRegistry $dynamicReturnTypeExtensionRegistry,
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
private bool $implicitThrows,
#[AutowiredParameter]
private bool $rememberPossiblyImpureFunctionValues,
private FuncCallScopeEffectsHelper $scopeEffectsHelper,
private ExpressionResultFactory $expressionResultFactory,
private TypeSpecifier $typeSpecifier,
private DefaultNarrowingHelper $defaultNarrowingHelper,
private EarlyTerminatingCallHelper $earlyTerminatingHelper,
private DynamicReturnTypeStoragePrimer $storagePrimer,
private ImpossibleCheckTypeHelper $impossibleCheckTypeHelper,
private ClosureTypeResolver $closureTypeResolver,
private ArgumentsHandler $argumentsHandler,
private ClosureProcessor $closureProcessor,
private AssignHandler $assignHandler,
private ClosureSignatureInference $closureSignatureInference,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof FuncCall && !$expr->isPartialFunctionApplication();
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$beforeScope = $scope;
$parametersAcceptor = null;
$nameType = null;
$variants = [];
$namedArgumentsVariants = null;
$functionReflection = null;
$nameResult = null;
$throwPoints = [];
$impurePoints = [];
// A call configured as early-terminating never returns: give it an explicit
// never so the statement's exit point follows from the result type, instead of
// NodeScopeResolver re-deriving it via Scope::getType().
$isEarlyTerminating = $expr->name instanceof Name
&& $this->earlyTerminatingHelper->isEarlyTerminatingFunctionCall($expr->name->toString());
$hasYield = false;
$isAlwaysTerminating = $isEarlyTerminating;
$argumentsWalkedAhead = null;
if (
($expr->name instanceof Expr\Closure || $expr->name instanceof Expr\ArrowFunction)
&& $expr->name->getAttribute(ImmediatelyInvokedClosureVisitor::ARGS_ATTRIBUTE_NAME) !== null
) {
// An immediately invoked closure's untyped parameters take the types
// of its invocation arguments, so the arguments are walked BEFORE the
// callee - on the closure's declared signature - and the closure body
// consumes their stored results instead of pricing them ahead of
// their turn. The closure is then walked on the post-argument scope.
$shallowVariants = $this->closureTypeResolver->getDeclaredClosureType($scope, $expr->name)->getCallableParametersAcceptors($scope);
$shallowAcceptor = ParametersAcceptorSelector::combineVariantsForNormalization($expr->getArgs(), $shallowVariants, null);
$argumentsWalkedAhead = $this->argumentsHandler->processArgs($nodeScopeResolver, $stmt, null, null, $shallowVariants, null, ArgumentsNormalizer::reorderFuncArguments($shallowAcceptor, $expr) ?? $expr, $scope, $storage, $nodeCallback, $context);
$scope = $argumentsWalkedAhead->getScope();
}
if ($expr->name instanceof Expr) {
// process the dynamic callee name first, then consume its type (single-pass
// inside-out) rather than reading it before processExprNode() stores it
$nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep());
$nameType = $nameResult->getType();
if (!$nameType->isCallable()->no()) {
$variants = $nameType->getCallableParametersAcceptors($scope);
// A structural acceptor (names/positions/variadic) drives the per-arg
// metadata and the throw/impure points - generics are resolved
// type-driven by processArgs() into $resolvedParametersAcceptor.
$parametersAcceptor = ParametersAcceptorSelector::combineVariantsForNormalization($expr->getArgs(), $variants, null);
}
$scope = $nameResult->getScope();
$hasYield = $nameResult->hasYield();
$throwPoints = $nameResult->getThrowPoints();
$impurePoints = $nameResult->getImpurePoints();
$isAlwaysTerminating = $nameResult->isAlwaysTerminating();
if ( // phpcs:ignore
$nameType->isObject()->yes()
&& $nameType->isCallable()->yes()
&& (new ObjectType(Closure::class))->isSuperTypeOf($nameType)->no()
) {
// processed later
} elseif ($parametersAcceptor instanceof CallableParametersAcceptor) {
foreach ($parametersAcceptor->getThrowPoints() as $throwPoint) {
$isExplicit = $throwPoint->isExplicit();
if (!$this->implicitThrows && !$isExplicit) {
continue;
}
if ($isExplicit) {
$throwPoints[] = InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable());
} else {
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
}
}
// A callable value's impure points are resolved from its
// ParametersAcceptor alone, so a conditionally pure function reached
// through a first-class callable arrives here as an unconditional
// "possibly impure" point. The arguments are known at this call site,
// so settle the verdict here.
$callableImpurePoints = SimpleImpurePoint::narrowByConditionalPurity($parametersAcceptor->getImpurePoints(), $parametersAcceptor, $scope, $expr->getArgs());
$impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $expr, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $callableImpurePoints));
$scope = $this->closureProcessor->processImmediatelyCalledCallable($scope, $parametersAcceptor->getInvalidateExpressions(), $parametersAcceptor->getUsedVariables());
}
} elseif ($this->reflectionProvider->hasFunction($expr->name, $scope)) {
$functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope);
$variants = $functionReflection->getVariants();
$namedArgumentsVariants = $functionReflection->getNamedArgumentsVariants();
// A structural acceptor (names/positions/variadic) drives argument
// normalization, the impure point and the throw points - generics are
// resolved type-driven by processArgs() into $resolvedParametersAcceptor.
$parametersAcceptor = ParametersAcceptorSelector::combineVariantsForNormalization($expr->getArgs(), $variants, $namedArgumentsVariants);
} else {
$impurePoints[] = new ImpurePoint(
$scope,
$expr,
'functionCall',
'call to unknown function',
false,
);
}
$normalizedExpr = $expr;
if ($parametersAcceptor !== null) {
$normalizedExpr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
$returnType = $parametersAcceptor->getReturnType();
$isAlwaysTerminating = $isAlwaysTerminating || $returnType instanceof NeverType && $returnType->isExplicit();
}
if (
$normalizedExpr->name instanceof Name
&& $functionReflection !== null
&& $functionReflection->getName() === 'clone'
&& count($normalizedExpr->getArgs()) === 2
) {
// process the clone arguments as reads so the cloned object and the
// properties array resolve from stored results instead of unprocessed
// nodes; processArgs() below processes them again as clone()'s arguments,
// so the NoopNodeCallback here avoids duplicate node-callbacks.
$cloneObjectArgResult = $nodeScopeResolver->processExprNode($stmt, $normalizedExpr->getArgs()[0]->value, $scope, $storage, new NoopNodeCallback(), $context->enterDeep()->withoutTemplateArgumentResolution());
$clonePropertiesArgResult = $nodeScopeResolver->processExprNode($stmt, $normalizedExpr->getArgs()[1]->value, $scope, $storage, new NoopNodeCallback(), $context->enterDeep()->withoutTemplateArgumentResolution());
$clonePropertiesArgType = $clonePropertiesArgResult->getType();
// the cloned type is composed from the object argument's result -
// no synthetic Clone_ walk
$cloneExpr = new TypeExpr(CloneHandler::resolveCloneType($cloneObjectArgResult->getType()));
$clonePropertiesArgTypeConstantArrays = $clonePropertiesArgType->getConstantArrays();
foreach ($clonePropertiesArgTypeConstantArrays as $clonePropertiesArgTypeConstantArray) {
foreach ($clonePropertiesArgTypeConstantArray->getKeyTypes() as $i => $clonePropertyKeyType) {
$clonePropertyKeyTypeScalars = $clonePropertyKeyType->getConstantScalarValues();
$propertyAttributes = $normalizedExpr->getAttributes();
unset($propertyAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
$propertyAttributes['inCloneWith'] = true;
if (count($clonePropertyKeyTypeScalars) === 1) {
$this->assignHandler->processVirtualAssign(
$nodeScopeResolver,
$scope,
$storage,
$stmt,
new PropertyFetch($cloneExpr, (string) $clonePropertyKeyTypeScalars[0], $propertyAttributes),
new TypeExpr($clonePropertiesArgTypeConstantArray->getValueTypes()[$i]),
$nodeCallback,
);
continue;
}
$this->assignHandler->processVirtualAssign(
$nodeScopeResolver,
$scope,
$storage,
$stmt,
new PropertyFetch($cloneExpr, new TypeExpr($clonePropertyKeyType), $propertyAttributes),
new TypeExpr($clonePropertiesArgTypeConstantArray->getValueTypes()[$i]),
$nodeCallback,
);
}
}
}
/** @var array{Type, Type}|null $arrayWalkValueTypes */
$arrayWalkValueTypes = null;
$arrayWalkArrayArg = null;
$argsGatherer = null;
if (
$functionReflection !== null
&& $functionReflection->getName() === 'array_walk'
&& count($normalizedExpr->getArgs()) >= 2
) {
$callbackArg = $normalizedExpr->getArgs()[1]->value;
$firstParamName = null;
if (
$callbackArg instanceof Expr\Closure
&& isset($callbackArg->params[0])
&& $callbackArg->params[0]->byRef
&& $callbackArg->params[0]->var instanceof Variable
&& is_string($callbackArg->params[0]->var->name)
) {
$firstParamName = $callbackArg->params[0]->var->name;
}
if ($firstParamName !== null) {
$arrayWalkArrayArg = $normalizedExpr->getArgs()[0]->value;
$argsGatherer = static function (Node $node, Scope $scope) use ($callbackArg, $firstParamName, &$arrayWalkValueTypes): void {
if (!($node instanceof ClosureReturnStatementsNode) || $node->getClosureExpr() !== $callbackArg) {
return;
}
$types = [];
$nativeTypes = [];
$stmtResult = $node->getStatementResult();
foreach ($stmtResult->getExitPoints() as $exitPoint) {
$exitScope = $exitPoint->getScope();
if (!$exitScope->hasVariableType($firstParamName)->yes()) {
continue;
}
$types[] = $exitScope->getVariableType($firstParamName);
$nativeTypes[] = $exitScope->toWalkScope()->doNotTreatPhpDocTypesAsCertain()->getVariableType($firstParamName);
}
if (!$stmtResult->isAlwaysTerminating()) {
$stmtScope = $stmtResult->getScope();
if ($stmtScope->hasVariableType($firstParamName)->yes()) {
$types[] = $stmtScope->getVariableType($firstParamName);
$nativeTypes[] = $stmtScope->toWalkScope()->doNotTreatPhpDocTypesAsCertain()->getVariableType($firstParamName);
}
}
if (count($types) <= 0) {
return;
}
$arrayWalkValueTypes = [
TypeCombinator::union(...$types),
TypeCombinator::union(...$nativeTypes),
];
};
}
}
$scopeBeforeArgs = $scope;
if ($parametersAcceptor !== null && $context->getInAssignRightSideExpr() === $expr) {
$context = $context->enterAssignRightSideCallArgs($parametersAcceptor);
}
if ($argsGatherer !== null) {
$nodeScopeResolver->pushNodeGatherer($argsGatherer);
}
try {
$argsResult = $argumentsWalkedAhead !== null
// the arguments are processed; the call resolves from the walked
// closure's acceptor, which is the sole variant of its type
? $argumentsWalkedAhead->withResolvedParametersAcceptor($parametersAcceptor)
: $this->argumentsHandler->processArgs($nodeScopeResolver, $stmt, $functionReflection, null, $variants, $namedArgumentsVariants, $normalizedExpr, $scope, $storage, $nodeCallback, $context);
} finally {
if ($argsGatherer !== null) {
$nodeScopeResolver->popNodeGatherer();
}
}
$resolvedParametersAcceptor = $argsResult->getResolvedParametersAcceptor();
if (
$resolvedParametersAcceptor !== null
&& $nameResult !== null
&& $expr->name instanceof Expr\Variable
&& is_string($expr->name->name)
) {
$invokedClosureType = $this->resolveInvokedClosureType($nodeScopeResolver, $normalizedExpr, $expr->name->name, $nameResult->getType(), $argsResult->getScope(), $storage);
if ($invokedClosureType !== null) {
$resolvedParametersAcceptor = $invokedClosureType->getCallableParametersAcceptors($scope)[0];
}
}
// arguments walked ahead of the callee: the scope already carries them
// and the callee walk's own effects (a closure's by-ref uses) on top
if ($argumentsWalkedAhead === null) {
$scope = $argsResult->getScope();
}
$this->argumentsHandler->processDroppedArgs($nodeScopeResolver, $stmt, $expr, $normalizedExpr, $scope, $storage, $context);
$hasYield = $hasYield || $argsResult->hasYield();
$throwPoints = array_merge($throwPoints, $argsResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $argsResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $argsResult->isAlwaysTerminating();
if ($functionReflection !== null) {
// created after the args were processed - the side-effect flip
// parameters (print_r's $return, ...) read an argument's type, which
// is only available once its result is stored
$impurePoint = SimpleImpurePoint::createFromVariant($functionReflection, $parametersAcceptor, $scope, $expr->getArgs());
if ($impurePoint !== null) {
$impurePoints[] = new ImpurePoint($scopeBeforeArgs, $expr, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain());
}
}
if ($arrayWalkValueTypes !== null && $arrayWalkArrayArg !== null) {
$scope = $this->scopeEffectsHelper->applyArrayWalkResult($nodeScopeResolver, $stmt, $arrayWalkArrayArg, $arrayWalkValueTypes, $argsResult, $scope, $storage, $nodeCallback);
}
// The return type is derived from $resolvedParametersAcceptor - the acceptor
// processArgs() selected from the arg types gathered on the arg-to-arg
// evolving scope (type-driven, generics resolved). When null
// (native-types-promoted, on-demand / synthetic pricing, or special cases
// inside resolveReturnType), the acceptor is re-derived from the
// already-processed argument results on the asking scope.
$storageRef = WeakReference::create($storage);
$typeCallback = $isEarlyTerminating
? static fn (bool $nativeTypesPromoted): Type => new NeverType(true)
: function (bool $nativeTypesPromoted) use ($nodeScopeResolver, $beforeScope, $expr, $nameResult, $resolvedParametersAcceptor, $argsResult, $storageRef): Type {
// for always-true/always-false type checks the call's own narrowing
// (already produced as this result's specifyTypesCallback) decides
// the return type - the verdict is a read of that narrowing, not a
// second derivation. The result is looked up through a weak storage
// reference: this callback is owned by that very result, and a
// strong backedge would be a cycle (PHPStan runs with gc_disable()).
if (
!$nativeTypesPromoted
&& $expr->name instanceof Name
&& in_array($expr->name->toLowerString(), ['array_key_exists', 'key_exists', 'in_array', 'is_subclass_of'], true)
) {
$callStorage = $storageRef->get();
$callResult = $callStorage?->findExpressionResult($expr);
if ($callResult !== null) {
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($beforeScope, $expr, $callResult, $argsResult);
if ($isAlways !== null) {
return new ConstantBooleanType($isAlways);
}
}
}
return $this->resolveReturnType(
$nodeScopeResolver,
$beforeScope,
$nativeTypesPromoted,
$expr,
$nameResult,
$nativeTypesPromoted ? null : $resolvedParametersAcceptor,
$argsResult,
);
};
$specifyTypesCallback = fn (TypeSpecifierContext $specifyContext, bool $nativeTypesPromoted): SpecifiedTypes => $this->specifyTypes(
$nativeTypesPromoted ? $beforeScope->doNotTreatPhpDocTypesAsCertain() : $beforeScope,
$expr,
$normalizedExpr,
$nameResult,
$resolvedParametersAcceptor,
$specifyContext,
$argsResult,
);
// A type constraint on a (narrowable, i.e. non-side-effecting, non-first-class)
// function call narrows the call itself - the inside-out equivalent of
// createForExpr's FuncCall purity gate + tail entry. An impure call narrows to
// nothing.
$createTypesCallback = function (Type $type, TypeSpecifierContext $createContext, bool $nativeTypesPromoted) use ($expr, $normalizedExpr, $nameResult, $beforeScope, $argsResult): SpecifiedTypes {
$s = $nativeTypesPromoted ? $beforeScope->doNotTreatPhpDocTypesAsCertain() : $beforeScope;
if (!$this->isFuncCallNarrowable($s, $expr, $nameResult)) {
return new SpecifiedTypes([], []);
}
$types = $this->defaultNarrowingHelper->createSubjectTypes($s, $expr, null, $type, $createContext);
// array_key_first/array_key_last/array_find_key return null iff the
// array has no matching key - a null constraint on the call narrows
// the array argument (both directions for first/last, non-empty only
// for find_key: an empty result does not mean an empty array)
if (
$expr->name instanceof Name
&& !$expr->isPartialFunctionApplication()
&& isset($normalizedExpr->getArgs()[0])
&& $type->isNull()->yes()
) {
$funcName = $expr->name->toLowerString();
$bothDirections = in_array($funcName, ['array_key_first', 'array_key_last'], true);
if ($bothDirections || $funcName === 'array_find_key') {
// the normalized arguments are the ones processArgs() walked
$argExpr = $normalizedExpr->getArgs()[0]->value;
$argType = $argsResult->requireArgResult($argExpr)->getTypeOnScope($s, $s->nativeTypesPromoted);
if ($argType->isArray()->yes() && ($bothDirections || $createContext->falsey())) {
$types = $types->unionWith(
$this->defaultNarrowingHelper->createForSubject($argExpr, new NonEmptyArrayType(), $createContext->negate(), $s),
);
}
}
}
return $types;
};
// Store a preliminary result carrying the type/specify callbacks before the
// throw-point return type is computed: getFunctionThrowPoint() resolves the
// return type through the typeCallback, whose type-check verdict reads this
// very result's narrowing, and dynamic return type extensions may ask about
// the call too. Without a stored result those asks would re-process this
// FuncCall on demand and recurse back into getFunctionThrowPoint(). The
// callbacks are scope-independent, so the preliminary result answers those
// asks correctly; finalize() below completes it with the resolved scope and
// throw/impure points.
$preliminaryResult = $this->expressionResultFactory->create(
$scope,
beforeScope: $beforeScope,
expr: $expr,
hasYield: $hasYield,
isAlwaysTerminating: $isAlwaysTerminating,
throwPoints: [],
impurePoints: [],
typeCallback: $typeCallback,
specifyTypesCallback: $specifyTypesCallback,
createTypesCallback: $createTypesCallback,
argsResult: $argsResult,
);
$nodeScopeResolver->storeExpressionResult($storage, $expr, $preliminaryResult);
if ($normalizedExpr->name instanceof Expr) {
$nameType = $nodeScopeResolver->readStoredResult($normalizedExpr->name, $storage)->getTypeOnScope($scope, false);
if (
$nameType->isObject()->yes()
&& $nameType->isCallable()->yes()
&& (new ObjectType(Closure::class))->isSuperTypeOf($nameType)->no()
) {
$invokeAttributes = $normalizedExpr->getAttributes();
unset($invokeAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
$invokeResult = $nodeScopeResolver->processExprNode(
$stmt,
new MethodCall($normalizedExpr->name, '__invoke', $normalizedExpr->getArgs(), $invokeAttributes),
$scope,
$storage,
new NoopNodeCallback(),
$context->enterDeep(),
);
$throwPoints = array_merge($throwPoints, $invokeResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $invokeResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $invokeResult->isAlwaysTerminating();
}
}
if ($functionReflection !== null) {
// The call's return type, computed from the already-processed argument
// results (resolveReturnType reads them from the stored results,
// never re-running processArgs) - asking Scope::getType() for the
// FuncCall here would re-enter this handler on demand, as its result is
// not stored yet.
// Resolve it through the stored preliminary result so the memoized
// value seeds the final result below - the first later type read
// would otherwise run resolveReturnType() again.
$returnType = $preliminaryResult->getKeepVoidType(false);
// The early structural check above (line ~180) only sees the unresolved
// acceptor return type; a conditional-return never (e.g.
// `($x is Foo ? never : string)`) only resolves to never once the actual
// argument types are folded in by the type-driven resolved acceptor. Read
// it from that acceptor's return type, not resolveReturnType(), which
// folds in call_user_func()/dynamic-extension special cases that must not
// make the call itself always-terminating (e.g.
// `call_user_func(fn() => exit())`).
if ($resolvedParametersAcceptor !== null) {
$resolvedReturnType = $resolvedParametersAcceptor->getReturnType();
$isAlwaysTerminating = $isAlwaysTerminating || ($resolvedReturnType instanceof NeverType && $resolvedReturnType->isExplicit());
}
$functionThrowPoint = $this->getFunctionThrowPoint($functionReflection, $parametersAcceptor, $returnType, $normalizedExpr, $scope, $context);
if ($functionThrowPoint !== null) {
$throwPoints[] = $functionThrowPoint;
}
} else {
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
}
$byRefWrittenNames = [];
if ($nameType !== null) {
if ($nodeScopeResolver->observingTemplateArgumentFrame($scope) !== null) {
$scope = $scope->addTemplateArgumentConstraints(ClosureSignatureInference::collectInvokedCallee($nameType));
}
[$scope, $byRefThrowPoints, $byRefWrittenNames] = $this->processByRefInvocations($nodeScopeResolver, $normalizedExpr, $nameType, $scope, $storage);
$throwPoints = array_merge($throwPoints, $byRefThrowPoints);
}
$scope = $this->scopeEffectsHelper->applyCallScopeEffects($nodeScopeResolver, $stmt, $normalizedExpr, $functionReflection, $parametersAcceptor, $argsResult, $scope, $scopeBeforeArgs, $storage, $nodeCallback);
// the invoked closure reads and writes its by-ref variables here
$byRefFlows = [];
foreach ($byRefWrittenNames as $byRefWrittenName) {
$byRefFlows[] = VariableFlow::escape($byRefWrittenName);
}
$byRefFlow = $byRefFlows === [] ? null : VariableFlow::sequence(...$byRefFlows);
$variableFlow = VariableFlow::sequence(
$nameResult?->getVariableFlow(),
VariableFlowBuilder::arguments($expr, $argsResult, $storage),
self::getCallVariableFlow($functionReflection?->getName(), $normalizedExpr, $argsResult, $scope),
VariableFlowBuilder::throws($expr, $throwPoints),
$byRefFlow,
$isAlwaysTerminating ? VariableFlow::exit(VariableFlow::STOP) : null,
);
$dependencies = Dependencies::merge(
$nameResult?->getDependencies(),
$argsResult->getDependencies(),
$this->getDependencies($beforeScope, $functionReflection, $nameResult, $preliminaryResult),
);
return $preliminaryResult->finalize($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints, $variableFlow, $dependencies);
}
/**
* Invoking a closure whose by-ref uses are followed to where it runs (see
* ClosureSignatureInference): the by-ref variables take the body's exit
* types here. Observing, the invocation's state joins the closure's entry
* and the effects are the fixpoint from here, containing whatever the
* second pass computes; there, a closure whose every invocation was seen
* runs once, an escaped one converges again.
*
* @return array{MutatingScope, list<InternalThrowPoint>, list<string>}
*/
private function processByRefInvocations(NodeScopeResolver $nodeScopeResolver, FuncCall $call, Type $calleeType, MutatingScope $scope, ExpressionResultStorage $storage): array
{
$members = $calleeType instanceof UnionType ? $calleeType->getTypes() : [$calleeType];
$closureTypes = [];
$hasOtherMember = false;
foreach ($members as $member) {
if (!$member instanceof ClosureType) {
$hasOtherMember = true;
continue;
}
$byRefUseTypes = $member->getByRefUseTypes();
$marker = $byRefUseTypes[array_key_first($byRefUseTypes) ?? ''] ?? null;
if (!$marker instanceof UnresolvedTemplateArgumentType || !$marker->getSite() instanceof Expr\Closure) {
$hasOtherMember = true;
continue;
}
$closureTypes[] = [$member, $marker->getSite()];
}
if ($closureTypes === []) {
return [$scope, [], []];
}
$observing = $nodeScopeResolver->observingTemplateArgumentFrame($scope) !== null;
$argumentTypes = [];
foreach ($call->getArgs() as $i => $arg) {
if ($arg->name !== null || $arg->unpack) {
break;
}
$argumentTypes[$i] = $nodeScopeResolver->readTypeOfMaybeStored($arg->value, $scope);
}
$invocations = [];
$byRefNames = [];
foreach ($closureTypes as [$closureType, $site]) {
$mode = $observing ? null : $this->closureSignatureInference->getByRefSiteMode($scope, $site);
$creationScope = $observing || $mode !== null ? ClosureSignatureInference::findCreationScope($scope, $storage, $site) : null;
if ($creationScope === null) {
$hasOtherMember = true;
continue;
}
if ($observing || $mode === 'local') {
$scope = $scope->addTemplateArgumentConstraints(ClosureSignatureInference::collectInvocation($scope, $call, $closureType, $observing));
}
$invocations[] = [$site, $closureType, $creationScope, $mode !== 'local'];
foreach ($site->uses as $use) {
if (!$use->byRef || !is_string($use->var->name)) {
continue;
}
$byRefNames[$use->var->name] = true;
}
}
$resultScope = $hasOtherMember ? $scope : null;
$throwPoints = [];
foreach ($invocations as [$site, $closureType, $creationScope, $untilFixpoint]) {
$invokedScope = $observing ? $this->findCreationFixpointScope($scope, $storage, $site, $closureType, $argumentTypes) : null;
if ($invokedScope !== null) {
$resultScope = $resultScope === null ? $invokedScope : $resultScope->mergeWith($invokedScope);
continue;
}
[$invokedScope, $invocationThrowPoints] = $this->closureProcessor->processByRefInvocation(
$nodeScopeResolver,
$site,
$call,
$scope,
$storage,
$argumentTypes,
$creationScope,
$untilFixpoint,
);
$resultScope = $resultScope === null ? $invokedScope : $resultScope->mergeWith($invokedScope);
$throwPoints = array_merge($throwPoints, $invocationThrowPoints);
}
return [$resultScope ?? $scope, $throwPoints, array_keys($byRefNames)];
}
/**
* While observing, the creation of the closure left its by-ref variables at
* the fixpoint of any number of runs from where it was created. An
* invocation from within that fixpoint - with arguments its parameters
* accept, and nothing a catch could see in the middle of a run - adds
* nothing to it, so the variables stay at it without walking the body.
*
* @param array<int, Type> $argumentTypes
*/
private function findCreationFixpointScope(MutatingScope $scope, ExpressionResultStorage $storage, Expr\Closure $site, ClosureType $closureType, array $argumentTypes): ?MutatingScope
{
if ($closureType->getThrowPoints() !== []) {
return null;
}
$creationResult = $storage->findExpressionResult($site);
if ($creationResult === null) {
return null;
}
foreach ($closureType->getParameters() as $i => $parameter) {
if (!isset($argumentTypes[$i])) {
continue;
}
if (!$parameter->getType()->isSuperTypeOf($argumentTypes[$i])->yes()) {
return null;
}
}
$fixpointScope = $creationResult->getScope();
foreach ($site->uses as $use) {
if (!$use->byRef || !is_string($use->var->name)) {
continue;
}
$name = $use->var->name;
if (!$fixpointScope->hasVariableType($name)->yes()) {
return null;
}
$fixpointType = $fixpointScope->getVariableType($name);
$type = $scope->hasVariableType($name)->yes() ? $scope->getVariableType($name) : new NullType();
if (!$fixpointType->isSuperTypeOf($type)->yes()) {
return null;
}
$scope = $scope->assignVariable($name, $fixpointType, $fixpointType, TrinaryLogic::createYes());
}
return $scope;
}
private function getFunctionThrowPoint(
FunctionReflection $functionReflection,
?ParametersAcceptor $parametersAcceptor,
Type $returnType,
FuncCall $normalizedFuncCall,
MutatingScope $scope,
ExpressionContext $context,
): ?InternalThrowPoint
{
foreach ($this->dynamicFunctionThrowTypeExtensions->getAll() as $extension) {
if (!$extension->isFunctionSupported($functionReflection)) {
continue;
}
$throwType = $extension->getThrowTypeFromFunctionCall($functionReflection, $normalizedFuncCall, $scope);
if ($throwType === null || $throwType->isVoid()->yes()) {
return null;
}
return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedFuncCall, false);
}
$throwType = $functionReflection->getThrowType();
if ($throwType !== null && $parametersAcceptor !== null) {
$throwType = ConditionalTypeResolver::resolveForCall($throwType, $parametersAcceptor, $normalizedFuncCall->getArgs(), $scope);
}
if ($throwType === null) {
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
$throwType = new ObjectType(Throwable::class);
}
}
if ($throwType !== null) {
if (!$throwType->isVoid()->yes()) {
return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedFuncCall, true);
}
} elseif ($this->implicitThrows) {
$requiredParameters = null;
if ($parametersAcceptor !== null) {
$requiredParameters = 0;
foreach ($parametersAcceptor->getParameters() as $parameter) {
if ($parameter->isOptional()) {
continue;
}
$requiredParameters++;
}
}
if (
!$functionReflection->isBuiltin()
|| $requiredParameters === null
|| $requiredParameters > 0
|| count($normalizedFuncCall->getArgs()) > 0
) {
if (!$context->isInThrow() || !(new ObjectType(Throwable::class))->isSuperTypeOf($returnType)->yes()) {
return InternalThrowPoint::createImplicit($scope, $normalizedFuncCall);
}
}
}
return null;
}
/**
* The call-expression type is derived from $preResolvedAcceptor - the acceptor
* processArgs() selected from the arg types gathered on the arg-to-arg evolving
* scope (type-driven, generics resolved). When null (native-types-promoted, or
* a callable callee whose name was processed elsewhere), it falls back to a
* structural acceptor combined from the variants - generic resolution from the
* actual arg types lives in $preResolvedAcceptor, recomputed by on-demand /
* synthetic pricing that re-runs processArgs().
*
* @param FuncCall $expr
*/
private function resolveReturnType(NodeScopeResolver $nodeScopeResolver, MutatingScope $reflectionScope, bool $nativeTypesPromoted, Expr $expr, ?ExpressionResult $nameResult, ?ParametersAcceptor $preResolvedAcceptor, ArgsResult $argsResult): Type
{
// the operands/arguments were processed during processExpr; read their
// already computed results instead of re-walking via Scope::getType().
// The function reflection and dynamic-return-type extensions run on the
// reflection scope (the lexical context / beforeScope). Synthetic nodes the
// resolver builds (e.g. Clone_, call_user_func's inner FuncCall) are priced
// on demand by the same helper.
$getType = static function (Expr $e) use ($expr, $nameResult, $reflectionScope, $nodeScopeResolver, $argsResult, $nativeTypesPromoted): Type {
if ($nameResult !== null && $e === $expr->name) {
return $nativeTypesPromoted ? $nameResult->getNativeType() : $nameResult->getType();
}
$argResult = $argsResult->findArgResult($e);
if ($argResult !== null) {
return $nativeTypesPromoted ? $argResult->getNativeType() : $argResult->getType();
}
// Synthetic nodes (call_user_func's inner FuncCall, clone-with's Clone_)
// have no captured arg result; they are priced on demand.
$s = $nativeTypesPromoted ? $reflectionScope->doNotTreatPhpDocTypesAsCertain() : $reflectionScope;
return $nodeScopeResolver->processSyntheticOnDemand($e, $s)->getTypeOnScope($s, $s->nativeTypesPromoted);
};
if ($expr->name instanceof Expr) {
$calledOnType = $getType($expr->name);
if ($calledOnType->isCallable()->no()) {
return new ErrorType();
}
if ($preResolvedAcceptor !== null) {
$parametersAcceptor = $preResolvedAcceptor;
} else {
$variants = $calledOnType->getCallableParametersAcceptors($reflectionScope);
$parametersAcceptor = count($variants) === 1
? $variants[0]
: ParametersAcceptorSelector::combineAcceptors($variants);
}
// the same shortcut the named-function branch below takes: the native
// signature answers on its own, before any phpdoc-based dynamic
// return type extension gets a say
if ($nativeTypesPromoted && $parametersAcceptor instanceof ExtendedParametersAcceptor) {
return $parametersAcceptor->getNativeReturnType();
}
$functionName = null;
if ($expr->name instanceof String_) {
/** @var non-empty-string $name */
$name = $expr->name->value;
$functionName = new Name($name);
} elseif (
$expr->name instanceof FuncCall
&& $expr->name->name instanceof Name
&& $expr->name->isFirstClassCallable()
) {
$functionName = $expr->name->name;
}
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr);
if ($normalizedNode !== null && $functionName !== null && $this->reflectionProvider->hasFunction($functionName, $reflectionScope)) {
$functionReflection = $this->reflectionProvider->getFunction($functionName, $reflectionScope);
$resolvedType = $this->getDynamicFunctionReturnType($reflectionScope, $normalizedNode, $functionReflection, $argsResult);
if ($resolvedType !== null) {
return $resolvedType;
}
}
return TemplateArgumentFrame::returnTypeOfCall($parametersAcceptor, $reflectionScope, $expr);
}
if (!$this->reflectionProvider->hasFunction($expr->name, $reflectionScope)) {
return new ErrorType();
}
$functionReflection = $this->reflectionProvider->getFunction($expr->name, $reflectionScope);
if ($nativeTypesPromoted) {
return ParametersAcceptorSelector::combineAcceptors($functionReflection->getVariants())->getNativeReturnType();
}
if ($functionReflection->getName() === 'call_user_func') {
$result = ArgumentsNormalizer::reorderCallUserFuncArguments($expr, $reflectionScope);
if ($result !== null) {
[, $innerFuncCall] = $result;
return $getType($innerFuncCall);
}
}
if ($functionReflection->getName() === 'call_user_func_array') {
$result = ArgumentsNormalizer::reorderCallUserFuncArrayArguments($expr, $reflectionScope);
if ($result !== null) {
[, $innerFuncCall] = $result;
return $getType($innerFuncCall);
}
}
if ($preResolvedAcceptor !== null) {
$parametersAcceptor = $preResolvedAcceptor;
} else {
$variants = $functionReflection->getVariants();
$parametersAcceptor = count($variants) === 1
? $variants[0]
: ParametersAcceptorSelector::combineAcceptors($variants);
}
$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr);
if ($normalizedNode !== null) {
if ($functionReflection->getName() === 'clone' && count($normalizedNode->getArgs()) > 0) {
$cloneType = $getType(new Expr\Clone_($normalizedNode->getArgs()[0]->value));
if (count($normalizedNode->getArgs()) === 2) {
$propertiesType = $getType($normalizedNode->getArgs()[1]->value);
if ($propertiesType->isConstantArray()->yes()) {
$constantArrays = $propertiesType->getConstantArrays();
if (count($constantArrays) === 1) {
$accessories = [];
foreach ($constantArrays[0]->getKeyTypes() as $keyType) {
$constantKeyTypes = $keyType->getConstantScalarValues();
if (count($constantKeyTypes) !== 1) {
return $cloneType;
}
$accessories[] = new HasPropertyType((string) $constantKeyTypes[0]);
}
if (count($accessories) > 0 && count($accessories) <= 16) {
return TypeCombinator::intersect($cloneType, ...$accessories);
}
}
}
}
return $cloneType;
}
$resolvedType = $this->getDynamicFunctionReturnType($reflectionScope, $normalizedNode, $functionReflection, $argsResult);
if ($resolvedType !== null) {
return $resolvedType;
}
}
// the typeCallback keeps void; ExpressionResult projects void->null for
// value reads, getKeepVoidType() keeps it
return TemplateArgumentFrame::returnTypeOfCall($parametersAcceptor, $reflectionScope, $expr);
}
/**
* A closure whose signature is inferred from its invocations (see
* ClosureSignatureInference) joins the arguments of all of them; invoked
* with narrower ones, it returns what its body returns for these. The
* variable holds the closure it was assigned when the type it holds is the
* type the closure was created with.
*/
private function resolveInvokedClosureType(NodeScopeResolver $nodeScopeResolver, FuncCall $call, string $name, Type $calleeType, MutatingScope $scope, ExpressionResultStorage $storage): ?ClosureType
{
if (!$calleeType instanceof ClosureType || !$this->closureSignatureInference->infersInvocationReturnType($scope, $calleeType)) {
return null;
}
$parameters = $calleeType->getParameters();
$args = $call->getArgs();
if ($args === [] || count($args) > count($parameters)) {
return null;
}
$narrower = false;
$callableParameters = [];
foreach ($parameters as $i => $parameter) {
if ($parameter->isVariadic() || !$parameter->passedByReference()->no()) {
return null;
}
$type = $parameter->getType();
if (isset($args[$i])) {
if ($args[$i]->name !== null || $args[$i]->unpack) {
return null;
}
$argumentType = $nodeScopeResolver->readTypeOfMaybeStored($args[$i]->value, $scope);
// an argument the signature rejects is reported against it
if (!$type->isSuperTypeOf($argumentType)->yes()) {
return null;
}
if (!$argumentType->isSuperTypeOf($type)->yes()) {
$narrower = true;
$type = $argumentType;
}
}
$callableParameters[] = new NativeParameterReflection($parameter->getName(), $parameter->isOptional(), $type, PassedByReference::createNo(), false, null);
}
if (!$narrower) {
return null;