Total Complexity | 64 |
Total Lines | 456 |
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 |
||
29 | class CoreExecuteDelegator |
||
30 | { |
||
31 | use CommonUtils; |
||
32 | |||
33 | /** |
||
34 | * @var CoreInterface インスタンス |
||
35 | */ |
||
36 | private $instance; |
||
37 | |||
38 | /** |
||
39 | * @var CoreInterface 注入済みインスタンス |
||
40 | */ |
||
41 | private $injectedInstance; |
||
42 | |||
43 | /** |
||
44 | * @var Container 依存コンテナ |
||
45 | */ |
||
46 | private $container; |
||
47 | |||
48 | /** |
||
49 | * @var Logger ロガー |
||
50 | */ |
||
51 | private $logger; |
||
52 | |||
53 | /** |
||
54 | * @var AnnotationContainer アノテーション |
||
55 | */ |
||
56 | private $annotation; |
||
57 | |||
58 | /** |
||
59 | * @var array<AnnotationContainer> 例外ハンドラリスト |
||
60 | */ |
||
61 | private $exceptionHandler; |
||
62 | |||
63 | /** |
||
64 | * constructor |
||
65 | */ |
||
66 | public function __construct(CoreInterface $instance, Container $container) |
||
67 | { |
||
68 | $this->instance = $instance; |
||
69 | $this->container = $container; |
||
70 | $this->logger = $container->logger; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * method missing |
||
75 | */ |
||
76 | public function __call($method, $arguments) |
||
77 | { |
||
78 | return $this->run($method, $arguments); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * overload getter |
||
83 | */ |
||
84 | public function __get($name) |
||
85 | { |
||
86 | return $this->getInstance()->{$name}; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * 処理を実行する |
||
91 | * @param string メソッド名 |
||
92 | * @param array 引数リスト |
||
93 | */ |
||
94 | public function run($method, $arguments = []) |
||
95 | { |
||
96 | // すでに注入済みのインスタンスの場合、そのまま実行 |
||
97 | if ($this->injectedInstance !== null) { |
||
98 | return $this->execute($method, $arguments); |
||
99 | } |
||
100 | |||
101 | try { |
||
102 | $result = null; |
||
103 | if ($this->instance instanceof CoreController) { |
||
104 | $this->controllerInjector($this->getOriginMethod($method), $arguments); |
||
105 | } elseif ($this->instance instanceof CoreService) { |
||
106 | $result = $this->serviceInjector($this->getOriginMethod($method), $arguments); |
||
107 | } elseif ($this->instance instanceof CoreModel) { |
||
108 | $result = $this->modelInjector($this->getOriginMethod($method), $arguments); |
||
109 | } elseif ($this->instance instanceof CoreView) { |
||
110 | $this->viewInjector($method, $arguments); |
||
111 | } elseif ($this->instance instanceof CoreHelper) { |
||
112 | $result = $this->helperInjector($this->getOriginMethod($method), $arguments); |
||
113 | } |
||
114 | |||
115 | return $result; |
||
116 | } catch (DoctrineAnnotationException $e) { |
||
117 | throw new AnnotationException($e); |
||
118 | } catch (DelegateException $e) { |
||
119 | // すでにデリゲート済み例外の場合はそのままスロー |
||
120 | // カスタムアノテーション定義で発生する |
||
121 | throw $e; |
||
122 | } catch (\Exception $e) { |
||
123 | $exceptionClass = get_class($e); |
||
124 | switch ($exceptionClass) { |
||
125 | case "Exception": |
||
126 | case "LogicException": |
||
127 | $e = new ApplicationException($e->getMessage(), 500, $e); |
||
128 | break; |
||
129 | case "RuntimeException": |
||
130 | $e = new SystemException($e->getMessage(), 500, $e); |
||
131 | break; |
||
132 | } |
||
133 | |||
134 | $exception = new ExceptionDelegator($this->getInstance(), $e, $method); |
||
135 | $exception->inject('logger', $this->logger); |
||
136 | |||
137 | if ($this->annotation !== null && is_array($this->annotation->exceptionHandler)) { |
||
138 | $exception->setExceptionHandler($this->annotation->exceptionHandler); |
||
139 | } |
||
140 | $exception->raise(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * オリジナルのインスタンスを返却する |
||
146 | * @return CoreInterface インスタンス |
||
147 | */ |
||
148 | public function getInstance() |
||
149 | { |
||
150 | return $this->injectedInstance ?: $this->instance; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * メソッドを実行する |
||
155 | * @param string メソッド名 |
||
156 | * @param array 引数リスト |
||
157 | */ |
||
158 | private function execute($method, $arguments) |
||
159 | { |
||
160 | // serviceの場合、modelの探索に行くためエラーにはしない |
||
161 | if (!($this->injectedInstance instanceof CoreService) && !method_exists($this->injectedInstance, $method)) { |
||
162 | $class = get_class($this->injectedInstance); |
||
163 | throw new MethodNotFoundException("${class}#${method} is not defined."); |
||
164 | } |
||
165 | |||
166 | return call_user_func_array([$this->injectedInstance, $method], $arguments); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Controllerに注入する |
||
171 | * @param string メソッド名 |
||
172 | * @param array 引数リスト |
||
173 | */ |
||
174 | private function controllerInjector($method, $arguments) |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Serviceに注入する |
||
287 | * @param string メソッド名 |
||
288 | * @param array 引数リスト |
||
289 | */ |
||
290 | private function serviceInjector($method, $arguments) |
||
291 | { |
||
292 | // アノテーション注入処理は1度しか行わない |
||
293 | if ($this->injectedInstance === null) { |
||
294 | $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
295 | |||
296 | // @Filter |
||
297 | $filter = $this->annotation->filter; |
||
298 | |||
299 | // @ExceptionHandler |
||
300 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
301 | |||
302 | // custom annotation |
||
303 | $this->instance->__customAnnotation($this->annotation->customAnnotations); |
||
304 | |||
305 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
306 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
307 | $exception = $this->annotation->exception; |
||
308 | if ($exception instanceof ExceptionDelegator) { |
||
309 | if ($this->annotation->exceptionHandler !== null) { |
||
310 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
311 | } |
||
312 | $exception->inject('logger', $this->logger); |
||
313 | $exception->setExceptionHandler($this->exceptionHandler); |
||
314 | $exception->raise(); |
||
315 | } |
||
316 | |||
317 | foreach ($filter->initialize as $refMethod) { |
||
318 | $refMethod->invokeArgs($this->instance, [$this->container]); |
||
319 | } |
||
320 | |||
321 | $this->injectedInstance = $this->instance; |
||
322 | $this->instance = null; |
||
323 | } |
||
324 | |||
325 | return $this->execute($method, $arguments); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Modelに注入する |
||
330 | * @param string メソッド名 |
||
331 | * @param array 引数リスト |
||
332 | */ |
||
333 | private function modelInjector($method, $arguments) |
||
334 | { |
||
335 | // アノテーション注入処理は1度しか行わない |
||
336 | if ($this->injectedInstance === null) { |
||
337 | $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
338 | |||
339 | // @Filter |
||
340 | $filter = $this->annotation->filter; |
||
341 | |||
342 | // custom annotation |
||
343 | $this->instance->__customAnnotation($this->annotation->customAnnotations); |
||
344 | |||
345 | if ($this->exceptionHandler === null) { |
||
346 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
347 | } |
||
348 | |||
349 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
350 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
351 | $exception = $this->annotation->exception; |
||
352 | if ($exception instanceof ExceptionDelegator) { |
||
353 | if ($this->annotation->exceptionHandler !== null) { |
||
354 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
355 | } |
||
356 | $exception->inject('logger', $this->logger); |
||
357 | $exception->setExceptionHandler($this->annotation->exceptionHandler); |
||
358 | $exception->raise(); |
||
359 | } |
||
360 | |||
361 | $initializeContainer = new Container(false); |
||
362 | $initializeContainer->connectionContainerList = $this->annotation->database; |
||
363 | $initializeContainer->queryAnnotations = $this->annotation->query; |
||
364 | |||
365 | foreach ($filter->initialize as $refMethod) { |
||
366 | $refMethod->invokeArgs($this->instance, [$initializeContainer]); |
||
367 | } |
||
368 | |||
369 | $this->injectedInstance = $this->instance; |
||
370 | $this->instance = null; |
||
371 | } |
||
372 | |||
373 | return $this->execute($method, $arguments); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Viewに注入する |
||
378 | * @param string メソッド名 |
||
379 | * @param array 引数リスト |
||
380 | */ |
||
381 | private function viewInjector($method, $arguments) |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Helperに注入する |
||
411 | * @param string メソッド名 |
||
412 | * @param array 引数リスト |
||
413 | */ |
||
414 | private function helperInjector($method, $arguments) |
||
415 | { |
||
416 | // アノテーション注入処理は1度しか行わない |
||
417 | if ($this->injectedInstance === null) { |
||
418 | $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
419 | |||
420 | // @Filter |
||
421 | $filter = $this->annotation->filter; |
||
422 | |||
423 | // custom annotation |
||
424 | $this->instance->__customAnnotation($this->annotation->customAnnotations); |
||
425 | |||
426 | if ($this->exceptionHandler === null) { |
||
427 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
428 | } |
||
429 | |||
430 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
431 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
432 | $exception = $this->annotation->exception; |
||
433 | if ($exception instanceof ExceptionDelegator) { |
||
434 | if ($this->annotation->exceptionHandler !== null) { |
||
435 | $this->exceptionHandler = $this->annotation->exceptionHandler; |
||
436 | } |
||
437 | $exception->inject('logger', $this->logger); |
||
438 | $exception->setExceptionHandler($this->annotation->exceptionHandler); |
||
439 | $exception->raise(); |
||
440 | } |
||
441 | |||
442 | foreach ($filter->initialize as $refMethod) { |
||
443 | $refMethod->invokeArgs($this->instance, [$this->container]); |
||
444 | } |
||
445 | |||
446 | $this->injectedInstance = $this->instance; |
||
447 | $this->instance = null; |
||
448 | } |
||
449 | |||
450 | return $this->execute($method, $arguments); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * 実メソッド名を返却する |
||
455 | * @param stirng $method エイリアスメソッド名 |
||
456 | * @return stirng 実メソッド名 |
||
457 | */ |
||
458 | private function getOriginMethod($method) |
||
485 | } |
||
486 | } |
||
487 |
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