| Total Complexity | 63 |
| Total Lines | 482 |
| 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 |
||
| 35 | class CoreExecuteDelegator |
||
| 36 | { |
||
| 37 | use Injector, CommonUtils; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var CoreInterface インスタンス |
||
| 41 | */ |
||
| 42 | private $instance; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var CoreInterface 注入済みインスタンス |
||
| 46 | */ |
||
| 47 | private $injectedInstance; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Container 依存コンテナ |
||
| 51 | */ |
||
| 52 | private $container; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Logger ロガー |
||
| 56 | */ |
||
| 57 | private $logger; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var AnnotationContainer アノテーション |
||
| 61 | */ |
||
| 62 | private $annotation; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array<AnnotationContainer> 例外ハンドラリスト |
||
| 66 | */ |
||
| 67 | private $exceptionHandler; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * constructor |
||
| 71 | */ |
||
| 72 | public function __construct(CoreInterface $instance, Container $container) |
||
| 73 | { |
||
| 74 | $this->instance = $instance; |
||
| 75 | $this->container = $container; |
||
| 76 | $this->logger = $container->logger; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * method missing |
||
| 81 | */ |
||
| 82 | public function __call($method, $arguments) |
||
| 83 | { |
||
| 84 | return $this->run($method, $arguments); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * overload getter |
||
| 89 | */ |
||
| 90 | public function __get($name) |
||
| 91 | { |
||
| 92 | return $this->getInstance()->{$name}; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * 処理を実行する |
||
| 97 | * @param string メソッド名 |
||
| 98 | * @param array 引数リスト |
||
| 99 | */ |
||
| 100 | public function run($method, $arguments = []) |
||
| 101 | { |
||
| 102 | // すでに注入済みのインスタンスの場合、そのまま実行 |
||
| 103 | if ($this->injectedInstance !== null) { |
||
| 104 | return $this->execute($method, $arguments); |
||
| 105 | } |
||
| 106 | |||
| 107 | try { |
||
| 108 | $result = null; |
||
| 109 | if ($this->instance instanceof CoreController) { |
||
| 110 | $this->controllerInjector($this->getOriginMethod($method), $arguments); |
||
| 111 | } elseif ($this->instance instanceof CoreService) { |
||
| 112 | $result = $this->serviceInjector($this->getOriginMethod($method), $arguments); |
||
| 113 | } elseif ($this->instance instanceof CoreModel) { |
||
| 114 | $result = $this->modelInjector($this->getOriginMethod($method), $arguments); |
||
| 115 | } elseif ($this->instance instanceof CoreView) { |
||
| 116 | $this->viewInjector($method, $arguments); |
||
| 117 | } elseif ($this->instance instanceof CoreHelper) { |
||
| 118 | $result = $this->helperInjector($this->getOriginMethod($method), $arguments); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $result; |
||
| 122 | } catch (DoctrineAnnotationException $e) { |
||
| 123 | throw new AnnotationException($e); |
||
| 124 | } catch (DelegateException $e) { |
||
| 125 | // すでにデリゲート済み例外の場合はそのままスロー |
||
| 126 | // カスタムアノテーション定義で発生する |
||
| 127 | throw $e; |
||
| 128 | } catch (\Exception $e) { |
||
| 129 | $exceptionClass = get_class($e); |
||
| 130 | switch ($exceptionClass) { |
||
| 131 | case "Exception": |
||
| 132 | case "LogicException": |
||
| 133 | $e = new ApplicationException($e->getMessage(), 500, $e); |
||
| 134 | break; |
||
| 135 | case "RuntimeException": |
||
| 136 | $e = new SystemException($e->getMessage(), 500, $e); |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | $exception = new ExceptionDelegator($this->getInstance(), $e, $method); |
||
| 141 | $exception->inject('logger', $this->logger); |
||
| 142 | |||
| 143 | if ($this->exceptionHandler !== null) { |
||
| 144 | $exception->setExceptionHandler($this->exceptionHandler); |
||
| 145 | } |
||
| 146 | |||
| 147 | $exception->raise(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * オリジナルのインスタンスを返却する |
||
| 153 | * @return CoreInterface インスタンス |
||
| 154 | */ |
||
| 155 | public function getInstance() |
||
| 156 | { |
||
| 157 | return $this->injectedInstance ?: $this->instance; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * メソッドを実行する |
||
| 162 | * @param string メソッド名 |
||
| 163 | * @param array 引数リスト |
||
| 164 | */ |
||
| 165 | private function execute($method, $arguments) |
||
| 166 | { |
||
| 167 | // serviceの場合、modelの探索に行くためエラーにはしない |
||
| 168 | if (!($this->injectedInstance instanceof CoreService) && !method_exists($this->injectedInstance, $method)) { |
||
| 169 | $class = get_class($this->injectedInstance); |
||
| 170 | throw new MethodNotFoundException("${class}#${method} is not defined."); |
||
| 171 | } |
||
| 172 | |||
| 173 | return call_user_func_array([$this->injectedInstance, $method], $arguments); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Controllerに注入する |
||
| 178 | * @param string メソッド名 |
||
| 179 | * @param array 引数リスト |
||
| 180 | */ |
||
| 181 | private function controllerInjector($method, $arguments) |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Serviceに注入する |
||
| 293 | * @param string メソッド名 |
||
| 294 | * @param array 引数リスト |
||
| 295 | */ |
||
| 296 | private function serviceInjector($method, $arguments) |
||
| 297 | { |
||
| 298 | // アノテーション注入処理は1度しか行わない |
||
| 299 | if ($this->injectedInstance === null) { |
||
| 300 | $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
| 301 | $annotationClosure = function ($classpath) use ($annotation) { |
||
| 302 | if (array_key_exists($classpath, $annotation->annotationInfoList)) { |
||
| 303 | return $annotation->annotationInfoList[$classpath]; |
||
| 304 | } |
||
| 305 | return null; |
||
| 306 | }; |
||
| 307 | |||
| 308 | // @Filter |
||
| 309 | $filter = $annotationClosure(Filter::class); |
||
| 310 | |||
| 311 | // @ExceptionHandler |
||
| 312 | $exceptionHandler = $annotationClosure(ExceptionHandler::class); |
||
| 313 | |||
| 314 | // custom annotation |
||
| 315 | $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList); |
||
| 316 | |||
| 317 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
| 318 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
| 319 | $exception = $annotation->exception; |
||
| 320 | if ($exception instanceof ExceptionDelegator) { |
||
| 321 | $this->exceptionHandler = $exceptionHandler; |
||
| 322 | $exception->raise(); |
||
| 323 | } |
||
| 324 | |||
| 325 | foreach ($filter->initialize as $refMethod) { |
||
| 326 | $refMethod->invokeArgs($this->instance, [$this->container]); |
||
| 327 | } |
||
| 328 | |||
| 329 | $this->injectedInstance = $this->instance; |
||
| 330 | $this->instance = null; |
||
| 331 | } |
||
| 332 | |||
| 333 | return $this->execute($method, $arguments); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Modelに注入する |
||
| 338 | * @param string メソッド名 |
||
| 339 | * @param array 引数リスト |
||
| 340 | */ |
||
| 341 | private function modelInjector($method, $arguments) |
||
| 342 | { |
||
| 343 | // アノテーション注入処理は1度しか行わない |
||
| 344 | if ($this->injectedInstance === null) { |
||
| 345 | $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
| 346 | $annotationClosure = function ($classpath) use ($annotation) { |
||
| 347 | if (array_key_exists($classpath, $annotation->annotationInfoList)) { |
||
| 348 | return $annotation->annotationInfoList[$classpath]; |
||
| 349 | } |
||
| 350 | return null; |
||
| 351 | }; |
||
| 352 | |||
| 353 | // @Filter |
||
| 354 | $filter = $annotationClosure(Filter::class); |
||
| 355 | |||
| 356 | // @ExceptionHandler |
||
| 357 | $exceptionHandler = $annotationClosure(ExceptionHandler::class); |
||
| 358 | |||
| 359 | // @Database |
||
| 360 | $database = $annotationClosure(Database::class); |
||
| 361 | |||
| 362 | // @Query |
||
| 363 | $query = $annotationClosure(Query::class); |
||
| 364 | |||
| 365 | // custom annotation |
||
| 366 | $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList); |
||
| 367 | |||
| 368 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
| 369 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
| 370 | $exception = $annotation->exception; |
||
| 371 | if ($exception instanceof ExceptionDelegator) { |
||
| 372 | $this->exceptionHandler = $exceptionHandler; |
||
| 373 | $exception->raise(); |
||
| 374 | } |
||
| 375 | |||
| 376 | $connectionContainerList = []; |
||
| 377 | foreach (($database ?? []) as $databaseInfo) { |
||
| 378 | $container = new Container(false); |
||
| 379 | $container->filepath = $databaseInfo['filepath']; |
||
| 380 | $container->configPath = $databaseInfo['configPath']; |
||
| 381 | $container->driverClassPath = $databaseInfo['driverClassPath']; |
||
| 382 | $connectionContainerList[] = $container; |
||
| 383 | } |
||
| 384 | |||
| 385 | $connectionContainer = new Container(); |
||
| 386 | $connectionContainer->connectionContainerList = $connectionContainerList; |
||
| 387 | $connectionContainer->register("queryInfo", $query); |
||
| 388 | $connectionContainer->logger = $this->logger; |
||
| 389 | |||
| 390 | foreach ($filter->initialize as $refMethod) { |
||
| 391 | $refMethod->invokeArgs($this->instance, [$connectionContainer]); |
||
| 392 | } |
||
| 393 | |||
| 394 | $this->injectedInstance = $this->instance; |
||
| 395 | $this->instance = null; |
||
| 396 | } |
||
| 397 | |||
| 398 | return $this->execute($method, $arguments); |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Viewに注入する |
||
| 403 | * @param string メソッド名 |
||
| 404 | * @param array 引数リスト |
||
| 405 | */ |
||
| 406 | private function viewInjector($method, $arguments) |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Helperに注入する |
||
| 434 | * @param string メソッド名 |
||
| 435 | * @param array 引数リスト |
||
| 436 | */ |
||
| 437 | private function helperInjector($method, $arguments) |
||
| 438 | { |
||
| 439 | // アノテーション注入処理は1度しか行わない |
||
| 440 | if ($this->injectedInstance === null) { |
||
| 441 | $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method); |
||
| 442 | $annotationClosure = function ($classpath) use ($annotation) { |
||
| 443 | if (array_key_exists($classpath, $annotation->annotationInfoList)) { |
||
| 444 | return $annotation->annotationInfoList[$classpath]; |
||
| 445 | } |
||
| 446 | return null; |
||
| 447 | }; |
||
| 448 | |||
| 449 | // @Filter |
||
| 450 | $filter = $annotationClosure(Filter::class); |
||
| 451 | |||
| 452 | // @ExceptionHandler |
||
| 453 | $exceptionHandler = $annotationClosure(ExceptionHandler::class); |
||
| 454 | |||
| 455 | // custom annotation |
||
| 456 | $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList); |
||
| 457 | |||
| 458 | // 各アノテーションでエラーがあった場合この時点で例外を起こす。 |
||
| 459 | // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない |
||
| 460 | $exception = $annotation->exception; |
||
| 461 | if ($exception instanceof ExceptionDelegator) { |
||
| 462 | $this->exceptionHandler = $exceptionHandler; |
||
| 463 | $exception->raise(); |
||
| 464 | } |
||
| 465 | |||
| 466 | foreach ($filter->initialize as $refMethod) { |
||
| 467 | $refMethod->invokeArgs($this->instance, [$this->container]); |
||
| 468 | } |
||
| 469 | |||
| 470 | $this->injectedInstance = $this->instance; |
||
| 471 | $this->instance = null; |
||
| 472 | } |
||
| 473 | |||
| 474 | return $this->execute($method, $arguments); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * 実メソッド名を返却する |
||
| 479 | * @param stirng $method エイリアスメソッド名 |
||
| 480 | * @return stirng 実メソッド名 |
||
| 481 | */ |
||
| 482 | private function getOriginMethod($method) |
||
| 517 | } |
||
| 518 | } |
||
| 519 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: