Conditions | 11 |
Paths | 16 |
Total Lines | 43 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
58 | public function call($callable) |
||
59 | { |
||
60 | if (is_array($callable)) { |
||
61 | if (count($callable) != 2) { |
||
62 | throw new InvalidCallableException('Invalid callable: ' . implode(',', $callable)); |
||
63 | } |
||
64 | |||
65 | [$class, $method] = $callable; |
||
66 | |||
67 | if (!class_exists($class)) { |
||
68 | throw new InvalidCallableException("Class `$class` not found."); |
||
69 | } |
||
70 | |||
71 | $object = $this->container->instantiate($class); |
||
72 | |||
73 | if (!method_exists($object, $method)) { |
||
74 | throw new InvalidCallableException("Method `$class::$method` is not declared."); |
||
75 | } |
||
76 | |||
77 | $callable = [$object, $method]; |
||
78 | } else { |
||
79 | if (is_string($callable)) { |
||
80 | if (class_exists($callable)) { |
||
81 | $callable = new $callable(); |
||
82 | } else { |
||
83 | throw new InvalidCallableException("Class `$callable` not found."); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | if (is_object($callable) && !$callable instanceof Closure) { |
||
88 | if (method_exists($callable, 'handle')) { |
||
89 | $callable = [$callable, 'handle']; |
||
90 | } else { |
||
91 | throw new InvalidCallableException("Method `handle` is not declared."); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if (!is_callable($callable)) { |
||
97 | throw new InvalidCallableException('Invalid callable.'); |
||
98 | } |
||
99 | |||
100 | return $this->container->call($callable); |
||
101 | } |
||
103 |