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 |
||
34 | class App implements AppInterface |
||
35 | { |
||
36 | /** |
||
37 | * Version string. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | const VERSION = '2.1.0-DEV'; |
||
42 | |||
43 | /** |
||
44 | * @var boolean |
||
45 | */ |
||
46 | protected $booted = false; |
||
47 | |||
48 | /** |
||
49 | * @var boolean |
||
50 | */ |
||
51 | protected $debug; |
||
52 | |||
53 | /** |
||
54 | * Application environment: "dev|development" vs "prod|production". |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $environment; |
||
59 | |||
60 | /** |
||
61 | * @var \Psr\Log\LoggerInterface |
||
62 | */ |
||
63 | protected $logger; |
||
64 | |||
65 | /** |
||
66 | * Unix timestamp with microseconds. |
||
67 | * |
||
68 | * @var float |
||
69 | */ |
||
70 | protected $startTime; |
||
71 | |||
72 | /** |
||
73 | * Configuration loader. |
||
74 | * |
||
75 | * @var \PPI\Framework\Config\ConfigManager |
||
76 | */ |
||
77 | protected $configManager; |
||
78 | |||
79 | /** |
||
80 | * The Module Manager. |
||
81 | * |
||
82 | * @var \Zend\ModuleManager\ModuleManager |
||
83 | */ |
||
84 | protected $moduleManager; |
||
85 | |||
86 | /** |
||
87 | * @param integer $errorReportingLevel The level of error reporting you want |
||
88 | */ |
||
89 | protected $errorReportingLevel; |
||
90 | |||
91 | /** |
||
92 | * @var null|array |
||
93 | */ |
||
94 | protected $matchedRoute; |
||
95 | |||
96 | /** |
||
97 | * @var \PPI\Framework\Module\Controller\ControllerResolver |
||
98 | */ |
||
99 | protected $resolver; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $name; |
||
105 | |||
106 | /** |
||
107 | * Path to the application root dir aka the "app" directory. |
||
108 | * |
||
109 | * @var null|string |
||
110 | */ |
||
111 | protected $rootDir; |
||
112 | |||
113 | /** |
||
114 | * Service Manager. |
||
115 | * |
||
116 | * @var \PPI\Framework\ServiceManager\ServiceManager |
||
117 | */ |
||
118 | protected $serviceManager; |
||
119 | |||
120 | /** |
||
121 | * App constructor. |
||
122 | * |
||
123 | * @param array $options |
||
124 | */ |
||
125 | public function __construct(array $options = array()) |
||
140 | |||
141 | /** |
||
142 | * Set an App option. |
||
143 | * |
||
144 | * @param $option |
||
145 | * @param $value |
||
146 | * |
||
147 | * @throws \RuntimeException |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function setOption($option, $value) |
||
167 | |||
168 | /** |
||
169 | * Get an App option. |
||
170 | * |
||
171 | * @param $option |
||
172 | * |
||
173 | * @throws \RuntimeException |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getOption($option) |
||
187 | |||
188 | public function __clone() |
||
197 | |||
198 | /** |
||
199 | * Run the boot process, load our modules and their dependencies. |
||
200 | * |
||
201 | * This method is automatically called by dispatch(), but you can use it |
||
202 | * to build all services when not handling a request. |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function boot() |
||
235 | |||
236 | /** |
||
237 | * Run the application and send the response. |
||
238 | * |
||
239 | * @param HttpRequest|null $request |
||
240 | * @param HttpRequest|null $response |
||
241 | * @return Response |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
258 | |||
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 | * @return Response |
||
266 | * @throws \Exception |
||
267 | */ |
||
268 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
305 | |||
306 | /** |
||
307 | * Gets the name of the application. |
||
308 | * |
||
309 | * @return string The application name |
||
310 | * |
||
311 | * @api |
||
312 | */ |
||
313 | public function getName() |
||
321 | |||
322 | /** |
||
323 | * Gets the version of the application. |
||
324 | * |
||
325 | * @return string The application version |
||
326 | * |
||
327 | * @api |
||
328 | */ |
||
329 | public function getVersion() |
||
333 | |||
334 | /** |
||
335 | * Get the environment mode the application is in. |
||
336 | * |
||
337 | * @return string The current environment |
||
338 | * |
||
339 | * @api |
||
340 | */ |
||
341 | public function getEnvironment() |
||
345 | |||
346 | /** |
||
347 | * @param $env |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | public function isEnvironment($env) |
||
361 | |||
362 | /** |
||
363 | * Checks if debug mode is enabled. |
||
364 | * |
||
365 | * @return boolean true if debug mode is enabled, false otherwise |
||
366 | * |
||
367 | * @api |
||
368 | */ |
||
369 | public function isDebug() |
||
373 | |||
374 | /** |
||
375 | * Gets the application root dir. |
||
376 | * |
||
377 | * @return string The application root dir |
||
378 | * |
||
379 | * @api |
||
380 | */ |
||
381 | public function getRootDir() |
||
389 | |||
390 | /** |
||
391 | * Get the service manager. |
||
392 | * |
||
393 | * @return ServiceManager\ServiceManager |
||
394 | */ |
||
395 | public function getServiceManager() |
||
399 | |||
400 | /** |
||
401 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
402 | * |
||
403 | * @return null|ServiceManager\ServiceManager |
||
404 | */ |
||
405 | public function getContainer() |
||
409 | |||
410 | /** |
||
411 | * Returns the Module Manager. |
||
412 | * |
||
413 | * @return \Zend\ModuleManager\ModuleManager |
||
414 | */ |
||
415 | public function getModuleManager() |
||
423 | |||
424 | /** |
||
425 | * Get an array of the loaded modules. |
||
426 | * |
||
427 | * @return array An array of Module objects, keyed by module name |
||
428 | */ |
||
429 | public function getModules() |
||
433 | |||
434 | /** |
||
435 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
436 | * |
||
437 | * @param string $name A resource name to locate |
||
438 | * @param string $dir A directory where to look for the resource first |
||
439 | * @param Boolean $first Whether to return the first path or paths for all matching bundles |
||
440 | * |
||
441 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
442 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
443 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
444 | * |
||
445 | * @return string|array The absolute path of the resource or an array if $first is false |
||
446 | */ |
||
447 | public function locateResource($name, $dir = null, $first = true) |
||
451 | |||
452 | /** |
||
453 | * Gets the request start time (not available if debug is disabled). |
||
454 | * |
||
455 | * @return integer The request start timestamp |
||
456 | * |
||
457 | * @api |
||
458 | */ |
||
459 | public function getStartTime() |
||
463 | |||
464 | /** |
||
465 | * Gets the cache directory. |
||
466 | * |
||
467 | * @return string The cache directory |
||
468 | * |
||
469 | * @api |
||
470 | */ |
||
471 | public function getCacheDir() |
||
475 | |||
476 | /** |
||
477 | * Gets the log directory. |
||
478 | * |
||
479 | * @return string The log directory |
||
480 | * |
||
481 | * @api |
||
482 | */ |
||
483 | public function getLogDir() |
||
487 | |||
488 | /** |
||
489 | * Gets the charset of the application. |
||
490 | * |
||
491 | * @return string The charset |
||
492 | * |
||
493 | * @api |
||
494 | */ |
||
495 | public function getCharset() |
||
499 | |||
500 | /** |
||
501 | * Returns a ConfigManager instance. |
||
502 | * |
||
503 | * @return \PPI\Framework\Config\ConfigManager |
||
504 | */ |
||
505 | public function getConfigManager() |
||
514 | |||
515 | /** |
||
516 | * Loads a configuration file or PHP array. |
||
517 | * |
||
518 | * @param $resource |
||
519 | * @param null $type |
||
520 | * |
||
521 | * @return App The current instance |
||
522 | */ |
||
523 | public function loadConfig($resource, $type = null) |
||
529 | |||
530 | /** |
||
531 | * Returns the application configuration. |
||
532 | * |
||
533 | * @throws \RuntimeException |
||
534 | * |
||
535 | * @return array|object |
||
536 | */ |
||
537 | public function getConfig() |
||
545 | |||
546 | public function serialize() |
||
550 | |||
551 | public function unserialize($data) |
||
557 | |||
558 | /** |
||
559 | * Returns the application parameters. |
||
560 | * |
||
561 | * @return array An array of application parameters |
||
562 | */ |
||
563 | protected function getAppParameters() |
||
578 | |||
579 | /** |
||
580 | * Gets the environment parameters. |
||
581 | * |
||
582 | * Only the parameters starting with "PPI__" are considered. |
||
583 | * |
||
584 | * @return array An array of parameters |
||
585 | */ |
||
586 | protected function getEnvParameters() |
||
597 | |||
598 | /** |
||
599 | * Creates and initializes a ServiceManager instance. |
||
600 | * |
||
601 | * @return ServiceManager The compiled service manager |
||
602 | */ |
||
603 | protected function buildServiceManager() |
||
612 | |||
613 | /** |
||
614 | * |
||
615 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
616 | * Otherwise exceptions get thrown |
||
617 | * |
||
618 | * @param HttpRequest $request |
||
619 | * @return array |
||
620 | * |
||
621 | * @throws \Exception |
||
622 | */ |
||
623 | protected function handleRouting(HttpRequest $request) |
||
648 | |||
649 | /** |
||
650 | * Logs with an arbitrary level. |
||
651 | * |
||
652 | * @param mixed $level |
||
653 | * @param string $message |
||
654 | * @param array $context |
||
655 | */ |
||
656 | protected function log($level, $message, array $context = array()) |
||
666 | |||
667 | /** |
||
668 | * Enables the debug tools. |
||
669 | * |
||
670 | * This method registers an error handler and an exception handler. |
||
671 | * |
||
672 | * If the Symfony ClassLoader component is available, a special |
||
673 | * class loader is also registered. |
||
674 | */ |
||
675 | protected function enableDebug() |
||
691 | } |
||
692 |
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.