5
5
use PhpParser \Node \Expr \StaticCall ;
6
6
use PhpParser \Node \Name ;
7
7
use PHPStan \Analyser \Scope ;
8
+ use PHPStan \Broker \Broker ;
9
+ use PHPStan \Reflection \BrokerAwareExtension ;
8
10
use PHPStan \Reflection \MethodReflection ;
9
11
use PHPStan \Reflection \ParametersAcceptorSelector ;
10
12
use PHPStan \Rules \Doctrine \ORM \DynamicQueryBuilderArgumentException ;
11
13
use PHPStan \Type \Doctrine \ArgumentsProcessor ;
12
14
use PHPStan \Type \DynamicStaticMethodReturnTypeExtension ;
13
15
use PHPStan \Type \Type ;
14
16
15
- class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
17
+ class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension, BrokerAwareExtension
16
18
{
17
19
18
20
/** @var \PHPStan\Type\Doctrine\ArgumentsProcessor */
@@ -21,6 +23,9 @@ class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnType
21
23
/** @var string */
22
24
private $ class ;
23
25
26
+ /** @var \PHPStan\Broker\Broker */
27
+ private $ broker ;
28
+
24
29
public function __construct (
25
30
ArgumentsProcessor $ argumentsProcessor ,
26
31
string $ class
@@ -30,6 +35,11 @@ public function __construct(
30
35
$ this ->class = $ class ;
31
36
}
32
37
38
+ public function setBroker (Broker $ broker ): void
39
+ {
40
+ $ this ->broker = $ broker ;
41
+ }
42
+
33
43
public function getClass (): string
34
44
{
35
45
return $ this ->class ;
@@ -46,7 +56,12 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
46
56
throw new \PHPStan \ShouldNotHappenException ();
47
57
}
48
58
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
+ }
50
65
51
66
try {
52
67
$ exprObject = new $ className (
0 commit comments