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 | * App constructor. |
||
| 119 | * |
||
| 120 | * @param array $options |
||
| 121 | */ |
||
| 122 | public function __construct(array $options = array()) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set an App option. |
||
| 140 | * |
||
| 141 | * @param $option |
||
| 142 | * @param $value |
||
| 143 | * |
||
| 144 | * @throws \RuntimeException |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function setOption($option, $value) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get an App option. |
||
| 167 | * |
||
| 168 | * @param $option |
||
| 169 | * |
||
| 170 | * @throws \RuntimeException |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getOption($option) |
||
| 184 | |||
| 185 | public function __clone() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Run the boot process, load our modules and their dependencies. |
||
| 197 | * |
||
| 198 | * This method is automatically called by dispatch(), but you can use it |
||
| 199 | * to build all services when not handling a request. |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function boot() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Run the application and send the response. |
||
| 235 | * |
||
| 236 | * @param HttpRequest|null $request |
||
| 237 | * @param HttpRequest|null $request |
||
| 238 | * |
||
| 239 | * @throws \Exception |
||
| 240 | * |
||
| 241 | * @return Response |
||
| 242 | */ |
||
| 243 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Decide on a route to use and dispatch our module's controller action. |
||
| 257 | * |
||
| 258 | * @param HttpRequest $request |
||
| 259 | * @param HttpResponse $response |
||
| 260 | * |
||
| 261 | * @throws \Exception |
||
| 262 | * |
||
| 263 | * @return Response |
||
| 264 | */ |
||
| 265 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets the name of the application. |
||
| 305 | * |
||
| 306 | * @return string The application name |
||
| 307 | * |
||
| 308 | * @api |
||
| 309 | */ |
||
| 310 | public function getName() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Gets the version of the application. |
||
| 321 | * |
||
| 322 | * @return string The application version |
||
| 323 | * |
||
| 324 | * @api |
||
| 325 | */ |
||
| 326 | public function getVersion() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the environment mode the application is in. |
||
| 333 | * |
||
| 334 | * @return string The current environment |
||
| 335 | * |
||
| 336 | * @api |
||
| 337 | */ |
||
| 338 | public function getEnvironment() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param $env |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function isEnvironment($env) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Checks if debug mode is enabled. |
||
| 361 | * |
||
| 362 | * @return bool true if debug mode is enabled, false otherwise |
||
| 363 | * |
||
| 364 | * @api |
||
| 365 | */ |
||
| 366 | public function isDebug() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Gets the application root dir. |
||
| 373 | * |
||
| 374 | * @return string The application root dir |
||
| 375 | * |
||
| 376 | * @api |
||
| 377 | */ |
||
| 378 | public function getRootDir() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the service manager. |
||
| 389 | * |
||
| 390 | * @return ServiceManager\ServiceManager |
||
| 391 | */ |
||
| 392 | public function getServiceManager() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
| 399 | * |
||
| 400 | * @return null|ServiceManager\ServiceManager |
||
| 401 | */ |
||
| 402 | public function getContainer() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns the Module Manager. |
||
| 409 | * |
||
| 410 | * @return \Zend\ModuleManager\ModuleManager |
||
| 411 | */ |
||
| 412 | public function getModuleManager() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get an array of the loaded modules. |
||
| 423 | * |
||
| 424 | * @return array An array of Module objects, keyed by module name |
||
| 425 | */ |
||
| 426 | public function getModules() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
| 433 | * |
||
| 434 | * @param string $name A resource name to locate |
||
| 435 | * @param string $dir A directory where to look for the resource first |
||
| 436 | * @param bool $first Whether to return the first path or paths for all matching bundles |
||
| 437 | * |
||
| 438 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
| 439 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
| 440 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
| 441 | * |
||
| 442 | * @return string|array The absolute path of the resource or an array if $first is false |
||
| 443 | */ |
||
| 444 | public function locateResource($name, $dir = null, $first = true) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Gets the request start time (not available if debug is disabled). |
||
| 451 | * |
||
| 452 | * @return int The request start timestamp |
||
| 453 | * |
||
| 454 | * @api |
||
| 455 | */ |
||
| 456 | public function getStartTime() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Gets the cache directory. |
||
| 463 | * |
||
| 464 | * @return string The cache directory |
||
| 465 | * |
||
| 466 | * @api |
||
| 467 | */ |
||
| 468 | public function getCacheDir() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Gets the log directory. |
||
| 475 | * |
||
| 476 | * @return string The log directory |
||
| 477 | * |
||
| 478 | * @api |
||
| 479 | */ |
||
| 480 | public function getLogDir() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Gets the charset of the application. |
||
| 487 | * |
||
| 488 | * @return string The charset |
||
| 489 | * |
||
| 490 | * @api |
||
| 491 | */ |
||
| 492 | public function getCharset() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns a ConfigManager instance. |
||
| 499 | * |
||
| 500 | * @return \PPI\Framework\Config\ConfigManager |
||
| 501 | */ |
||
| 502 | public function getConfigManager() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Loads a configuration file or PHP array. |
||
| 514 | * |
||
| 515 | * @param $resource |
||
| 516 | * @param null $type |
||
| 517 | * |
||
| 518 | * @return App The current instance |
||
| 519 | */ |
||
| 520 | public function loadConfig($resource, $type = null) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns the application configuration. |
||
| 529 | * |
||
| 530 | * @throws \RuntimeException |
||
| 531 | * |
||
| 532 | * @return array|object |
||
| 533 | */ |
||
| 534 | public function getConfig() |
||
| 542 | |||
| 543 | public function serialize() |
||
| 547 | |||
| 548 | public function unserialize($data) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Returns the application parameters. |
||
| 557 | * |
||
| 558 | * @return array An array of application parameters |
||
| 559 | */ |
||
| 560 | protected function getAppParameters() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Gets the environment parameters. |
||
| 578 | * |
||
| 579 | * Only the parameters starting with "PPI__" are considered. |
||
| 580 | * |
||
| 581 | * @return array An array of parameters |
||
| 582 | */ |
||
| 583 | protected function getEnvParameters() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Creates and initializes a ServiceManager instance. |
||
| 597 | * |
||
| 598 | * @return ServiceManager The compiled service manager |
||
| 599 | */ |
||
| 600 | protected function buildServiceManager() |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
| 612 | * Otherwise exceptions get thrown. |
||
| 613 | * |
||
| 614 | * @param HttpRequest $request |
||
| 615 | * |
||
| 616 | * @throws \Exception |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | protected function handleRouting(HttpRequest $request) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Logs with an arbitrary level. |
||
| 647 | * |
||
| 648 | * @param mixed $level |
||
| 649 | * @param string $message |
||
| 650 | * @param array $context |
||
| 651 | */ |
||
| 652 | protected function log($level, $message, array $context = array()) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Enables the debug tools. |
||
| 665 | * |
||
| 666 | * This method registers an error handler and an exception handler. |
||
| 667 | * |
||
| 668 | * If the Symfony ClassLoader component is available, a special |
||
| 669 | * class loader is also registered. |
||
| 670 | */ |
||
| 671 | protected function enableDebug() |
||
| 687 | } |
||
| 688 |
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):