Skip to content

Commit d152fe2

Browse files
committed
Type: fixed resolving of 'static'
1 parent cead663 commit d152fe2

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/Utils/Type.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

tests/Utils/Type.fromReflection.method.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@ $type = Type::fromReflection((new ReflectionObject($class))->getMethod('foo'));
3737

3838
Assert::same([$class::class], $type->getNames());
3939
Assert::same($class::class, (string) $type);
40+
41+
42+
class ParentClass
43+
{
44+
public function foo(): static
45+
{
46+
}
47+
}
48+
49+
class ChildClass extends ParentClass
50+
{
51+
}
52+
53+
$type = Type::fromReflection(new ReflectionMethod(ChildClass::class, 'foo'), new ReflectionClass(ChildClass::class));
54+
Assert::same([ChildClass::class], $type->getNames());
55+
Assert::same(ChildClass::class, (string) $type);

0 commit comments

Comments
 (0)