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