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 |
||
43 | class App implements AppInterface |
||
44 | { |
||
45 | /** |
||
46 | * Version string. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | const VERSION = '2.1.0-DEV'; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $booted = false; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $debug; |
||
61 | |||
62 | /** |
||
63 | * Application environment: "dev|development" vs "prod|production". |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $environment; |
||
68 | |||
69 | /** |
||
70 | * @var \Psr\Log\LoggerInterface |
||
71 | */ |
||
72 | protected $logger; |
||
73 | |||
74 | /** |
||
75 | * Unix timestamp with microseconds. |
||
76 | * |
||
77 | * @var float |
||
78 | */ |
||
79 | protected $startTime; |
||
80 | |||
81 | /** |
||
82 | * Configuration loader. |
||
83 | * |
||
84 | * @var \PPI\Framework\Config\ConfigManager |
||
85 | */ |
||
86 | protected $configManager; |
||
87 | |||
88 | /** |
||
89 | * The Module Manager. |
||
90 | * |
||
91 | * @var \Zend\ModuleManager\ModuleManager |
||
92 | */ |
||
93 | protected $moduleManager; |
||
94 | |||
95 | /** |
||
96 | * @param int $errorReportingLevel The level of error reporting you want |
||
97 | */ |
||
98 | protected $errorReportingLevel; |
||
99 | |||
100 | /** |
||
101 | * @var null|array |
||
102 | */ |
||
103 | protected $matchedRoute; |
||
104 | |||
105 | /** |
||
106 | * @var \PPI\Framework\Module\Controller\ControllerResolver |
||
107 | */ |
||
108 | protected $resolver; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | protected $name; |
||
114 | |||
115 | /** |
||
116 | * Path to the application root dir aka the "app" directory. |
||
117 | * |
||
118 | * @var null|string |
||
119 | */ |
||
120 | protected $rootDir; |
||
121 | |||
122 | /** |
||
123 | * Service Manager. |
||
124 | * |
||
125 | * @var \PPI\Framework\ServiceManager\ServiceManager |
||
126 | */ |
||
127 | protected $serviceManager; |
||
128 | |||
129 | /** |
||
130 | * App constructor. |
||
131 | * |
||
132 | * @param array $options |
||
133 | */ |
||
134 | public function __construct(array $options = array()) |
||
149 | |||
150 | /** |
||
151 | * Set an App option. |
||
152 | * |
||
153 | * @param $option |
||
154 | * @param $value |
||
155 | * |
||
156 | * @throws \RuntimeException |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setOption($option, $value) |
||
176 | |||
177 | /** |
||
178 | * Get an App option. |
||
179 | * |
||
180 | * @param $option |
||
181 | * |
||
182 | * @throws \RuntimeException |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getOption($option) |
||
196 | |||
197 | public function __clone() |
||
206 | |||
207 | /** |
||
208 | * Run the boot process, load our modules and their dependencies. |
||
209 | * |
||
210 | * This method is automatically called by dispatch(), but you can use it |
||
211 | * to build all services when not handling a request. |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function boot() |
||
244 | |||
245 | /** |
||
246 | * Run the application and send the response. |
||
247 | * |
||
248 | * @param HttpRequest|null $request |
||
249 | * @param HttpRequest|null $request |
||
250 | * |
||
251 | * @throws \Exception |
||
252 | * |
||
253 | * @return Response |
||
254 | */ |
||
255 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
269 | |||
270 | /** |
||
271 | * Decide on a route to use and dispatch our module's controller action. |
||
272 | * |
||
273 | * @param HttpRequest $request |
||
274 | * @param HttpResponse $response |
||
275 | * |
||
276 | * @throws \Exception |
||
277 | * |
||
278 | * @return Response |
||
279 | */ |
||
280 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
317 | |||
318 | /** |
||
319 | * Gets the name of the application. |
||
320 | * |
||
321 | * @return string The application name |
||
322 | * |
||
323 | * @api |
||
324 | */ |
||
325 | public function getName() |
||
333 | |||
334 | /** |
||
335 | * Gets the version of the application. |
||
336 | * |
||
337 | * @return string The application version |
||
338 | * |
||
339 | * @api |
||
340 | */ |
||
341 | public function getVersion() |
||
345 | |||
346 | /** |
||
347 | * Get the environment mode the application is in. |
||
348 | * |
||
349 | * @return string The current environment |
||
350 | * |
||
351 | * @api |
||
352 | */ |
||
353 | public function getEnvironment() |
||
357 | |||
358 | /** |
||
359 | * @param $env |
||
360 | * |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function isEnvironment($env) |
||
373 | |||
374 | /** |
||
375 | * Checks if debug mode is enabled. |
||
376 | * |
||
377 | * @return bool true if debug mode is enabled, false otherwise |
||
378 | * |
||
379 | * @api |
||
380 | */ |
||
381 | public function isDebug() |
||
385 | |||
386 | /** |
||
387 | * Gets the application root dir. |
||
388 | * |
||
389 | * @return string The application root dir |
||
390 | * |
||
391 | * @api |
||
392 | */ |
||
393 | public function getRootDir() |
||
401 | |||
402 | /** |
||
403 | * Get the service manager. |
||
404 | * |
||
405 | * @return ServiceManager\ServiceManager |
||
406 | */ |
||
407 | public function getServiceManager() |
||
411 | |||
412 | /** |
||
413 | * @note Added for compatibility with Symfony's HttpKernel\Kernel. |
||
414 | * |
||
415 | * @return null|ServiceManager\ServiceManager |
||
416 | */ |
||
417 | public function getContainer() |
||
421 | |||
422 | /** |
||
423 | * Returns the Module Manager. |
||
424 | * |
||
425 | * @return \Zend\ModuleManager\ModuleManager |
||
426 | */ |
||
427 | public function getModuleManager() |
||
435 | |||
436 | /** |
||
437 | * Get an array of the loaded modules. |
||
438 | * |
||
439 | * @return array An array of Module objects, keyed by module name |
||
440 | */ |
||
441 | public function getModules() |
||
445 | |||
446 | /** |
||
447 | * @see PPI\Framework\Module\ModuleManager::locateResource() |
||
448 | * |
||
449 | * @param string $name A resource name to locate |
||
450 | * @param string $dir A directory where to look for the resource first |
||
451 | * @param bool $first Whether to return the first path or paths for all matching bundles |
||
452 | * |
||
453 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
454 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
455 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
456 | * |
||
457 | * @return string|array The absolute path of the resource or an array if $first is false |
||
458 | */ |
||
459 | public function locateResource($name, $dir = null, $first = true) |
||
463 | |||
464 | /** |
||
465 | * Gets the request start time (not available if debug is disabled). |
||
466 | * |
||
467 | * @return int The request start timestamp |
||
468 | * |
||
469 | * @api |
||
470 | */ |
||
471 | public function getStartTime() |
||
475 | |||
476 | /** |
||
477 | * Gets the cache directory. |
||
478 | * |
||
479 | * @return string The cache directory |
||
480 | * |
||
481 | * @api |
||
482 | */ |
||
483 | public function getCacheDir() |
||
487 | |||
488 | /** |
||
489 | * Gets the log directory. |
||
490 | * |
||
491 | * @return string The log directory |
||
492 | * |
||
493 | * @api |
||
494 | */ |
||
495 | public function getLogDir() |
||
499 | |||
500 | /** |
||
501 | * Gets the charset of the application. |
||
502 | * |
||
503 | * @return string The charset |
||
504 | * |
||
505 | * @api |
||
506 | */ |
||
507 | public function getCharset() |
||
511 | |||
512 | /** |
||
513 | * Returns a ConfigManager instance. |
||
514 | * |
||
515 | * @return \PPI\Framework\Config\ConfigManager |
||
516 | */ |
||
517 | public function getConfigManager() |
||
526 | |||
527 | /** |
||
528 | * Loads a configuration file or PHP array. |
||
529 | * |
||
530 | * @param $resource |
||
531 | * @param null $type |
||
532 | * |
||
533 | * @return App The current instance |
||
534 | */ |
||
535 | public function loadConfig($resource, $type = null) |
||
541 | |||
542 | /** |
||
543 | * Returns the application configuration. |
||
544 | * |
||
545 | * @throws \RuntimeException |
||
546 | * |
||
547 | * @return array|object |
||
548 | */ |
||
549 | public function getConfig() |
||
557 | |||
558 | public function serialize() |
||
562 | |||
563 | public function unserialize($data) |
||
569 | |||
570 | /** |
||
571 | * Returns the application parameters. |
||
572 | * |
||
573 | * @return array An array of application parameters |
||
574 | */ |
||
575 | protected function getAppParameters() |
||
590 | |||
591 | /** |
||
592 | * Gets the environment parameters. |
||
593 | * |
||
594 | * Only the parameters starting with "PPI__" are considered. |
||
595 | * |
||
596 | * @return array An array of parameters |
||
597 | */ |
||
598 | protected function getEnvParameters() |
||
609 | |||
610 | /** |
||
611 | * Creates and initializes a ServiceManager instance. |
||
612 | * |
||
613 | * @return ServiceManager The compiled service manager |
||
614 | */ |
||
615 | protected function buildServiceManager() |
||
624 | |||
625 | /** |
||
626 | * Perform the matching of a route and return a set of routing parameters if a valid one is found. |
||
627 | * Otherwise exceptions get thrown. |
||
628 | * |
||
629 | * @param HttpRequest $request |
||
630 | * |
||
631 | * @throws \Exception |
||
632 | * |
||
633 | * @return array |
||
634 | */ |
||
635 | protected function handleRouting(HttpRequest $request) |
||
659 | |||
660 | /** |
||
661 | * Logs with an arbitrary level. |
||
662 | * |
||
663 | * @param mixed $level |
||
664 | * @param string $message |
||
665 | * @param array $context |
||
666 | */ |
||
667 | protected function log($level, $message, array $context = array()) |
||
677 | |||
678 | /** |
||
679 | * Enables the debug tools. |
||
680 | * |
||
681 | * This method registers an error handler and an exception handler. |
||
682 | * |
||
683 | * If the Symfony ClassLoader component is available, a special |
||
684 | * class loader is also registered. |
||
685 | */ |
||
686 | protected function enableDebug() |
||
702 | |||
703 | protected function setupRouters() |
||
749 | |||
750 | |||
751 | } |
||
752 |
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.