@@ -29,27 +29,33 @@ final class Type
29
29
*/
30
30
public static function fromReflection (
31
31
\ReflectionFunctionAbstract |\ReflectionParameter |\ReflectionProperty $ reflection ,
32
+ ?\ReflectionClass $ staticClass = null ,
32
33
): ?self
33
34
{
34
35
$ type = $ reflection instanceof \ReflectionFunctionAbstract
35
36
? $ reflection ->getReturnType () ?? (PHP_VERSION_ID >= 80100 && $ reflection instanceof \ReflectionMethod ? $ reflection ->getTentativeReturnType () : null )
36
37
: $ reflection ->getType ();
37
38
38
- return $ type ? self ::fromReflectionType ($ type , $ reflection , true ) : null ;
39
+ return $ type ? self ::fromReflectionType ($ type , $ reflection , true , $ staticClass ) : null ;
39
40
}
40
41
41
42
42
- private static function fromReflectionType (\ReflectionType $ type , $ of , bool $ asObject ): self |string
43
+ private static function fromReflectionType (
44
+ \ReflectionType $ type ,
45
+ $ of ,
46
+ bool $ asObject ,
47
+ ?\ReflectionClass $ staticClass ,
48
+ ): self |string
43
49
{
44
50
if ($ type instanceof \ReflectionNamedType) {
45
- $ name = self ::resolve ($ type ->getName (), $ of );
51
+ $ name = self ::resolve ($ type ->getName (), $ of, $ staticClass );
46
52
return $ asObject
47
53
? new self ($ type ->allowsNull () && $ name !== 'mixed ' ? [$ name , 'null ' ] : [$ name ])
48
54
: $ name ;
49
55
50
56
} elseif ($ type instanceof \ReflectionUnionType || $ type instanceof \ReflectionIntersectionType) {
51
57
return new self (
52
- array_map (fn ($ t ) => self ::fromReflectionType ($ t , $ of , false ), $ type ->getTypes ()),
58
+ array_map (fn ($ t ) => self ::fromReflectionType ($ t , $ of , false , $ staticClass ), $ type ->getTypes ()),
53
59
$ type instanceof \ReflectionUnionType ? '| ' : '& ' ,
54
60
);
55
61
@@ -90,13 +96,16 @@ public static function fromString(string $type): self
90
96
public static function resolve (
91
97
string $ type ,
92
98
\ReflectionFunctionAbstract |\ReflectionParameter |\ReflectionProperty $ of ,
99
+ ?\ReflectionClass $ staticClass = null ,
93
100
): string
94
101
{
95
102
$ lower = strtolower ($ type );
96
103
if ($ of instanceof \ReflectionFunction) {
97
104
return $ type ;
98
- } elseif ($ lower === 'self ' || $ lower === ' static ' ) {
105
+ } elseif ($ lower === 'self ' ) {
99
106
return $ of ->getDeclaringClass ()->name ;
107
+ } elseif ($ lower === 'static ' ) {
108
+ return ($ staticClass ?? $ of ->getDeclaringClass ())->name ;
100
109
} elseif ($ lower === 'parent ' && $ of ->getDeclaringClass ()->getParentClass ()) {
101
110
return $ of ->getDeclaringClass ()->getParentClass ()->name ;
102
111
} else {
0 commit comments