Conditions | 16 |
Paths | 56 |
Total Lines | 44 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function __invoke($handler, array $arguments) |
||
50 | { |
||
51 | if (\is_string($handler)) { |
||
52 | if (null !== $this->container && $this->container->has($handler)) { |
||
53 | $handler = $this->container->get($handler); |
||
54 | } elseif (\str_contains($handler, '@')) { |
||
55 | $handler = \explode('@', $handler, 2); |
||
56 | |||
57 | goto maybe_callable; |
||
58 | } elseif (\class_exists($handler)) { |
||
59 | $handlerRef = new \ReflectionClass($handler); |
||
60 | |||
61 | if ($handlerRef->hasMethod('__invoke')) { |
||
62 | $handler = [$handler, '__invoke']; |
||
63 | |||
64 | goto maybe_callable; |
||
65 | } |
||
66 | |||
67 | if (null === $constructor = $handlerRef->getConstructor()) { |
||
68 | return $handlerRef->newInstanceWithoutConstructor(); |
||
69 | } |
||
70 | |||
71 | return $handlerRef->newInstanceArgs($this->resolveParameters($constructor->getParameters(), $arguments)); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if ((\is_array($handler) && [0, 1] === \array_keys($handler)) && \is_string($handler[0])) { |
||
76 | maybe_callable: |
||
77 | if (null !== $this->container && $this->container->has($handler[0])) { |
||
78 | $handler[0] = $this->container->get($handler[0]); |
||
79 | } elseif (\class_exists($handler[0])) { |
||
80 | $handler[0] = (new \ReflectionClass($handler[0]))->newInstanceArgs([]); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | if (\is_callable($handler)) { |
||
85 | $handlerRef = new \ReflectionFunction(\Closure::fromCallable($handler)); |
||
86 | } elseif (\is_object($handler)) { |
||
87 | return $handler; |
||
88 | } else { |
||
89 | throw new InvalidControllerException(\sprintf('Route has an invalid handler type of "%s".', \gettype($handler))); |
||
90 | } |
||
91 | |||
92 | return $handlerRef->invokeArgs($this->resolveParameters($handlerRef->getParameters(), $arguments)); |
||
93 | } |
||
148 |