55use PhpParser \Node \Expr \StaticCall ;
66use PhpParser \Node \Name ;
77use PHPStan \Analyser \Scope ;
8+ use PHPStan \Broker \Broker ;
9+ use PHPStan \Reflection \BrokerAwareExtension ;
810use PHPStan \Reflection \MethodReflection ;
911use PHPStan \Reflection \ParametersAcceptorSelector ;
1012use PHPStan \Rules \Doctrine \ORM \DynamicQueryBuilderArgumentException ;
1113use PHPStan \Type \Doctrine \ArgumentsProcessor ;
1214use PHPStan \Type \DynamicStaticMethodReturnTypeExtension ;
1315use PHPStan \Type \Type ;
1416
15- class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
17+ class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension, BrokerAwareExtension
1618{
1719
1820 /** @var \PHPStan\Type\Doctrine\ArgumentsProcessor */
@@ -21,6 +23,9 @@ class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnType
2123 /** @var string */
2224 private $ class ;
2325
26+ /** @var \PHPStan\Broker\Broker */
27+ private $ broker ;
28+
2429 public function __construct (
2530 ArgumentsProcessor $ argumentsProcessor ,
2631 string $ class
@@ -30,6 +35,11 @@ public function __construct(
3035 $ this ->class = $ class ;
3136 }
3237
38+ public function setBroker (Broker $ broker ): void
39+ {
40+ $ this ->broker = $ broker ;
41+ }
42+
3343 public function getClass (): string
3444 {
3545 return $ this ->class ;
@@ -46,7 +56,12 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
4656 throw new \PHPStan \ShouldNotHappenException ();
4757 }
4858
49- $ className = $ methodCall ->class ->toString ();
59+ $ className = $ scope ->resolveName ($ methodCall ->class );
60+ if (!$ this ->broker ->hasClass ($ className )) {
61+ return ParametersAcceptorSelector::selectSingle (
62+ $ methodReflection ->getVariants ()
63+ )->getReturnType ();
64+ }
5065
5166 try {
5267 $ exprObject = new $ className (
0 commit comments