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', //The App's name, default is "App", use by \Monolog\Logger as the logging channel |
||
| 50 | * 'App.uriPrefix' => '/', // The prefix of api uri path, default is "/" |
||
| 51 | * |
||
| 52 | * //DB.* are the params for PDO::__construct, @see http://php.net/manual/en/pdo.construct.php |
||
| 53 | * 'DB.connection' => 'mysql:dbname=default;host=localhost', |
||
| 54 | * 'DB.username' => 'root', |
||
| 55 | * 'DB.password' => 'root', |
||
| 56 | * 'DB.options' => [], |
||
| 57 | * |
||
| 58 | * |
||
| 59 | * LoggerInterface::class => \DI\object(\Monolog\Logger::class) |
||
| 60 | * ->constructor(\DI\get('App.name')), |
||
| 61 | * // 注意, 系统缓存, 只使用 apc、文件缓存等本地缓存, 不要使用 redis 等分布式缓存 |
||
| 62 | * Cache::class => \DI\object(FilesystemCache::class) |
||
| 63 | * ->constructorParameter('directory', sys_get_temp_dir()) |
||
| 64 | * ]; |
||
| 65 | * ``` |
||
| 66 | * or just the array |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | 89 | static public function createByDefault($conf = []) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return Cache |
||
| 122 | */ |
||
| 123 | 1 | public function getCache() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param Cache $localCache |
||
| 130 | */ |
||
| 131 | public function setCache(Cache $localCache) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * load routes from class |
||
| 138 | * |
||
| 139 | * @param string $className |
||
| 140 | * @param string[] $hooks hook class names |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | 2 | public function loadRoutesFromClass($className, $hooks=[]) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * load routes from path |
||
| 187 | * |
||
| 188 | * 被加载的文件必须以: 类名.php的形式命名 |
||
| 189 | * @param string $fromPath |
||
| 190 | * @param string $namespace |
||
| 191 | * @param string[] $hooks |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | View Code Duplication | public function loadRoutesFromPath($fromPath, $namespace = '', $hooks=[]) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Add route |
||
| 222 | * @param string $method |
||
| 223 | * @param string $uri |
||
| 224 | * @param callable $handler function(Application $app, Request $request):Response |
||
| 225 | * @param string[] $hooks |
||
| 226 | */ |
||
| 227 | 1 | public function addRoute($method, $uri, callable $handler, $hooks=[]) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return ControllerContainer[] |
||
| 234 | */ |
||
| 235 | 1 | public function getControllers() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param Request|null $request |
||
| 246 | * @param bool $send |
||
| 247 | * @return Response |
||
| 248 | */ |
||
| 249 | 3 | public function dispatch(Request $request = null, $send = true) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @return GroupCountBasedDispatcher |
||
| 316 | */ |
||
| 317 | 2 | private function getDispatcher() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Finds an entry of the container by its identifier and returns it. |
||
| 330 | * |
||
| 331 | * @param string $id Identifier of the entry to look for. |
||
| 332 | * |
||
| 333 | * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
||
| 334 | * @throws ContainerExceptionInterface Error while retrieving the entry. |
||
| 335 | * |
||
| 336 | * @return mixed Entry. |
||
| 337 | */ |
||
| 338 | 18 | public function get($id) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Returns true if the container can return an entry for the given identifier. |
||
| 345 | * Returns false otherwise. |
||
| 346 | * |
||
| 347 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
| 348 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
| 349 | * |
||
| 350 | * @param string $id Identifier of the entry to look for. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function has($id) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Call the given function using the given parameters. |
||
| 361 | * |
||
| 362 | * @param callable $callable Function to call. |
||
| 363 | * @param array $parameters Parameters to use. |
||
| 364 | * |
||
| 365 | * @return mixed Result of the function. |
||
| 366 | * |
||
| 367 | * @throws InvocationException Base exception class for all the sub-exceptions below. |
||
| 368 | * @throws NotCallableException |
||
| 369 | * @throws NotEnoughParametersException |
||
| 370 | */ |
||
| 371 | public function call($callable, array $parameters = array()) |
||
| 375 | |||
| 376 | 23 | public function make($name, array $parameters = []) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param \string[] $globalHooks |
||
| 383 | */ |
||
| 384 | public function setGlobalHooks($globalHooks) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return \string[] |
||
| 391 | */ |
||
| 392 | 2 | public function getGlobalHooks() |
|
| 396 | |||
| 397 | public static function createRequestFromGlobals() |
||
| 409 | |||
| 410 | 3 | public function getFullUri($uri) |
|
| 414 | /** |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 3 | public function getUriPrefix() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $uriPrefix |
||
| 424 | */ |
||
| 425 | 89 | public function setUriPrefix($uriPrefix) |
|
| 429 | /** |
||
| 430 | * @var string |
||
| 431 | */ |
||
| 432 | protected $uriPrefix = '/'; |
||
| 433 | /** |
||
| 434 | * @inject |
||
| 435 | * @var Container |
||
| 436 | */ |
||
| 437 | protected $container; |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @inject |
||
| 441 | * @var ControllerContainerBuilder |
||
| 442 | */ |
||
| 443 | protected $controllerContainerBuilder; |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @inject |
||
| 447 | * @var Cache |
||
| 448 | */ |
||
| 449 | protected $cache; |
||
| 450 | |||
| 451 | /** |
||
| 452 | * [ |
||
| 453 | * [method, uri, handler, hooks] |
||
| 454 | * ] |
||
| 455 | * @var array |
||
| 456 | */ |
||
| 457 | protected $routes = []; |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @var string[] |
||
| 461 | */ |
||
| 462 | protected $controllers = []; |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @var string[] |
||
| 466 | */ |
||
| 467 | protected $globalHooks = []; |
||
| 468 | |||
| 469 | } |
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope