Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Application 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Application implements ContainerInterface, FactoryInterface, \DI\InvokerInterface |
||
| 40 | { |
||
| 41 | use EnableDIAnnotations; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param string|array |
||
| 45 | * .php file |
||
| 46 | * ``` |
||
| 47 | * return |
||
| 48 | * [ |
||
| 49 | * 'App.name' => 'App', |
||
| 50 | * 'App.uriPrefix' => '/', |
||
| 51 | * |
||
| 52 | * 'DB.connection' => 'mysql:dbname=default;host=localhost', |
||
| 53 | * 'DB.username' => 'root', |
||
| 54 | * 'DB.password' => 'root', |
||
| 55 | * 'DB.options' => [], |
||
| 56 | * |
||
| 57 | * |
||
| 58 | * LoggerInterface::class => \DI\object(\Monolog\Logger::class) |
||
| 59 | * ->constructor(\DI\get('AppName')), |
||
| 60 | * // 注意, 系统缓存, 只使用 apc、文件缓存等本地缓存, 不要使用 redis 等分布式缓存 |
||
| 61 | * Cache::class => \DI\object(FilesystemCache::class) |
||
| 62 | * ->constructorParameter('directory', sys_get_temp_dir()) |
||
| 63 | * ]; |
||
| 64 | * ``` |
||
| 65 | * or just the array |
||
| 66 | * @return self |
||
| 67 | */ |
||
| 68 | 72 | static public function createByDefault($conf = []) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return Cache |
||
| 121 | */ |
||
| 122 | 1 | public function getCache() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param Cache $localCache |
||
| 129 | */ |
||
| 130 | public function setCache(Cache $localCache) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * load routes from class |
||
| 137 | * |
||
| 138 | * @param string $className |
||
| 139 | * @param string[] $hooks hook class names |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | 2 | public function loadRoutesFromClass($className, $hooks=[]) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * load routes from path |
||
| 186 | * |
||
| 187 | * 被加载的文件必须以: 类名.php的形式命名 |
||
| 188 | * @param string $fromPath |
||
| 189 | * @param string $namespace |
||
| 190 | * @param string[] $hooks |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function loadRoutesFromPath($fromPath, $namespace = '', $hooks=[]) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add route |
||
| 221 | * @param string $method |
||
| 222 | * @param string $uri |
||
| 223 | * @param callable $handler function(Application $app, Request $request):Response |
||
| 224 | * @param string[] $hooks |
||
| 225 | */ |
||
| 226 | 1 | public function addRoute($method, $uri, callable $handler, $hooks=[]) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return ControllerContainer[] |
||
| 233 | */ |
||
| 234 | 1 | public function getControllers() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param Request|null $request |
||
| 245 | * @param bool $send |
||
| 246 | * @return Response |
||
| 247 | */ |
||
| 248 | 3 | public function dispatch(Request $request = null, $send = true) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @return GroupCountBasedDispatcher |
||
| 315 | */ |
||
| 316 | 2 | private function getDispatcher() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Finds an entry of the container by its identifier and returns it. |
||
| 329 | * |
||
| 330 | * @param string $id Identifier of the entry to look for. |
||
| 331 | * |
||
| 332 | * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
||
| 333 | * @throws ContainerExceptionInterface Error while retrieving the entry. |
||
| 334 | * |
||
| 335 | * @return mixed Entry. |
||
| 336 | */ |
||
| 337 | 18 | public function get($id) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Returns true if the container can return an entry for the given identifier. |
||
| 344 | * Returns false otherwise. |
||
| 345 | * |
||
| 346 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
| 347 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
| 348 | * |
||
| 349 | * @param string $id Identifier of the entry to look for. |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function has($id) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Call the given function using the given parameters. |
||
| 360 | * |
||
| 361 | * @param callable $callable Function to call. |
||
| 362 | * @param array $parameters Parameters to use. |
||
| 363 | * |
||
| 364 | * @return mixed Result of the function. |
||
| 365 | * |
||
| 366 | * @throws InvocationException Base exception class for all the sub-exceptions below. |
||
| 367 | * @throws NotCallableException |
||
| 368 | * @throws NotEnoughParametersException |
||
| 369 | */ |
||
| 370 | public function call($callable, array $parameters = array()) |
||
| 374 | |||
| 375 | 22 | public function make($name, array $parameters = []) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param \string[] $globalHooks |
||
| 382 | */ |
||
| 383 | public function setGlobalHooks($globalHooks) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return \string[] |
||
| 390 | */ |
||
| 391 | 2 | public function getGlobalHooks() |
|
| 395 | |||
| 396 | public static function createRequestFromGlobals() |
||
| 408 | |||
| 409 | 3 | public function getFullUri($uri) |
|
| 413 | /** |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | 3 | public function getUriPrefix() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $uriPrefix |
||
| 423 | */ |
||
| 424 | 72 | public function setUriPrefix($uriPrefix) |
|
| 428 | /** |
||
| 429 | * @var string |
||
| 430 | */ |
||
| 431 | protected $uriPrefix = '/'; |
||
| 432 | /** |
||
| 433 | * @inject |
||
| 434 | * @var Container |
||
| 435 | */ |
||
| 436 | protected $container; |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @inject |
||
| 440 | * @var ControllerContainerBuilder |
||
| 441 | */ |
||
| 442 | protected $controllerContainerBuilder; |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @inject |
||
| 446 | * @var Cache |
||
| 447 | */ |
||
| 448 | protected $cache; |
||
| 449 | |||
| 450 | /** |
||
| 451 | * [ |
||
| 452 | * [method, uri, handler, hooks] |
||
| 453 | * ] |
||
| 454 | * @var array |
||
| 455 | */ |
||
| 456 | protected $routes = []; |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @var string[] |
||
| 460 | */ |
||
| 461 | protected $controllers = []; |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @var string[] |
||
| 465 | */ |
||
| 466 | protected $globalHooks = []; |
||
| 467 | |||
| 468 | } |