Total Complexity | 62 |
Total Lines | 444 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CoreExecuteDelegator 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 CoreExecuteDelegator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class CoreExecuteDelegator |
||
27 | { |
||
28 | use CommonUtils; |
||
29 | |||
30 | /** |
||
31 | * @var CoreInterface インスタンス |
||
32 | */ |
||
33 | private $instance; |
||
34 | |||
35 | /** |
||
36 | * @var CoreInterface 注入済みインスタンス |
||
37 | */ |
||
38 | private $injectedInstance; |
||
39 | |||
40 | /** |
||
41 | * @var Container 依存コンテナ |
||
42 | */ |
||
43 | private $container; |
||
44 | |||
45 | /** |
||
46 | * @var Logger ロガー |
||
47 | */ |
||
48 | private $logger; |
||
49 | |||
50 | /** |
||
51 | * @var AnnotationContainer アノテーション |
||
52 | */ |
||
53 | private $annotation; |
||
54 | |||
55 | /** |
||
56 | * @var array<AnnotationContainer> 例外ハンドラリスト |
||
57 | */ |
||
58 | private $exceptionHandler; |
||
59 | |||
60 | /** |
||
61 | * constructor |
||
62 | */ |
||
63 | public function __construct(CoreInterface $instance, Container $container) |
||
64 | { |
||
65 | $this->instance = $instance; |
||
66 | $this->container = $container; |
||
67 | $this->logger = $container->logger; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * method missing |
||
72 | */ |
||
73 | public function __call($method, $arguments) |
||
74 | { |
||
75 | return $this->run($method, $arguments); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * overload getter |
||
80 | */ |
||
81 | public function __get($name) |
||
82 | { |
||
83 | return $this->getInstance()->{$name}; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * 処理を実行する |
||
88 | * @param string メソッド名 |
||
89 | * @param array 引数リスト |
||
90 | */ |
||
91 | public function run($method, $arguments = []) |
||
92 | { |
||
93 | // すでに注入済みのインスタンスの場合、そのまま実行 |
||
94 | if ($this->injectedInstance !== null) { |
||
95 | return $this->execute($method, $arguments); |
||
96 | } |
||
97 | |||
98 | try { |
||
99 | $result = null; |
||
100 | if ($this->instance instanceof CoreController) { |
||
101 | $this->controllerInjector($this->getOriginMethod($method), $arguments); |
||
102 | } elseif ($this->instance instanceof CoreService) { |
||
103 | $result = $this->serviceInjector($this->getOriginMethod($method), $arguments); |
||
104 | } elseif ($this->instance instanceof CoreModel) { |
||
105 | $result = $this->modelInjector($this->getOriginMethod($method), $arguments); |
||
106 | } elseif ($this->instance instanceof CoreView) { |
||
107 | $this->viewInjector($method, $arguments); |
||
108 | } elseif ($this->instance instanceof CoreHelper) { |
||
109 | $result = $this->helperInjector($this->getOriginMethod($method), $arguments); |
||
110 | } |
||
111 | |||
112 | return $result; |
||
113 | } catch (DoctrineAnnotationException $e) { |
||
114 | throw new AnnotationException($e); |
||
115 | } catch (DelegateException $e) { |
||
116 | // すでにデリゲート済み例外の場合はそのままスロー |
||
117 | // カスタムアノテーション定義で発生する |
||
118 | throw $e; |
||
119 | } catch (\Exception $e) { |
||
120 | $exceptionClass = get_class($e); |
||
121 | switch ($exceptionClass) { |
||
122 | case "Exception": |
||
123 | case "LogicException": |
||
124 | $e = new ApplicationException($e->getMessage(), 500, $e); |
||
125 | break; |
||
126 | case "RuntimeException": |
||
127 | $e = new SystemException($e->getMessage(), 500, $e); |
||
128 | break; |
||
129 | } |
||
130 | |||
131 | $exception = new ExceptionDelegator($this->getInstance(), $e, $method); |
||
132 | $exception->inject('logger', $this->logger); |
||
133 | |||
134 | if ($this->annotation !== null && is_array($this->annotation->exceptionHandler)) { |
||
135 | $exception->setExceptionHandler($this->annotation->exceptionHandler); |
||
136 | } |
||
137 | $exception->raise(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * オリジナルのインスタンスを返却する |
||
143 | * @return CoreInterface インスタンス |
||
144 | */ |
||
145 | public function getInstance() |
||
146 | { |
||
147 | return $this->injectedInstance ?: $this->instance; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * メソッドを実行する |
||
152 | * @param string メソッド名 |
||
153 | * @param array 引数リスト |
||
154 | */ |
||
155 | private function execute($method, $arguments) |
||
156 | { |
||
157 | // serviceの場合、modelの探索に行くためエラーにはしない |
||
158 | if (!($this->injectedInstance instanceof CoreService) && !method_exists($this->injectedInstance, $method)) { |
||
159 | $class = get_class($this->injectedInstance); |
||
160 | throw new MethodNotFoundException("${class}#${method} is not defined."); |
||
161 | } |
||
162 | |||
163 | return call_user_func_array([$this->injectedInstance, $method], $arguments); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Controllerに注入する |
||
168 | * @param string メソッド名 |
||
169 | * @param array 引数リスト |
||
170 | */ |
||
171 | private function controllerInjector($method, $arguments) |
||
267 | } |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Serviceに注入する |
||
272 | * @param string メソッド名 |
||
273 | * @param array 引数リスト |
||
274 | */ |
||
275 | private function serviceInjector($method, $arguments) |
||
276 | { |
||
277 | // アノテーション注入処理は1度しか行わない |
||
278 | if ($this->injectedInstance === null) { |
||
279 | $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
280 | |||
281 | // @Filter |
||
282 | $filter = $this->annotation->filter; |
||
283 | |||
284 | // @ExceptionHandler |
||
285 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
286 | |||
287 | // custom annotation |
||
288 | $this->instance->__customAnnotation($this->annotation->customAnnotations); |
||
289 | |||
290 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
291 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
292 | $exception = $this->annotation->exception; |
||
293 | if ($exception instanceof ExceptionDelegator) { |
||
294 | if ($this->annotation->exceptionHandler !== null) { |
||
295 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
296 | } |
||
297 | $exception->inject('logger', $this->logger); |
||
298 | $exception->setExceptionHandler($this->exceptionHandler); |
||
299 | $exception->raise(); |
||
300 | } |
||
301 | |||
302 | foreach ($filter->initialize as $refMethod) { |
||
303 | $refMethod->invokeArgs($this->instance, [$this->container]); |
||
304 | } |
||
305 | |||
306 | $this->injectedInstance = $this->instance; |
||
307 | $this->instance = null; |
||
308 | } |
||
309 | |||
310 | return $this->execute($method, $arguments); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Modelに注入する |
||
315 | * @param string メソッド名 |
||
316 | * @param array 引数リスト |
||
317 | */ |
||
318 | private function modelInjector($method, $arguments) |
||
319 | { |
||
320 | // アノテーション注入処理は1度しか行わない |
||
321 | if ($this->injectedInstance === null) { |
||
322 | $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
323 | |||
324 | // @Filter |
||
325 | $filter = $this->annotation->filter; |
||
326 | |||
327 | // custom annotation |
||
328 | $this->instance->__customAnnotation($this->annotation->customAnnotations); |
||
329 | |||
330 | if ($this->exceptionHandler === null) { |
||
331 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
332 | } |
||
333 | |||
334 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
335 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
336 | $exception = $this->annotation->exception; |
||
337 | if ($exception instanceof ExceptionDelegator) { |
||
338 | if ($this->annotation->exceptionHandler !== null) { |
||
339 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
340 | } |
||
341 | $exception->inject('logger', $this->logger); |
||
342 | $exception->setExceptionHandler($this->annotation->exceptionHandler); |
||
343 | $exception->raise(); |
||
344 | } |
||
345 | |||
346 | $initializeContainer = new Container(false); |
||
347 | $initializeContainer->connectionContainerList = $this->annotation->database; |
||
348 | $initializeContainer->queryAnnotations = $this->annotation->query; |
||
349 | |||
350 | foreach ($filter->initialize as $refMethod) { |
||
351 | $refMethod->invokeArgs($this->instance, [$initializeContainer]); |
||
352 | } |
||
353 | |||
354 | $this->injectedInstance = $this->instance; |
||
355 | $this->instance = null; |
||
356 | } |
||
357 | |||
358 | return $this->execute($method, $arguments); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Viewに注入する |
||
363 | * @param string メソッド名 |
||
364 | * @param array 引数リスト |
||
365 | */ |
||
366 | private function viewInjector($method, $arguments) |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Helperに注入する |
||
396 | * @param string メソッド名 |
||
397 | * @param array 引数リスト |
||
398 | */ |
||
399 | private function helperInjector($method, $arguments) |
||
400 | { |
||
401 | // アノテーション注入処理は1度しか行わない |
||
402 | if ($this->injectedInstance === null) { |
||
403 | $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
404 | |||
405 | // @Filter |
||
406 | $filter = $this->annotation->filter; |
||
407 | |||
408 | // custom annotation |
||
409 | $this->instance->__customAnnotation($this->annotation->customAnnotations); |
||
410 | |||
411 | if ($this->exceptionHandler === null) { |
||
412 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
413 | } |
||
414 | |||
415 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
416 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
417 | $exception = $this->annotation->exception; |
||
418 | if ($exception instanceof ExceptionDelegator) { |
||
419 | if ($this->annotation->exceptionHandler !== null) { |
||
420 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
421 | } |
||
422 | $exception->inject('logger', $this->logger); |
||
423 | $exception->setExceptionHandler($this->annotation->exceptionHandler); |
||
424 | $exception->raise(); |
||
425 | } |
||
426 | |||
427 | foreach ($filter->initialize as $refMethod) { |
||
428 | $refMethod->invokeArgs($this->instance, [$this->container]); |
||
429 | } |
||
430 | |||
431 | $this->injectedInstance = $this->instance; |
||
432 | $this->instance = null; |
||
433 | } |
||
434 | |||
435 | return $this->execute($method, $arguments); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * 実メソッド名を返却する |
||
440 | * @param stirng $method エイリアスメソッド名 |
||
441 | * @return stirng 実メソッド名 |
||
442 | */ |
||
443 | private function getOriginMethod($method) |
||
470 | } |
||
471 | } |
||
472 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths