33namespace PHPStan \Analyser \ExprHandler ;
44
55use PhpParser \Node \Expr ;
6+ use PhpParser \Node \Expr \BinaryOp \Identical ;
67use PhpParser \Node \Expr \New_ ;
78use PhpParser \Node \Expr \StaticCall ;
89use PhpParser \Node \Expr \Variable ;
10+ use PhpParser \Node \Identifier ;
911use PhpParser \Node \Name ;
12+ use PhpParser \Node \Scalar \String_ ;
1013use PhpParser \Node \Stmt ;
1114use PHPStan \Analyser \ArgumentsNormalizer ;
1215use PHPStan \Analyser \ExpressionContext ;
1316use PHPStan \Analyser \ExpressionResult ;
1417use PHPStan \Analyser \ExpressionResultStorage ;
1518use PHPStan \Analyser \ExprHandler ;
19+ use PHPStan \Analyser \ExprHandler \Helper \MethodCallReturnTypeHelper ;
20+ use PHPStan \Analyser \ExprHandler \Helper \NullsafeShortCircuitingHelper ;
1621use PHPStan \Analyser \ImpurePoint ;
1722use PHPStan \Analyser \InternalThrowPoint ;
1823use PHPStan \Analyser \MutatingScope ;
2631use PHPStan \Reflection \MethodReflection ;
2732use PHPStan \Reflection \ParametersAcceptor ;
2833use PHPStan \Reflection \ParametersAcceptorSelector ;
34+ use PHPStan \Type \ErrorType ;
2935use PHPStan \Type \MixedType ;
3036use PHPStan \Type \NeverType ;
3137use PHPStan \Type \ObjectType ;
38+ use PHPStan \Type \StaticType ;
39+ use PHPStan \Type \Type ;
3240use PHPStan \Type \TypeCombinator ;
41+ use PHPStan \Type \TypeWithClassName ;
3342use ReflectionProperty ;
3443use Throwable ;
44+ use function array_map ;
3545use function array_merge ;
3646use function count ;
47+ use function in_array ;
3748use function sprintf ;
3849use function strtolower ;
3950
@@ -46,6 +57,7 @@ final class StaticCallHandler implements ExprHandler
4657
4758 public function __construct (
4859 private DynamicThrowTypeExtensionProvider $ dynamicThrowTypeExtensionProvider ,
60+ private MethodCallReturnTypeHelper $ methodCallReturnTypeHelper ,
4961 #[AutowiredParameter(ref: '%exceptions.implicitThrows% ' )]
5062 private bool $ implicitThrows ,
5163 #[AutowiredParameter]
@@ -287,4 +299,88 @@ private function getStaticMethodThrowPoint(MethodReflection $methodReflection, P
287299 return null ;
288300 }
289301
302+ /**
303+ * @param StaticCall $expr
304+ */
305+ public function resolveType (MutatingScope $ scope , Expr $ expr ): Type
306+ {
307+ if ($ expr ->name instanceof Identifier) {
308+ if ($ scope ->nativeTypesPromoted ) {
309+ if ($ expr ->class instanceof Name) {
310+ $ staticMethodCalledOnType = $ this ->resolveTypeByNameWithLateStaticBinding ($ scope , $ expr ->class , $ expr ->name );
311+ } else {
312+ $ staticMethodCalledOnType = $ scope ->getNativeType ($ expr ->class );
313+ }
314+ $ methodReflection = $ scope ->getMethodReflection (
315+ $ staticMethodCalledOnType ,
316+ $ expr ->name ->name ,
317+ );
318+ if ($ methodReflection === null ) {
319+ $ callType = new ErrorType ();
320+ } else {
321+ $ callType = ParametersAcceptorSelector ::combineAcceptors ($ methodReflection ->getVariants ())->getNativeReturnType ();
322+ }
323+
324+ if ($ expr ->class instanceof Expr) {
325+ return NullsafeShortCircuitingHelper ::getType ($ scope , $ expr ->class , $ callType );
326+ }
327+
328+ return $ callType ;
329+ }
330+
331+ if ($ expr ->class instanceof Name) {
332+ $ staticMethodCalledOnType = $ this ->resolveTypeByNameWithLateStaticBinding ($ scope , $ expr ->class , $ expr ->name );
333+ } else {
334+ $ staticMethodCalledOnType = TypeCombinator ::removeNull ($ scope ->getType ($ expr ->class ))->getObjectTypeOrClassStringObjectType ();
335+ }
336+
337+ $ callType = $ this ->methodCallReturnTypeHelper ->methodCallReturnType (
338+ $ scope ,
339+ $ staticMethodCalledOnType ,
340+ $ expr ->name ->toString (),
341+ $ expr ,
342+ );
343+ if ($ callType === null ) {
344+ $ callType = new ErrorType ();
345+ }
346+
347+ if ($ expr ->class instanceof Expr) {
348+ return NullsafeShortCircuitingHelper ::getType ($ scope , $ expr ->class , $ callType );
349+ }
350+
351+ return $ callType ;
352+ }
353+
354+ $ nameType = $ scope ->getType ($ expr ->name );
355+ if (count ($ nameType ->getConstantStrings ()) > 0 ) {
356+ return TypeCombinator ::union (
357+ ...array_map (static fn ($ constantString ) => $ constantString ->getValue () === '' ? new ErrorType () : $ scope
358+ ->filterByTruthyValue (new Identical ($ expr ->name , new String_ ($ constantString ->getValue ())))
359+ ->getType (new Expr \StaticCall ($ expr ->class , new Identifier ($ constantString ->getValue ()), $ expr ->args )), $ nameType ->getConstantStrings ()),
360+ );
361+ }
362+
363+ return new MixedType ();
364+ }
365+
366+ private function resolveTypeByNameWithLateStaticBinding (MutatingScope $ scope , Name $ class , Identifier $ name ): TypeWithClassName
367+ {
368+ $ classType = $ scope ->resolveTypeByName ($ class );
369+
370+ if (
371+ $ classType instanceof StaticType
372+ && !in_array ($ class ->toLowerString (), ['self ' , 'static ' , 'parent ' ], true )
373+ ) {
374+ $ methodReflectionCandidate = $ scope ->getMethodReflection (
375+ $ classType ,
376+ $ name ->name ,
377+ );
378+ if ($ methodReflectionCandidate !== null && $ methodReflectionCandidate ->isStatic ()) {
379+ $ classType = $ classType ->getStaticObjectType ();
380+ }
381+ }
382+
383+ return $ classType ;
384+ }
385+
290386}
0 commit comments