Skip to content

Commit

Permalink
Type: fixed resolving of 'static' [Closes nette/di#295]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 20, 2023
1 parent cead663 commit 63ca11a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Utils/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public static function toReflection($callable): \ReflectionMethod|\ReflectionFun
}

if (is_string($callable) && str_contains($callable, '::')) {
return new \ReflectionMethod($callable);
return new ReflectionMethod($callable);
} elseif (is_array($callable)) {
return new \ReflectionMethod($callable[0], $callable[1]);
return new ReflectionMethod($callable[0], $callable[1]);
} elseif (is_object($callable) && !$callable instanceof \Closure) {
return new \ReflectionMethod($callable, '__invoke');
return new ReflectionMethod($callable, '__invoke');
} else {
return new \ReflectionFunction($callable);
}
Expand Down
36 changes: 36 additions & 0 deletions src/Utils/ReflectionMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Utils;


/**
* Fix for original ReflectionMethod
* @internal
*/
final class ReflectionMethod extends \ReflectionMethod
{
private \ReflectionClass $originalClass;


public function __construct(object|string $objectOrMethod, ?string $method = null)
{
if (is_string($objectOrMethod) && str_contains($objectOrMethod, '::')) {
[$objectOrMethod, $method] = explode('::', $objectOrMethod, 2);
}
parent::__construct($objectOrMethod, $method);
$this->originalClass = new \ReflectionClass($objectOrMethod);
}


public function getOriginalClass(): \ReflectionClass
{
return $this->originalClass;
}
}
4 changes: 3 additions & 1 deletion src/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public static function resolve(
$lower = strtolower($type);
if ($of instanceof \ReflectionFunction) {
return $type;
} elseif ($lower === 'self' || $lower === 'static') {
} elseif ($lower === 'self') {
return $of->getDeclaringClass()->name;
} elseif ($lower === 'static') {
return ($of instanceof ReflectionMethod ? $of->getOriginalClass() : $of->getDeclaringClass())->name;
} elseif ($lower === 'parent' && $of->getDeclaringClass()->getParentClass()) {
return $of->getDeclaringClass()->getParentClass()->name;
} else {
Expand Down
16 changes: 16 additions & 0 deletions tests/Utils/Type.fromReflection.method.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@ $type = Type::fromReflection((new ReflectionObject($class))->getMethod('foo'));

Assert::same([$class::class], $type->getNames());
Assert::same($class::class, (string) $type);


class ParentClass
{
public function foo(): static
{
}
}

class ChildClass extends ParentClass
{
}

$type = Type::fromReflection(new Nette\Utils\ReflectionMethod(ChildClass::class, 'foo'));
Assert::same([ChildClass::class], $type->getNames());
Assert::same(ChildClass::class, (string) $type);

0 comments on commit 63ca11a

Please sign in to comment.