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 RequestInterface|null $request |
||
240 | * @param ResponseInterface|null $response |
||
241 | * @return Response |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | public function run(RequestInterface $request = null, ResponseInterface $response = null) |
||
258 | |||
259 | /** |
||
260 | * |
||
261 | * Decide on a route to use and dispatch our module's controller action. |
||
262 | * |
||
263 | * @param RequestInterface $request |
||
264 | * @param ResponseInterface $response |
||
265 | * @return Response |
||
266 | * @throws \Exception |
||
267 | */ |
||
268 | public function dispatch(RequestInterface $request, ResponseInterface $response) |
||
304 | |||
305 | /** |
||
306 | * Gets the name of the application. |
||
307 | * |
||
308 | * @return string The application name |
||
309 | * |
||
310 | * @api |
||
311 | */ |
||
312 | public function getName() |
||
320 | |||
321 | /** |
||
322 | * Gets the version of the application. |
||
323 | * |
||
324 | * @return string The application version |
||
325 | * |
||
326 | * @api |
||
327 | */ |
||
328 | public function getVersion() |
||
332 | |||
333 | /** |
||
334 | * Get the environment mode the application is in. |
||
335 | * |
||
336 | * @return string The current environment |
||
337 | * |
||
338 | * @api |
||
339 | */ |
||
340 | public function getEnvironment() |
||
344 | |||
345 | /** |
||
346 | * @param $env |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function isEnvironment($env) |
||
360 | |||
361 | /** |
||
362 | * Checks if debug mode is enabled. |
||
363 | * |
||
364 | * @return boolean true if debug mode is enabled, false otherwise |
||
365 | * |
||
366 | * @api |
||
367 | */ |
||
368 | public function isDebug() |
||
372 | |||
373 | /** |
||
374 | * Gets the application root dir. |
||
375 | * |
||
376 | * @return string The application root dir |
||
377 | * |
||
378 | * @api |
||
379 | */ |
||
380 | public function getRootDir() |
||
388 | |||
389 | /** |
||
390 | * Get the service manager. |
||
391 | * |
||
392 | * @return ServiceManager\ServiceManager |
||
393 | */ |
||
394 | public function getServiceManager() |
||
398 | |||
399 | /** |
||
400 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
401 | * |
||
402 | * @return null|ServiceManager\ServiceManager |
||
403 | */ |
||
404 | public function getContainer() |
||
408 | |||
409 | /** |
||
410 | * Returns the Module Manager. |
||
411 | * |
||
412 | * @return \Zend\ModuleManager\ModuleManager |
||
413 | */ |
||
414 | public function getModuleManager() |
||
422 | |||
423 | /** |
||
424 | * Get an array of the loaded modules. |
||
425 | * |
||
426 | * @return array An array of Module objects, keyed by module name |
||
427 | */ |
||
428 | public function getModules() |
||
432 | |||
433 | /** |
||
434 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
435 | * |
||
436 | * @param string $name A resource name to locate |
||
437 | * @param string $dir A directory where to look for the resource first |
||
438 | * @param Boolean $first Whether to return the first path or paths for all matching bundles |
||
439 | * |
||
440 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
441 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
442 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
443 | * |
||
444 | * @return string|array The absolute path of the resource or an array if $first is false |
||
445 | */ |
||
446 | public function locateResource($name, $dir = null, $first = true) |
||
450 | |||
451 | /** |
||
452 | * Gets the request start time (not available if debug is disabled). |
||
453 | * |
||
454 | * @return integer The request start timestamp |
||
455 | * |
||
456 | * @api |
||
457 | */ |
||
458 | public function getStartTime() |
||
462 | |||
463 | /** |
||
464 | * Gets the cache directory. |
||
465 | * |
||
466 | * @return string The cache directory |
||
467 | * |
||
468 | * @api |
||
469 | */ |
||
470 | public function getCacheDir() |
||
474 | |||
475 | /** |
||
476 | * Gets the log directory. |
||
477 | * |
||
478 | * @return string The log directory |
||
479 | * |
||
480 | * @api |
||
481 | */ |
||
482 | public function getLogDir() |
||
486 | |||
487 | /** |
||
488 | * Gets the charset of the application. |
||
489 | * |
||
490 | * @return string The charset |
||
491 | * |
||
492 | * @api |
||
493 | */ |
||
494 | public function getCharset() |
||
498 | |||
499 | /** |
||
500 | * Returns a ConfigManager instance. |
||
501 | * |
||
502 | * @return \PPI\Framework\Config\ConfigManager |
||
503 | */ |
||
504 | public function getConfigManager() |
||
513 | |||
514 | /** |
||
515 | * Loads a configuration file or PHP array. |
||
516 | * |
||
517 | * @param $resource |
||
518 | * @param null $type |
||
519 | * |
||
520 | * @return App The current instance |
||
521 | */ |
||
522 | public function loadConfig($resource, $type = null) |
||
528 | |||
529 | /** |
||
530 | * Returns the application configuration. |
||
531 | * |
||
532 | * @throws \RuntimeException |
||
533 | * |
||
534 | * @return array|object |
||
535 | */ |
||
536 | public function getConfig() |
||
544 | |||
545 | public function serialize() |
||
549 | |||
550 | public function unserialize($data) |
||
556 | |||
557 | /** |
||
558 | * Returns the application parameters. |
||
559 | * |
||
560 | * @return array An array of application parameters |
||
561 | */ |
||
562 | protected function getAppParameters() |
||
577 | |||
578 | /** |
||
579 | * Gets the environment parameters. |
||
580 | * |
||
581 | * Only the parameters starting with "PPI__" are considered. |
||
582 | * |
||
583 | * @return array An array of parameters |
||
584 | */ |
||
585 | protected function getEnvParameters() |
||
596 | |||
597 | /** |
||
598 | * Creates and initializes a ServiceManager instance. |
||
599 | * |
||
600 | * @return ServiceManager The compiled service manager |
||
601 | */ |
||
602 | protected function buildServiceManager() |
||
611 | |||
612 | /** |
||
613 | * |
||
614 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
615 | * Otherwise exceptions get thrown |
||
616 | * |
||
617 | * @param RequestInterface $request |
||
618 | * @return array |
||
619 | * |
||
620 | * @throws \Exception |
||
621 | */ |
||
622 | protected function handleRouting(RequestInterface $request) |
||
647 | |||
648 | /** |
||
649 | * Logs with an arbitrary level. |
||
650 | * |
||
651 | * @param mixed $level |
||
652 | * @param string $message |
||
653 | * @param array $context |
||
654 | */ |
||
655 | protected function log($level, $message, array $context = array()) |
||
665 | |||
666 | /** |
||
667 | * Enables the debug tools. |
||
668 | * |
||
669 | * This method registers an error handler and an exception handler. |
||
670 | * |
||
671 | * If the Symfony ClassLoader component is available, a special |
||
672 | * class loader is also registered. |
||
673 | */ |
||
674 | protected function enableDebug() |
||
690 | } |
||
691 |
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.