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 |
||
33 | class App implements AppInterface |
||
34 | { |
||
35 | /** |
||
36 | * Version string. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const VERSION = '2.1.0-DEV'; |
||
41 | |||
42 | /** |
||
43 | * @var boolean |
||
44 | */ |
||
45 | protected $booted = false; |
||
46 | |||
47 | /** |
||
48 | * @var boolean |
||
49 | */ |
||
50 | protected $debug; |
||
51 | |||
52 | /** |
||
53 | * Application environment: "dev|development" vs "prod|production". |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $environment; |
||
58 | |||
59 | /** |
||
60 | * @var \Psr\Log\LoggerInterface |
||
61 | */ |
||
62 | protected $logger; |
||
63 | |||
64 | /** |
||
65 | * Unix timestamp with microseconds. |
||
66 | * |
||
67 | * @var float |
||
68 | */ |
||
69 | protected $startTime; |
||
70 | |||
71 | /** |
||
72 | * Configuration loader. |
||
73 | * |
||
74 | * @var \PPI\Framework\Config\ConfigManager |
||
75 | */ |
||
76 | protected $configManager; |
||
77 | |||
78 | /** |
||
79 | * The Module Manager. |
||
80 | * |
||
81 | * @var \Zend\ModuleManager\ModuleManager |
||
82 | */ |
||
83 | protected $moduleManager; |
||
84 | |||
85 | /** |
||
86 | * @param integer $errorReportingLevel The level of error reporting you want |
||
87 | */ |
||
88 | protected $errorReportingLevel; |
||
89 | |||
90 | /** |
||
91 | * @var null|array |
||
92 | */ |
||
93 | protected $matchedRoute; |
||
94 | |||
95 | /** |
||
96 | * @var \PPI\Framework\Module\Controller\ControllerResolver |
||
97 | */ |
||
98 | protected $resolver; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $name; |
||
104 | |||
105 | /** |
||
106 | * Path to the application root dir aka the "app" directory. |
||
107 | * |
||
108 | * @var null|string |
||
109 | */ |
||
110 | protected $rootDir; |
||
111 | |||
112 | /** |
||
113 | * Service Manager. |
||
114 | * |
||
115 | * @var \PPI\Framework\ServiceManager\ServiceManager |
||
116 | */ |
||
117 | protected $serviceManager; |
||
118 | |||
119 | /** |
||
120 | * App constructor. |
||
121 | * |
||
122 | * @param array $options |
||
123 | */ |
||
124 | public function __construct(array $options = array()) |
||
125 | { |
||
126 | // Default options |
||
127 | $this->environment = isset($options['environment']) && $options['environment'] ? (string)$options['environment'] : 'prod'; |
||
128 | $this->debug = isset($options['debug']) && null !== $options['debug'] ? (boolean)$options['debug'] : false; |
||
129 | $this->rootDir = isset($options['rootDir']) && $options['rootDir'] ? (string)$options['rootDir'] : $this->getRootDir(); |
||
130 | $this->name = isset($options['name']) && $options['name'] ? (string)$options['name'] : $this->getName(); |
||
131 | |||
132 | if ($this->debug) { |
||
133 | $this->startTime = microtime(true); |
||
134 | $this->enableDebug(); |
||
135 | } else { |
||
136 | ini_set('display_errors', 0); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Set an App option. |
||
142 | * |
||
143 | * @param $option |
||
144 | * @param $value |
||
145 | * |
||
146 | * @throws \RuntimeException |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function setOption($option, $value) |
||
151 | { |
||
152 | if (true === $this->booted) { |
||
153 | throw new \RuntimeException('Setting App options after boot() is now allowed'); |
||
154 | } |
||
155 | |||
156 | // "root_dir" to "rootDir" |
||
157 | $property = preg_replace('/_(.?)/e', "strtoupper('$1')", $option); |
||
158 | if (!property_exists($this, $property)) { |
||
159 | throw new \RuntimeException(sprintf('App property "%s" (option "%s") does not exist', $property, $option)); |
||
160 | } |
||
161 | |||
162 | $this->$property = $value; |
||
163 | |||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get an App option. |
||
169 | * |
||
170 | * @param $option |
||
171 | * |
||
172 | * @throws \RuntimeException |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getOption($option) |
||
177 | { |
||
178 | // "root_dir" to "rootDir" |
||
179 | $property = preg_replace('/_(.?)/e', "strtoupper('$1')", $option); |
||
180 | if (!property_exists($this, $property)) { |
||
181 | throw new \RuntimeException(sprintf('App property "%s" (option "%s") does not exist', $property, $option)); |
||
182 | } |
||
183 | |||
184 | return $property; |
||
185 | } |
||
186 | |||
187 | public function __clone() |
||
188 | { |
||
189 | if ($this->debug) { |
||
190 | $this->startTime = microtime(true); |
||
191 | } |
||
192 | |||
193 | $this->booted = false; |
||
194 | $this->serviceManager = null; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Run the boot process, load our modules and their dependencies. |
||
199 | * |
||
200 | * This method is automatically called by dispatch(), but you can use it |
||
201 | * to build all services when not handling a request. |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function boot() |
||
206 | { |
||
207 | if (true === $this->booted) { |
||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | $this->serviceManager = $this->buildServiceManager(); |
||
212 | $this->log('debug', sprintf('Booting %s ...', $this->name)); |
||
213 | |||
214 | // Loading our Modules |
||
215 | $this->getModuleManager()->loadModules(); |
||
216 | if ($this->debug) { |
||
217 | $modules = $this->getModuleManager()->getModules(); |
||
218 | $this->log('debug', sprintf('All modules online (%d): "%s"', count($modules), implode('", "', $modules))); |
||
219 | } |
||
220 | |||
221 | // Lets get all the services our of our modules and start setting them in the ServiceManager |
||
222 | $moduleServices = $this->serviceManager->get('ModuleDefaultListener')->getServices(); |
||
223 | foreach ($moduleServices as $key => $service) { |
||
224 | $this->serviceManager->setFactory($key, $service); |
||
225 | } |
||
226 | |||
227 | $this->booted = true; |
||
228 | if ($this->debug) { |
||
229 | $this->log('debug', sprintf('%s has booted (in %.3f secs)', $this->name, microtime(true) - $this->startTime)); |
||
230 | } |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Run the application and send the response. |
||
237 | * |
||
238 | * @param HttpRequest|null $request |
||
239 | * @param HttpRequest|null $response |
||
240 | * @return Response |
||
241 | * @throws \Exception |
||
242 | */ |
||
243 | public function run(HttpRequest $request = null, HttpResponse $response = null) |
||
244 | { |
||
245 | if (false === $this->booted) { |
||
246 | $this->boot(); |
||
247 | } |
||
248 | |||
249 | $request = $request ?: HttpRequest::createFromGlobals(); |
||
250 | $response = $response ?: new HttpResponse(); |
||
251 | |||
252 | $response = $this->dispatch($request, $response); |
||
|
|||
253 | $response->send(); |
||
254 | |||
255 | return $response; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * |
||
260 | * Decide on a route to use and dispatch our module's controller action. |
||
261 | * |
||
262 | * @param HttpRequest $request |
||
263 | * @param HttpResponse $response |
||
264 | * @return Response |
||
265 | * @throws \Exception |
||
266 | */ |
||
267 | public function dispatch(HttpRequest $request, HttpResponse $response) |
||
268 | { |
||
269 | if (false === $this->booted) { |
||
270 | $this->boot(); |
||
271 | } |
||
272 | |||
273 | // Routing |
||
274 | $routeParams = $this->handleRouting($request); |
||
275 | $request->attributes->add($routeParams); |
||
276 | |||
277 | // Resolve our Controller |
||
278 | $resolver = $this->serviceManager->get('ControllerResolver'); |
||
279 | if (false === $controller = $resolver->getController($request)) { |
||
280 | throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s".', $request->getPathInfo())); |
||
281 | } |
||
282 | |||
283 | $controllerArguments = $resolver->getArguments($request, $controller); |
||
284 | |||
285 | $result = call_user_func_array( |
||
286 | $controller, |
||
287 | $controllerArguments |
||
288 | ); |
||
289 | |||
290 | if($result === null) { |
||
291 | throw new \Exception('Your action returned null. It must always return something'); |
||
292 | } else if(is_string($result)) { |
||
293 | $response->setContent($result); |
||
294 | } else if ($result instanceof SymfonyResponse || $response instanceof HttpResponse) { |
||
295 | $response = $result; |
||
296 | } else { |
||
297 | throw new \Exception('Invalid response type returned from controller'); |
||
298 | } |
||
299 | |||
300 | $this->response = $response; |
||
301 | |||
302 | return $response; |
||
303 | } |
||
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 HttpRequest $request |
||
618 | * @return array |
||
619 | * |
||
620 | * @throws \Exception |
||
621 | */ |
||
622 | protected function handleRouting(HttpRequest $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 |
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.