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 |
||
| 31 | class App implements AppInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Version string. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const VERSION = '2.2.0-DEV'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $booted = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $debug; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Application environment: "dev|development" vs "prod|production". |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $environment; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Psr\Log\LoggerInterface |
||
| 59 | */ |
||
| 60 | protected $logger; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Unix timestamp with microseconds. |
||
| 64 | * |
||
| 65 | * @var float |
||
| 66 | */ |
||
| 67 | protected $startTime; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Configuration loader. |
||
| 71 | * |
||
| 72 | * @var \PPI\Framework\Config\ConfigManager |
||
| 73 | */ |
||
| 74 | protected $configManager; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Module Manager. |
||
| 78 | * |
||
| 79 | * @var \Zend\ModuleManager\ModuleManager |
||
| 80 | */ |
||
| 81 | protected $moduleManager; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param int $errorReportingLevel The level of error reporting you want |
||
| 85 | */ |
||
| 86 | protected $errorReportingLevel; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var null|array |
||
| 90 | */ |
||
| 91 | protected $matchedRoute; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var \PPI\Framework\Module\Controller\ControllerResolver |
||
| 95 | */ |
||
| 96 | protected $resolver; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $name; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Path to the application root dir aka the "app" directory. |
||
| 105 | * |
||
| 106 | * @var null|string |
||
| 107 | */ |
||
| 108 | protected $rootDir; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Service Manager. |
||
| 112 | * |
||
| 113 | * @var \PPI\Framework\ServiceManager\ServiceManager |
||
| 114 | */ |
||
| 115 | protected $serviceManager; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var ChainRouter |
||
| 119 | */ |
||
| 120 | private $router; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * App constructor. |
||
| 124 | * |
||
| 125 | * @param array $options |
||
| 126 | */ |
||
| 127 | public function __construct(array $options = array()) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set an App option. |
||
| 145 | * |
||
| 146 | * @param $option |
||
| 147 | * @param $value |
||
| 148 | * |
||
| 149 | * @throws \RuntimeException |
||
| 150 | * |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function setOption($option, $value) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get an App option. |
||
| 172 | * |
||
| 173 | * @param $option |
||
| 174 | * |
||
| 175 | * @throws \RuntimeException |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getOption($option) |
||
| 189 | |||
| 190 | public function __clone() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Run the boot process, load our modules and their dependencies. |
||
| 202 | * |
||
| 203 | * This method is automatically called by dispatch(), but you can use it |
||
| 204 | * to build all services when not handling a request. |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function boot() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Run the application and send the response. |
||
| 240 | * |
||
| 241 | * @param HttpRequest|null $request |
||
| 242 | * @param HttpResponse|null $request |
||
| 243 | * |
||
| 244 | * @throws \Exception |
||
| 245 | * |
||
| 246 | * @return HttpResponse |
||
| 247 | */ |
||
| 248 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Decide on a route to use and dispatch our module's controller action. |
||
| 262 | * |
||
| 263 | * @param HttpRequest $request |
||
| 264 | * @param HttpResponse $response |
||
| 265 | * |
||
| 266 | * @throws \Exception |
||
| 267 | * |
||
| 268 | * @return HttpResponse |
||
| 269 | */ |
||
| 270 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Gets the name of the application. |
||
| 310 | * |
||
| 311 | * @return string The application name |
||
| 312 | * |
||
| 313 | * @api |
||
| 314 | */ |
||
| 315 | public function getName() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Gets the version of the application. |
||
| 326 | * |
||
| 327 | * @return string The application version |
||
| 328 | * |
||
| 329 | * @api |
||
| 330 | */ |
||
| 331 | public function getVersion() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the environment mode the application is in. |
||
| 338 | * |
||
| 339 | * @return string The current environment |
||
| 340 | * |
||
| 341 | * @api |
||
| 342 | */ |
||
| 343 | public function getEnvironment() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $env |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function isEnvironment($env) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Checks if debug mode is enabled. |
||
| 366 | * |
||
| 367 | * @return bool true if debug mode is enabled, false otherwise |
||
| 368 | * |
||
| 369 | * @api |
||
| 370 | */ |
||
| 371 | public function isDebug() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Gets the application root dir. |
||
| 378 | * |
||
| 379 | * @return string The application root dir |
||
| 380 | * |
||
| 381 | * @api |
||
| 382 | */ |
||
| 383 | public function getRootDir() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the service manager. |
||
| 394 | * |
||
| 395 | * @return ServiceManager |
||
| 396 | */ |
||
| 397 | public function getServiceManager() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
| 404 | * |
||
| 405 | * @return null|ServiceManager |
||
| 406 | */ |
||
| 407 | public function getContainer() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Returns the Module Manager. |
||
| 414 | * |
||
| 415 | * @return \Zend\ModuleManager\ModuleManager |
||
| 416 | */ |
||
| 417 | public function getModuleManager() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get an array of the loaded modules. |
||
| 428 | * |
||
| 429 | * @return array An array of Module objects, keyed by module name |
||
| 430 | */ |
||
| 431 | public function getModules() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
| 438 | * |
||
| 439 | * @param string $name A resource name to locate |
||
| 440 | * @param string $dir A directory where to look for the resource first |
||
| 441 | * @param bool $first Whether to return the first path or paths for all matching bundles |
||
| 442 | * |
||
| 443 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
| 444 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
| 445 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
| 446 | * |
||
| 447 | * @return string|array The absolute path of the resource or an array if $first is false |
||
| 448 | */ |
||
| 449 | public function locateResource($name, $dir = null, $first = true) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Gets the request start time (not available if debug is disabled). |
||
| 456 | * |
||
| 457 | * @return int The request start timestamp |
||
| 458 | * |
||
| 459 | * @api |
||
| 460 | */ |
||
| 461 | public function getStartTime() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the cache directory. |
||
| 468 | * |
||
| 469 | * @return string The cache directory |
||
| 470 | * |
||
| 471 | * @api |
||
| 472 | */ |
||
| 473 | public function getCacheDir() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Gets the log directory. |
||
| 480 | * |
||
| 481 | * @return string The log directory |
||
| 482 | * |
||
| 483 | * @api |
||
| 484 | */ |
||
| 485 | public function getLogDir() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Gets the charset of the application. |
||
| 492 | * |
||
| 493 | * @return string The charset |
||
| 494 | * |
||
| 495 | * @api |
||
| 496 | */ |
||
| 497 | public function getCharset() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Returns a ConfigManager instance. |
||
| 504 | * |
||
| 505 | * @return \PPI\Framework\Config\ConfigManager |
||
| 506 | */ |
||
| 507 | public function getConfigManager() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Loads a configuration file or PHP array. |
||
| 519 | * |
||
| 520 | * @param $resource |
||
| 521 | * @param null $type |
||
| 522 | * |
||
| 523 | * @return App The current instance |
||
| 524 | */ |
||
| 525 | public function loadConfig($resource, $type = null) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Returns the application configuration. |
||
| 534 | * |
||
| 535 | * @throws \RuntimeException |
||
| 536 | * |
||
| 537 | * @return array|object |
||
| 538 | */ |
||
| 539 | public function getConfig() |
||
| 547 | |||
| 548 | public function serialize() |
||
| 552 | |||
| 553 | public function unserialize($data) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns the application parameters. |
||
| 562 | * |
||
| 563 | * @return array An array of application parameters |
||
| 564 | */ |
||
| 565 | protected function getAppParameters() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Gets the environment parameters. |
||
| 583 | * |
||
| 584 | * Only the parameters starting with "PPI__" are considered. |
||
| 585 | * |
||
| 586 | * @return array An array of parameters |
||
| 587 | */ |
||
| 588 | protected function getEnvParameters() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Creates and initializes a ServiceManager instance. |
||
| 602 | * |
||
| 603 | * @return ServiceManager The compiled service manager |
||
| 604 | */ |
||
| 605 | protected function buildServiceManager() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
| 617 | * Otherwise exceptions get thrown. |
||
| 618 | * |
||
| 619 | * @param HttpRequest $request |
||
| 620 | * |
||
| 621 | * @throws \Exception |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | protected function handleRouting(HttpRequest $request) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Logs with an arbitrary level. |
||
| 652 | * |
||
| 653 | * @param mixed $level |
||
| 654 | * @param string $message |
||
| 655 | * @param array $context |
||
| 656 | */ |
||
| 657 | protected function log($level, $message, array $context = array()) |
||
| 667 | } |
||
| 668 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):