3
3
namespace PHPStan \Analyser ;
4
4
5
5
use PHPStan \ShouldNotHappenException ;
6
- use PHPStan \Type \MixedType ;
7
6
use PHPStan \Type \Type ;
8
7
9
8
/**
@@ -23,29 +22,37 @@ final class TypeSpecifierContext
23
22
/** @var self[] */
24
23
private static array $ registry ;
25
24
26
- private function __construct (private ?int $ value )
25
+ private function __construct (
26
+ private ?int $ value ,
27
+ private ?Type $ returnType ,
28
+ )
27
29
{
28
30
}
29
31
30
- private static function create (?int $ value ): self
32
+ private static function create (?int $ value, ? Type $ returnType = null ): self
31
33
{
32
- self ::$ registry [$ value ] ??= new self ($ value );
34
+ if ($ returnType !== null ) {
35
+ // return type bound context is unique for each context and therefore not cachable
36
+ return new self ($ value , $ returnType );
37
+ }
38
+
39
+ self ::$ registry [$ value ] ??= new self ($ value , null );
33
40
return self ::$ registry [$ value ];
34
41
}
35
42
36
- public static function createTrue (): self
43
+ public static function createTrue (? Type $ returnType = null ): self
37
44
{
38
- return self ::create (self ::CONTEXT_TRUE );
45
+ return self ::create (self ::CONTEXT_TRUE , $ returnType );
39
46
}
40
47
41
48
public static function createTruthy (): self
42
49
{
43
50
return self ::create (self ::CONTEXT_TRUTHY );
44
51
}
45
52
46
- public static function createFalse (): self
53
+ public static function createFalse (? Type $ returnType = null ): self
47
54
{
48
- return self ::create (self ::CONTEXT_FALSE );
55
+ return self ::create (self ::CONTEXT_FALSE , $ returnType );
49
56
}
50
57
51
58
public static function createFalsey (): self
@@ -91,9 +98,9 @@ public function null(): bool
91
98
return $ this ->value === null ;
92
99
}
93
100
94
- public function getReturnType (): Type
101
+ public function getReturnType (): ? Type
95
102
{
96
- return new MixedType () ;
103
+ return $ this -> returnType ;
97
104
}
98
105
99
106
}
0 commit comments