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()) |
||
| 129 | { |
||
| 130 | // Default options |
||
| 131 | $this->environment = isset($options['environment']) && $options['environment'] ? (string) $options['environment'] : 'prod'; |
||
| 132 | $this->debug = isset($options['debug']) && null !== $options['debug'] ? (boolean) $options['debug'] : false; |
||
| 133 | $this->rootDir = isset($options['rootDir']) && $options['rootDir'] ? (string) $options['rootDir'] : $this->getRootDir(); |
||
| 134 | $this->name = isset($options['name']) && $options['name'] ? (string) $options['name'] : $this->getName(); |
||
| 135 | |||
| 136 | if ($this->debug) { |
||
| 137 | $this->startTime = microtime(true); |
||
| 138 | Debug::enable(); |
||
| 139 | } else { |
||
| 140 | ini_set('display_errors', 0); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set an App option. |
||
| 146 | * |
||
| 147 | * @param $option |
||
| 148 | * @param $value |
||
| 149 | * |
||
| 150 | * @throws \RuntimeException |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function setOption($option, $value) |
||
| 155 | { |
||
| 156 | if (true === $this->booted) { |
||
| 157 | throw new \RuntimeException('Setting App options after boot() is now allowed'); |
||
| 158 | } |
||
| 159 | |||
| 160 | // "root_dir" to "rootDir" |
||
| 161 | $property = preg_replace('/_(.?)/e', "strtoupper('$1')", $option); |
||
| 162 | if (!property_exists($this, $property)) { |
||
| 163 | throw new \RuntimeException(sprintf('App property "%s" (option "%s") does not exist', $property, $option)); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->$property = $value; |
||
| 167 | |||
| 168 | return $this; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get an App option. |
||
| 173 | * |
||
| 174 | * @param $option |
||
| 175 | * |
||
| 176 | * @throws \RuntimeException |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getOption($option) |
||
| 181 | { |
||
| 182 | // "root_dir" to "rootDir" |
||
| 183 | $property = preg_replace('/_(.?)/e', "strtoupper('$1')", $option); |
||
| 184 | if (!property_exists($this, $property)) { |
||
| 185 | throw new \RuntimeException(sprintf('App property "%s" (option "%s") does not exist', $property, $option)); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $property; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function __clone() |
||
| 192 | { |
||
| 193 | if ($this->debug) { |
||
| 194 | $this->startTime = microtime(true); |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->booted = false; |
||
| 198 | $this->serviceManager = null; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Run the boot process, load our modules and their dependencies. |
||
| 203 | * |
||
| 204 | * This method is automatically called by dispatch(), but you can use it |
||
| 205 | * to build all services when not handling a request. |
||
| 206 | * |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | public function boot() |
||
| 210 | { |
||
| 211 | if (true === $this->booted) { |
||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | $this->serviceManager = $this->buildServiceManager(); |
||
| 216 | $this->log('debug', sprintf('Booting %s ...', $this->name)); |
||
| 217 | |||
| 218 | // Loading our Modules |
||
| 219 | $this->getModuleManager()->loadModules(); |
||
| 220 | if ($this->debug) { |
||
| 221 | $modules = $this->getModuleManager()->getModules(); |
||
| 222 | $this->log('debug', sprintf('All modules online (%d): "%s"', count($modules), implode('", "', $modules))); |
||
| 223 | } |
||
| 224 | |||
| 225 | // Lets get all the services our of our modules and start setting them in the ServiceManager |
||
| 226 | $moduleServices = $this->serviceManager->get('ModuleDefaultListener')->getServices(); |
||
| 227 | foreach ($moduleServices as $key => $service) { |
||
| 228 | $this->serviceManager->setFactory($key, $service); |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->booted = true; |
||
| 232 | if ($this->debug) { |
||
| 233 | $this->log('debug', sprintf('%s has booted (in %.3f secs)', $this->name, microtime(true) - $this->startTime)); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $this; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Run the application and send the response. |
||
| 241 | * |
||
| 242 | * @param HttpRequest|null $request |
||
| 243 | * @param HttpResponse|null $request |
||
| 244 | * |
||
| 245 | * @throws \Exception |
||
| 246 | * |
||
| 247 | * @return HttpResponse |
||
| 248 | */ |
||
| 249 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
| 250 | { |
||
| 251 | if (false === $this->booted) { |
||
| 252 | $this->boot(); |
||
| 253 | } |
||
| 254 | |||
| 255 | $response = $this->dispatch($request, $response); |
||
| 256 | $response->send(); |
||
| 257 | |||
| 258 | return $response; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Decide on a route to use and dispatch our module's controller action. |
||
| 263 | * |
||
| 264 | * @param HttpRequest $request |
||
| 265 | * @param HttpResponse $response |
||
| 266 | * |
||
| 267 | * @throws \Exception |
||
| 268 | * |
||
| 269 | * @return HttpResponse |
||
| 270 | */ |
||
| 271 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
| 272 | { |
||
| 273 | if (false === $this->booted) { |
||
| 274 | $this->boot(); |
||
| 275 | } |
||
| 276 | |||
| 277 | // Routing |
||
| 278 | $routeParams = $this->handleRouting($request); |
||
| 279 | $request->attributes->add($routeParams); |
||
| 280 | |||
| 281 | // Resolve our Controller |
||
| 282 | $resolver = $this->serviceManager->get('ControllerResolver'); |
||
| 283 | if (false === $controller = $resolver->getController($request)) { |
||
| 284 | throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s".', $request->getPathInfo())); |
||
| 285 | } |
||
| 286 | |||
| 287 | $controllerArguments = $resolver->getArguments($request, $controller); |
||
| 288 | |||
| 289 | $result = call_user_func_array( |
||
| 290 | $controller, |
||
| 291 | $controllerArguments |
||
| 292 | ); |
||
| 293 | |||
| 294 | if ($result === null) { |
||
| 295 | throw new \Exception('Your action returned null. It must always return something'); |
||
| 296 | } elseif (is_string($result)) { |
||
| 297 | $response->setContent($result); |
||
| 298 | } elseif ($result instanceof SymfonyResponse || $response instanceof HttpResponse) { |
||
| 299 | $response = $result; |
||
| 300 | } else { |
||
| 301 | throw new \Exception('Invalid response type returned from controller'); |
||
| 302 | } |
||
| 303 | |||
| 304 | $this->response = $response; |
||
| 305 | |||
| 306 | return $response; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Gets the name of the application. |
||
| 311 | * |
||
| 312 | * @return string The application name |
||
| 313 | * |
||
| 314 | * @api |
||
| 315 | */ |
||
| 316 | public function getName() |
||
| 317 | { |
||
| 318 | if (null === $this->name) { |
||
| 319 | $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir)); |
||
| 320 | } |
||
| 321 | |||
| 322 | return $this->name; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Gets the version of the application. |
||
| 327 | * |
||
| 328 | * @return string The application version |
||
| 329 | * |
||
| 330 | * @api |
||
| 331 | */ |
||
| 332 | public function getVersion() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the environment mode the application is in. |
||
| 339 | * |
||
| 340 | * @return string The current environment |
||
| 341 | * |
||
| 342 | * @api |
||
| 343 | */ |
||
| 344 | public function getEnvironment() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param $env |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function isEnvironment($env) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Checks if debug mode is enabled. |
||
| 367 | * |
||
| 368 | * @return bool true if debug mode is enabled, false otherwise |
||
| 369 | * |
||
| 370 | * @api |
||
| 371 | */ |
||
| 372 | public function isDebug() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Gets the application root dir. |
||
| 379 | * |
||
| 380 | * @return string The application root dir |
||
| 381 | * |
||
| 382 | * @api |
||
| 383 | */ |
||
| 384 | public function getRootDir() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get the service manager. |
||
| 395 | * |
||
| 396 | * @return ServiceManager |
||
| 397 | */ |
||
| 398 | public function getServiceManager() |
||
| 399 | { |
||
| 400 | return $this->serviceManager; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
| 405 | * |
||
| 406 | * @return null|ServiceManager |
||
| 407 | */ |
||
| 408 | public function getContainer() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns the Module Manager. |
||
| 415 | * |
||
| 416 | * @return \Zend\ModuleManager\ModuleManager |
||
| 417 | */ |
||
| 418 | public function getModuleManager() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get an array of the loaded modules. |
||
| 429 | * |
||
| 430 | * @return array An array of Module objects, keyed by module name |
||
| 431 | */ |
||
| 432 | public function getModules() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
| 439 | * |
||
| 440 | * @param string $name A resource name to locate |
||
| 441 | * @param string $dir A directory where to look for the resource first |
||
| 442 | * @param bool $first Whether to return the first path or paths for all matching bundles |
||
| 443 | * |
||
| 444 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
| 445 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
| 446 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
| 447 | * |
||
| 448 | * @return string|array The absolute path of the resource or an array if $first is false |
||
| 449 | */ |
||
| 450 | public function locateResource($name, $dir = null, $first = true) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Gets the request start time (not available if debug is disabled). |
||
| 457 | * |
||
| 458 | * @return int The request start timestamp |
||
| 459 | * |
||
| 460 | * @api |
||
| 461 | */ |
||
| 462 | public function getStartTime() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Gets the cache directory. |
||
| 469 | * |
||
| 470 | * @return string The cache directory |
||
| 471 | * |
||
| 472 | * @api |
||
| 473 | */ |
||
| 474 | public function getCacheDir() |
||
| 475 | { |
||
| 476 | return $this->rootDir . '/cache/' . $this->environment; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Gets the log directory. |
||
| 481 | * |
||
| 482 | * @return string The log directory |
||
| 483 | * |
||
| 484 | * @api |
||
| 485 | */ |
||
| 486 | public function getLogDir() |
||
| 487 | { |
||
| 488 | return $this->rootDir . '/logs'; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Gets the charset of the application. |
||
| 493 | * |
||
| 494 | * @return string The charset |
||
| 495 | * |
||
| 496 | * @api |
||
| 497 | */ |
||
| 498 | public function getCharset() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns a ConfigManager instance. |
||
| 505 | * |
||
| 506 | * @return \PPI\Framework\Config\ConfigManager |
||
| 507 | */ |
||
| 508 | public function getConfigManager() |
||
| 509 | { |
||
| 510 | if (null === $this->configManager) { |
||
| 511 | $cachePath = $this->getCacheDir() . '/application-config-cache.' . $this->getName() . '.php'; |
||
| 512 | $this->configManager = new ConfigManager($cachePath, !$this->debug, $this->rootDir . '/config'); |
||
| 513 | } |
||
| 514 | |||
| 515 | return $this->configManager; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Loads a configuration file or PHP array. |
||
| 520 | * |
||
| 521 | * @param $resource |
||
| 522 | * @param null $type |
||
| 523 | * |
||
| 524 | * @return App The current instance |
||
| 525 | */ |
||
| 526 | public function loadConfig($resource, $type = null) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Returns the application configuration. |
||
| 535 | * |
||
| 536 | * @throws \RuntimeException |
||
| 537 | * |
||
| 538 | * @return array|object |
||
| 539 | */ |
||
| 540 | public function getConfig() |
||
| 548 | |||
| 549 | public function serialize() |
||
| 553 | |||
| 554 | public function unserialize($data) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Returns the application parameters. |
||
| 563 | * |
||
| 564 | * @return array An array of application parameters |
||
| 565 | */ |
||
| 566 | protected function getAppParameters() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Gets the environment parameters. |
||
| 584 | * |
||
| 585 | * Only the parameters starting with "PPI__" are considered. |
||
| 586 | * |
||
| 587 | * @return array An array of parameters |
||
| 588 | */ |
||
| 589 | protected function getEnvParameters() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Creates and initializes a ServiceManager instance. |
||
| 603 | * |
||
| 604 | * @return ServiceManager The compiled service manager |
||
| 605 | */ |
||
| 606 | protected function buildServiceManager() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
| 618 | * Otherwise exceptions get thrown. |
||
| 619 | * |
||
| 620 | * @param HttpRequest $request |
||
| 621 | * |
||
| 622 | * @throws \Exception |
||
| 623 | * |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | protected function handleRouting(HttpRequest $request) |
||
| 627 | { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Logs with an arbitrary level. |
||
| 653 | * |
||
| 654 | * @param mixed $level |
||
| 655 | * @param string $message |
||
| 656 | * @param array $context |
||
| 657 | */ |
||
| 658 | protected function log($level, $message, array $context = array()) |
||
| 668 | } |
||
| 669 |
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):