Skip to content

Commit 63ca11a

Browse files
committed
Type: fixed resolving of 'static' [Closes nette/di#295]
1 parent cead663 commit 63ca11a

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/Utils/Callback.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public static function toReflection($callable): \ReflectionMethod|\ReflectionFun
9494
}
9595

9696
if (is_string($callable) && str_contains($callable, '::')) {
97-
return new \ReflectionMethod($callable);
97+
return new ReflectionMethod($callable);
9898
} elseif (is_array($callable)) {
99-
return new \ReflectionMethod($callable[0], $callable[1]);
99+
return new ReflectionMethod($callable[0], $callable[1]);
100100
} elseif (is_object($callable) && !$callable instanceof \Closure) {
101-
return new \ReflectionMethod($callable, '__invoke');
101+
return new ReflectionMethod($callable, '__invoke');
102102
} else {
103103
return new \ReflectionFunction($callable);
104104
}

src/Utils/ReflectionMethod.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Utils;
11+
12+
13+
/**
14+
* Fix for original ReflectionMethod
15+
* @internal
16+
*/
17+
final class ReflectionMethod extends \ReflectionMethod
18+
{
19+
private \ReflectionClass $originalClass;
20+
21+
22+
public function __construct(object|string $objectOrMethod, ?string $method = null)
23+
{
24+
if (is_string($objectOrMethod) && str_contains($objectOrMethod, '::')) {
25+
[$objectOrMethod, $method] = explode('::', $objectOrMethod, 2);
26+
}
27+
parent::__construct($objectOrMethod, $method);
28+
$this->originalClass = new \ReflectionClass($objectOrMethod);
29+
}
30+
31+
32+
public function getOriginalClass(): \ReflectionClass
33+
{
34+
return $this->originalClass;
35+
}
36+
}

src/Utils/Type.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ public static function resolve(
9595
$lower = strtolower($type);
9696
if ($of instanceof \ReflectionFunction) {
9797
return $type;
98-
} elseif ($lower === 'self' || $lower === 'static') {
98+
} elseif ($lower === 'self') {
9999
return $of->getDeclaringClass()->name;
100+
} elseif ($lower === 'static') {
101+
return ($of instanceof ReflectionMethod ? $of->getOriginalClass() : $of->getDeclaringClass())->name;
100102
} elseif ($lower === 'parent' && $of->getDeclaringClass()->getParentClass()) {
101103
return $of->getDeclaringClass()->getParentClass()->name;
102104
} else {

tests/Utils/Type.fromReflection.method.phpt

+16
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 Nette\Utils\ReflectionMethod(ChildClass::class, 'foo'));
54+
Assert::same([ChildClass::class], $type->getNames());
55+
Assert::same(ChildClass::class, (string) $type);

0 commit comments

Comments
 (0)