@@ -29,27 +29,33 @@ final class Type
2929 */
3030 public static function fromReflection (
3131 \ReflectionFunctionAbstract |\ReflectionParameter |\ReflectionProperty $ reflection ,
32+ ?\ReflectionClass $ staticClass = null ,
3233 ): ?self
3334 {
3435 $ type = $ reflection instanceof \ReflectionFunctionAbstract
3536 ? $ reflection ->getReturnType () ?? (PHP_VERSION_ID >= 80100 && $ reflection instanceof \ReflectionMethod ? $ reflection ->getTentativeReturnType () : null )
3637 : $ reflection ->getType ();
3738
38- return $ type ? self ::fromReflectionType ($ type , $ reflection , true ) : null ;
39+ return $ type ? self ::fromReflectionType ($ type , $ reflection , true , $ staticClass ) : null ;
3940 }
4041
4142
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
4349 {
4450 if ($ type instanceof \ReflectionNamedType) {
45- $ name = self ::resolve ($ type ->getName (), $ of );
51+ $ name = self ::resolve ($ type ->getName (), $ of, $ staticClass );
4652 return $ asObject
4753 ? new self ($ type ->allowsNull () && $ name !== 'mixed ' ? [$ name , 'null ' ] : [$ name ])
4854 : $ name ;
4955
5056 } elseif ($ type instanceof \ReflectionUnionType || $ type instanceof \ReflectionIntersectionType) {
5157 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 ()),
5359 $ type instanceof \ReflectionUnionType ? '| ' : '& ' ,
5460 );
5561
@@ -90,13 +96,16 @@ public static function fromString(string $type): self
9096 public static function resolve (
9197 string $ type ,
9298 \ReflectionFunctionAbstract |\ReflectionParameter |\ReflectionProperty $ of ,
99+ ?\ReflectionClass $ staticClass = null ,
93100 ): string
94101 {
95102 $ lower = strtolower ($ type );
96103 if ($ of instanceof \ReflectionFunction) {
97104 return $ type ;
98- } elseif ($ lower === 'self ' || $ lower === ' static ' ) {
105+ } elseif ($ lower === 'self ' ) {
99106 return $ of ->getDeclaringClass ()->name ;
107+ } elseif ($ lower === 'static ' ) {
108+ return ($ staticClass ?? $ of ->getDeclaringClass ())->name ;
100109 } elseif ($ lower === 'parent ' && $ of ->getDeclaringClass ()->getParentClass ()) {
101110 return $ of ->getDeclaringClass ()->getParentClass ()->name ;
102111 } else {
0 commit comments