| @@ -1,22 +1,21 @@ | ||
| 1 | -<?php | |
| 1 | +<?php declare(strict_types=1); | |
| 2 | 2 | |
| 3 | 3 | namespace Invoker; |
| 4 | 4 | |
| 5 | +use Closure; | |
| 5 | 6 | use Invoker\Exception\NotCallableException; |
| 6 | 7 | use Psr\Container\ContainerInterface; |
| 7 | 8 | use Psr\Container\NotFoundExceptionInterface; |
| 9 | +use ReflectionException; | |
| 10 | +use ReflectionMethod; | |
| 8 | 11 | |
| 9 | 12 | /** |
| 10 | 13 | * Resolves a callable from a container. |
| 11 | - * | |
| 12 | - * @author Matthieu Napoli <matthieu@mnapoli.fr> | |
| 13 | 14 | */ |
| 14 | 15 | class CallableResolver |
| 15 | 16 | { |
| 16 | - /** | |
| 17 | - * @var ContainerInterface | |
| 18 | - */ | |
| 17 | + /** @var ContainerInterface */ | |
| 19 | 18 | private $container; |
| 20 | 19 | |
| 21 | 20 | public function __construct(ContainerInterface $container) |
| 22 | 21 | { |
| @@ -26,14 +25,12 @@ | ||
| 26 | 25 | /** |
| 27 | 26 | * Resolve the given callable into a real PHP callable. |
| 28 | 27 | * |
| 29 | 28 | * @param callable|string|array $callable |
| 30 | - * | |
| 31 | 29 | * @return callable Real PHP callable. |
| 32 | - * | |
| 33 | - * @throws NotCallableException | |
| 30 | + * @throws NotCallableException|ReflectionException | |
| 34 | 31 | */ |
| 35 | - public function resolve($callable) | |
| 32 | + public function resolve($callable): callable | |
| 36 | 33 | { |
| 37 | 34 | if (is_string($callable) && strpos($callable, '::') !== false) { |
| 38 | 35 | $callable = explode('::', $callable, 2); |
| 39 | 36 | } |
| @@ -48,24 +45,22 @@ | ||
| 48 | 45 | } |
| 49 | 46 | |
| 50 | 47 | /** |
| 51 | 48 | * @param callable|string|array $callable |
| 52 | - * @return callable | |
| 53 | - * @throws NotCallableException | |
| 49 | + * @return callable|mixed | |
| 50 | + * @throws NotCallableException|ReflectionException | |
| 54 | 51 | */ |
| 55 | 52 | private function resolveFromContainer($callable) |
| 56 | 53 | { |
| 57 | 54 | // Shortcut for a very common use case |
| 58 | - if ($callable instanceof \Closure) { | |
| 55 | + if ($callable instanceof Closure) { | |
| 59 | 56 | return $callable; |
| 60 | 57 | } |
| 61 | 58 | |
| 62 | - $isStaticCallToNonStaticMethod = false; | |
| 63 | - | |
| 64 | 59 | // If it's already a callable there is nothing to do |
| 65 | 60 | if (is_callable($callable)) { |
| 66 | - $isStaticCallToNonStaticMethod = $this->isStaticCallToNonStaticMethod($callable); | |
| 67 | - if (! $isStaticCallToNonStaticMethod) { | |
| 61 | + // TODO with PHP 8 that should not be necessary to check this anymore | |
| 62 | + if (! $this->isStaticCallToNonStaticMethod($callable)) { | |
| 68 | 63 | return $callable; |
| 69 | 64 | } |
| 70 | 65 | } |
| 71 | 66 | |
| @@ -91,19 +86,10 @@ | ||
| 91 | 86 | } catch (NotFoundExceptionInterface $e) { |
| 92 | 87 | if ($this->container->has($callable[0])) { |
| 93 | 88 | throw $e; |
| 94 | 89 | } |
| 95 | - if ($isStaticCallToNonStaticMethod) { | |
| 96 | - throw new NotCallableException(sprintf( | |
| 97 | - 'Cannot call %s::%s() because %s() is not a static method and "%s" is not a container entry', | |
| 98 | - $callable[0], | |
| 99 | - $callable[1], | |
| 100 | - $callable[1], | |
| 101 | - $callable[0] | |
| 102 | - )); | |
| 103 | - } | |
| 104 | 90 | throw new NotCallableException(sprintf( |
| 105 | - 'Cannot call %s on %s because it is not a class nor a valid container entry', | |
| 91 | + 'Cannot call %s() on %s because it is not a class nor a valid container entry', | |
| 106 | 92 | $callable[1], |
| 107 | 93 | $callable[0] |
| 108 | 94 | )); |
| 109 | 95 | } |
| @@ -116,15 +102,20 @@ | ||
| 116 | 102 | /** |
| 117 | 103 | * Check if the callable represents a static call to a non-static method. |
| 118 | 104 | * |
| 119 | 105 | * @param mixed $callable |
| 120 | - * @return bool | |
| 106 | + * @throws ReflectionException | |
| 121 | 107 | */ |
| 122 | - private function isStaticCallToNonStaticMethod($callable) | |
| 108 | + private function isStaticCallToNonStaticMethod($callable): bool | |
| 123 | 109 | { |
| 124 | 110 | if (is_array($callable) && is_string($callable[0])) { |
| 125 | - list($class, $method) = $callable; | |
| 126 | - $reflection = new \ReflectionMethod($class, $method); | |
| 111 | + [$class, $method] = $callable; | |
| 112 | + | |
| 113 | + if (! method_exists($class, $method)) { | |
| 114 | + return false; | |
| 115 | + } | |
| 116 | + | |
| 117 | + $reflection = new ReflectionMethod($class, $method); | |
| 127 | 118 | |
| 128 | 119 | return ! $reflection->isStatic(); |
| 129 | 120 | } |
| 130 | 121 | |