File tree Expand file tree Collapse file tree 4 files changed +17
-5
lines changed
tests/PHPStan/Analyser/data Expand file tree Collapse file tree 4 files changed +17
-5
lines changed Original file line number Diff line number Diff line change 6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Reflection \FunctionReflection ;
8
8
use PHPStan \Type \DynamicFunctionReturnTypeExtension ;
9
+ use PHPStan \Type \ErrorType ;
9
10
use PHPStan \Type \Type ;
10
11
use PHPStan \Type \TypeCombinator ;
11
12
use function count ;
@@ -39,7 +40,13 @@ public function getTypeFromFunctionCall(
39
40
if ($ constantName ->getValue () === '' ) {
40
41
return null ;
41
42
}
42
- $ results [] = $ scope ->getType ($ this ->constantHelper ->createExprFromConstantName ($ constantName ->getValue ()));
43
+
44
+ $ expr = $ this ->constantHelper ->createExprFromConstantName ($ constantName ->getValue ());
45
+ if ($ expr === null ) {
46
+ return new ErrorType ();
47
+ }
48
+
49
+ $ results [] = $ scope ->getType ($ expr );
43
50
}
44
51
45
52
if (count ($ results ) > 0 ) {
Original file line number Diff line number Diff line change 8
8
use PhpParser \Node \Identifier ;
9
9
use PhpParser \Node \Name ;
10
10
use PhpParser \Node \Name \FullyQualified ;
11
- use PHPStan \ShouldNotHappenException ;
12
11
use function count ;
13
12
use function explode ;
14
13
use function ltrim ;
@@ -19,13 +18,13 @@ class ConstantHelper
19
18
/**
20
19
* @param non-empty-string $constantName
21
20
*/
22
- public function createExprFromConstantName (string $ constantName ): Expr
21
+ public function createExprFromConstantName (string $ constantName ): ? Expr
23
22
{
24
23
$ classConstParts = explode (':: ' , $ constantName );
25
24
if (count ($ classConstParts ) >= 2 ) {
26
25
$ fqcn = ltrim ($ classConstParts [0 ], '\\' );
27
26
if ($ fqcn === '' ) {
28
- throw new ShouldNotHappenException () ;
27
+ return null ;
29
28
}
30
29
31
30
$ classConstName = new FullyQualified ($ fqcn );
Original file line number Diff line number Diff line change @@ -54,8 +54,13 @@ public function specifyTypes(
54
54
return new SpecifiedTypes ([], []);
55
55
}
56
56
57
+ $ expr = $ this ->constantHelper ->createExprFromConstantName ($ constantName ->getValue ());
58
+ if ($ expr === null ) {
59
+ return new SpecifiedTypes ([], []);
60
+ }
61
+
57
62
return $ this ->typeSpecifier ->create (
58
- $ this -> constantHelper -> createExprFromConstantName ( $ constantName -> getValue ()) ,
63
+ $ expr ,
59
64
new MixedType (),
60
65
$ context ,
61
66
false ,
Original file line number Diff line number Diff line change @@ -38,3 +38,4 @@ function doFoo(string $constantName): void
38
38
assertType ('Constant\Suit::Hearts ' , constant ('\Constant\Suit::Hearts ' ));
39
39
40
40
assertType ('*ERROR* ' , constant ('UNDEFINED ' ));
41
+ assertType ('*ERROR* ' , constant ('::aa ' ));
You can’t perform that action at this time.
0 commit comments