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 App 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 App, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class App implements AppInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Version string. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | const VERSION = '2.2.0-DEV'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $booted = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $debug; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Application environment: "dev|development" vs "prod|production". |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $environment; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \Psr\Log\LoggerInterface |
||
| 60 | */ |
||
| 61 | protected $logger; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Unix timestamp with microseconds. |
||
| 65 | * |
||
| 66 | * @var float |
||
| 67 | */ |
||
| 68 | protected $startTime; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Configuration loader. |
||
| 72 | * |
||
| 73 | * @var \PPI\Framework\Config\ConfigManager |
||
| 74 | */ |
||
| 75 | protected $configManager; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The Module Manager. |
||
| 79 | * |
||
| 80 | * @var \Zend\ModuleManager\ModuleManager |
||
| 81 | */ |
||
| 82 | protected $moduleManager; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param int $errorReportingLevel The level of error reporting you want |
||
| 86 | */ |
||
| 87 | protected $errorReportingLevel; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var null|array |
||
| 91 | */ |
||
| 92 | protected $matchedRoute; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var \PPI\Framework\Module\Controller\ControllerResolver |
||
| 96 | */ |
||
| 97 | protected $resolver; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $name; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Path to the application root dir aka the "app" directory. |
||
| 106 | * |
||
| 107 | * @var null|string |
||
| 108 | */ |
||
| 109 | protected $rootDir; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Service Manager. |
||
| 113 | * |
||
| 114 | * @var \PPI\Framework\ServiceManager\ServiceManager |
||
| 115 | */ |
||
| 116 | protected $serviceManager; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var ChainRouter |
||
| 120 | */ |
||
| 121 | private $router; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * App constructor. |
||
| 125 | * |
||
| 126 | * @param array $options |
||
| 127 | */ |
||
| 128 | public function __construct(array $options = array()) |
||
| 143 | |||
| 144 | public function __clone() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Run the boot process, load our modules and their dependencies. |
||
| 156 | * |
||
| 157 | * This method is automatically called by dispatch(), but you can use it |
||
| 158 | * to build all services when not handling a request. |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function boot() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Run the application and send the response. |
||
| 194 | * |
||
| 195 | * @param HttpRequest|null $request |
||
| 196 | * @param HttpResponse|null $response |
||
| 197 | * |
||
| 198 | * @throws \Exception |
||
| 199 | * |
||
| 200 | * @return HttpResponse |
||
| 201 | */ |
||
| 202 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Decide on a route to use and dispatch our module's controller action. |
||
| 224 | * |
||
| 225 | * @param HttpRequest $request |
||
| 226 | * @param HttpResponse $response |
||
| 227 | * |
||
| 228 | * @throws \Exception |
||
| 229 | * |
||
| 230 | * @return HttpResponse |
||
| 231 | */ |
||
| 232 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets the name of the application. |
||
| 270 | * |
||
| 271 | * @return string The application name |
||
| 272 | * |
||
| 273 | * @api |
||
| 274 | */ |
||
| 275 | public function getName() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Gets the version of the application. |
||
| 286 | * |
||
| 287 | * @return string The application version |
||
| 288 | * |
||
| 289 | * @api |
||
| 290 | */ |
||
| 291 | public function getVersion() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the environment mode the application is in. |
||
| 298 | * |
||
| 299 | * @return string The current environment |
||
| 300 | * |
||
| 301 | * @api |
||
| 302 | */ |
||
| 303 | public function getEnvironment() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param $env |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function isEnvironment($env) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Checks if debug mode is enabled. |
||
| 326 | * |
||
| 327 | * @return bool true if debug mode is enabled, false otherwise |
||
| 328 | * |
||
| 329 | * @api |
||
| 330 | */ |
||
| 331 | public function isDebug() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Gets the application root dir. |
||
| 338 | * |
||
| 339 | * @return string The application root dir |
||
| 340 | * |
||
| 341 | * @api |
||
| 342 | */ |
||
| 343 | public function getRootDir() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the service manager. |
||
| 354 | * |
||
| 355 | * @return ServiceManager |
||
| 356 | */ |
||
| 357 | public function getServiceManager() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
| 364 | * |
||
| 365 | * @return null|ServiceManager |
||
| 366 | */ |
||
| 367 | public function getContainer() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the Module Manager. |
||
| 374 | * |
||
| 375 | * @return \Zend\ModuleManager\ModuleManager |
||
| 376 | */ |
||
| 377 | public function getModuleManager() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get an array of the loaded modules. |
||
| 388 | * |
||
| 389 | * @return array An array of Module objects, keyed by module name |
||
| 390 | */ |
||
| 391 | public function getModules() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
| 398 | * |
||
| 399 | * @param string $name A resource name to locate |
||
| 400 | * @param string $dir A directory where to look for the resource first |
||
| 401 | * @param bool $first Whether to return the first path or paths for all matching bundles |
||
| 402 | * |
||
| 403 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
| 404 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
| 405 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
| 406 | * |
||
| 407 | * @return string|array The absolute path of the resource or an array if $first is false |
||
| 408 | */ |
||
| 409 | public function locateResource($name, $dir = null, $first = true) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Gets the request start time (not available if debug is disabled). |
||
| 416 | * |
||
| 417 | * @return int The request start timestamp |
||
| 418 | * |
||
| 419 | * @api |
||
| 420 | */ |
||
| 421 | public function getStartTime() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Gets the cache directory. |
||
| 428 | * |
||
| 429 | * @return string The cache directory |
||
| 430 | * |
||
| 431 | * @api |
||
| 432 | */ |
||
| 433 | public function getCacheDir() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Gets the log directory. |
||
| 440 | * |
||
| 441 | * @return string The log directory |
||
| 442 | * |
||
| 443 | * @api |
||
| 444 | */ |
||
| 445 | public function getLogDir() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Gets the charset of the application. |
||
| 452 | * |
||
| 453 | * @return string The charset |
||
| 454 | * |
||
| 455 | * @api |
||
| 456 | */ |
||
| 457 | public function getCharset() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns a ConfigManager instance. |
||
| 464 | * |
||
| 465 | * @return \PPI\Framework\Config\ConfigManager |
||
| 466 | */ |
||
| 467 | public function getConfigManager() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Loads a configuration file or PHP array. |
||
| 479 | * |
||
| 480 | * @param $resource |
||
| 481 | * @param null $type |
||
| 482 | * |
||
| 483 | * @return App The current instance |
||
| 484 | */ |
||
| 485 | public function loadConfig($resource, $type = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Returns the application configuration. |
||
| 494 | * |
||
| 495 | * @throws \RuntimeException |
||
| 496 | * |
||
| 497 | * @return array|object |
||
| 498 | */ |
||
| 499 | public function getConfig() |
||
| 507 | |||
| 508 | public function serialize() |
||
| 512 | |||
| 513 | public function unserialize($data) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns the application parameters. |
||
| 522 | * |
||
| 523 | * @return array An array of application parameters |
||
| 524 | */ |
||
| 525 | protected function getAppParameters() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Gets the environment parameters. |
||
| 543 | * |
||
| 544 | * Only the parameters starting with "PPI__" are considered. |
||
| 545 | * |
||
| 546 | * @return array An array of parameters |
||
| 547 | */ |
||
| 548 | protected function getEnvParameters() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Creates and initializes a ServiceManager instance. |
||
| 562 | * |
||
| 563 | * @return ServiceManager The compiled service manager |
||
| 564 | */ |
||
| 565 | protected function buildServiceManager() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
| 577 | * Otherwise exceptions get thrown. |
||
| 578 | * |
||
| 579 | * @param HttpRequest $request |
||
| 580 | * |
||
| 581 | * @throws \Exception |
||
| 582 | * |
||
| 583 | * @return array |
||
| 584 | */ |
||
| 585 | protected function handleRouting(HttpRequest $request) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Logs with an arbitrary level. |
||
| 612 | * |
||
| 613 | * @param mixed $level |
||
| 614 | * @param string $message |
||
| 615 | * @param array $context |
||
| 616 | */ |
||
| 617 | protected function log($level, $message, array $context = array()) |
||
| 627 | } |
||
| 628 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.