Total Complexity | 45 |
Total Lines | 267 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like DeferredCallChain often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DeferredCallChain, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class DeferredCallChain implements \JsonSerializable, \ArrayAccess |
||
22 | { |
||
23 | use \JClaveau\Traits\Fluent\New_; |
||
24 | use FunctionCallTrait; |
||
25 | use ArrayAccessTrait; |
||
26 | use ExportTrait; |
||
27 | |||
28 | /** @var array $stack The stack of deferred calls */ |
||
29 | protected $stack = []; |
||
30 | |||
31 | /** @var mixed $expectedTarget The stack of deferred calls */ |
||
32 | protected $expectedTarget; |
||
33 | |||
34 | /** |
||
35 | * Constructor |
||
36 | * |
||
37 | * @param string $class_type_interface_or_instance The expected target class/type/interface/instance |
||
38 | */ |
||
39 | public function __construct($class_type_interface_or_instance=null) |
||
40 | { |
||
41 | if ($class_type_interface_or_instance) { |
||
42 | $this->expectedTarget = $class_type_interface_or_instance; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Stores any call in the the stack. |
||
48 | * |
||
49 | * @param string $method |
||
50 | * @param array $arguments |
||
51 | * |
||
52 | * @return $this |
||
53 | */ |
||
54 | public final function __call($method, array $arguments) |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * ArrayAccess interface |
||
70 | * |
||
71 | * @param string $key The entry to acces |
||
72 | */ |
||
73 | public function &offsetGet($key) |
||
74 | { |
||
75 | $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
||
76 | |||
77 | $this->stack[] = [ |
||
78 | 'entry' => $key, |
||
79 | 'file' => isset($caller['file']) ? $caller['file'] : null, |
||
80 | 'line' => isset($caller['line']) ? $caller['line'] : null, |
||
81 | ]; |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Checks that the provided target matches the type/class/interface |
||
88 | * given during construction. |
||
89 | * |
||
90 | * @param mixed $target |
||
91 | * @return mixed $target Checked |
||
92 | */ |
||
93 | protected function checkTarget($target) |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Calling a method coded inside a magic __call can produce a |
||
133 | * BadMethodCallException and thus not be a callable. |
||
134 | * |
||
135 | * @param mixed $current_chained_subject |
||
136 | * @param string $method_name |
||
137 | * @param array $arguments |
||
138 | * |
||
139 | * @return bool $is_called |
||
140 | */ |
||
141 | protected function checkMethodIsReallyCallable( |
||
142 | $method_type, |
||
143 | &$current_chained_subject, |
||
144 | $method_name, |
||
145 | $arguments |
||
146 | ) { |
||
147 | $is_called = true; |
||
148 | try { |
||
149 | if ($method_type == '->') { |
||
150 | $callable = [$current_chained_subject, $method_name]; |
||
151 | } |
||
152 | elseif ($method_type == '::') { |
||
153 | if (is_object($current_chained_subject)) { |
||
154 | $class = get_class($current_chained_subject); |
||
155 | } |
||
156 | elseif (is_string($current_chained_subject)) { |
||
157 | $class = $current_chained_subject; |
||
158 | } |
||
159 | |||
160 | $callable = $class .'::'. $method_name; |
||
|
|||
161 | } |
||
162 | |||
163 | $current_chained_subject = call_user_func_array( |
||
164 | $callable, |
||
165 | $arguments |
||
166 | ); |
||
167 | } |
||
168 | catch (\BadMethodCallException $e) { |
||
169 | if ($this->exceptionTrownFromMagicCall( |
||
170 | $e->getTrace(), |
||
171 | $current_chained_subject, |
||
172 | $method_name |
||
173 | )) { |
||
174 | $is_called = false; |
||
175 | } |
||
176 | else { |
||
177 | throw $e; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | return $is_called; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Checks if the exception having $trace is thrown from à __call |
||
186 | * magic method. |
||
187 | * |
||
188 | * @param array $trace |
||
189 | * @param object $current_chained_subject |
||
190 | * @param string $method_name |
||
191 | * |
||
192 | * @return bool Whether or not the exception having the $trace has been |
||
193 | * thrown from a __call() method. |
||
194 | */ |
||
195 | protected function exceptionTrownFromMagicCall( |
||
196 | $trace, |
||
197 | $current_chained_subject, |
||
198 | $method_name |
||
199 | ) { |
||
200 | // Before PHP 7, there is a raw for the non existing method called |
||
201 | $call_user_func_array_position = PHP_VERSION_ID < 70000 ? 2 : 1; |
||
202 | |||
203 | return |
||
204 | ($trace[0]['function'] == '__call' || $trace[0]['function'] == '__callStatic') |
||
205 | && $trace[0]['class'] == get_class($current_chained_subject) |
||
206 | && $trace[0]['args'][0] == $method_name |
||
207 | && ( |
||
208 | $trace[$call_user_func_array_position]['file'] == __FILE__ |
||
209 | && $trace[$call_user_func_array_position]['function'] == 'call_user_func_array' |
||
210 | ) |
||
211 | ; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Invoking the instance produces the call of the stack |
||
216 | * |
||
217 | * @param mixed $target The target to apply the callchain on |
||
218 | * @return mixed The value returned once the call chain is called uppon $target |
||
219 | */ |
||
220 | public function __invoke($target=null) |
||
288 | } |
||
289 | |||
290 | /**/ |
||
292 |