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 Kernel 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 Kernel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | abstract class Kernel implements KernelInterface, TerminableInterface |
||
45 | { |
||
46 | /** |
||
47 | * @var BundleInterface[] |
||
48 | */ |
||
49 | protected $bundles = array(); |
||
50 | |||
51 | protected $bundleMap; |
||
52 | protected $container; |
||
53 | protected $rootDir; |
||
54 | protected $environment; |
||
55 | protected $debug; |
||
56 | protected $booted = false; |
||
57 | protected $name; |
||
58 | protected $startTime; |
||
59 | protected $loadClassCache; |
||
60 | |||
61 | const VERSION = '2.7.30-DEV'; |
||
62 | const VERSION_ID = 20730; |
||
63 | const MAJOR_VERSION = 2; |
||
64 | const MINOR_VERSION = 7; |
||
65 | const RELEASE_VERSION = 30; |
||
66 | const EXTRA_VERSION = 'DEV'; |
||
67 | |||
68 | const END_OF_MAINTENANCE = '05/2018'; |
||
69 | const END_OF_LIFE = '05/2019'; |
||
70 | |||
71 | /** |
||
72 | * Constructor. |
||
73 | * |
||
74 | * @param string $environment The environment |
||
75 | * @param bool $debug Whether to enable debugging or not |
||
76 | */ |
||
77 | public function __construct($environment, $debug) |
||
96 | |||
97 | /** |
||
98 | * @deprecated since version 2.3, to be removed in 3.0. Move your logic in the constructor instead. |
||
99 | */ |
||
100 | public function init() |
||
104 | |||
105 | public function __clone() |
||
114 | |||
115 | /** |
||
116 | * Boots the current kernel. |
||
117 | */ |
||
118 | public function boot() |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function terminate(Request $request, Response $response) |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function shutdown() |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
||
186 | |||
187 | /** |
||
188 | * Gets a HTTP kernel from the container. |
||
189 | * |
||
190 | * @return HttpKernel |
||
191 | */ |
||
192 | protected function getHttpKernel() |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function getBundles() |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | * |
||
208 | * @deprecated since version 2.6, to be removed in 3.0. |
||
209 | */ |
||
210 | public function isClassInActiveBundle($class) |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function getBundle($name, $first = true) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | * |
||
242 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle |
||
243 | */ |
||
244 | public function locateResource($name, $dir = null, $first = true) |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function getName() |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function getEnvironment() |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function isDebug() |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | View Code Duplication | public function getRootDir() |
|
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | */ |
||
345 | public function getContainer() |
||
349 | |||
350 | /** |
||
351 | * Loads the PHP class cache. |
||
352 | * |
||
353 | * This methods only registers the fact that you want to load the cache classes. |
||
354 | * The cache will actually only be loaded when the Kernel is booted. |
||
355 | * |
||
356 | * That optimization is mainly useful when using the HttpCache class in which |
||
357 | * case the class cache is not loaded if the Response is in the cache. |
||
358 | * |
||
359 | * @param string $name The cache name prefix |
||
360 | * @param string $extension File extension of the resulting file |
||
361 | */ |
||
362 | public function loadClassCache($name = 'classes', $extension = '.php') |
||
366 | |||
367 | /** |
||
368 | * Used internally. |
||
369 | */ |
||
370 | public function setClassCache(array $classes) |
||
374 | |||
375 | /** |
||
376 | * {@inheritdoc} |
||
377 | */ |
||
378 | public function getStartTime() |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | */ |
||
386 | public function getCacheDir() |
||
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | */ |
||
394 | public function getLogDir() |
||
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | public function getCharset() |
||
406 | |||
407 | protected function doLoadClassCache($name, $extension) |
||
413 | |||
414 | /** |
||
415 | * Initializes the data structures related to the bundle management. |
||
416 | * |
||
417 | * - the bundles property maps a bundle name to the bundle instance, |
||
418 | * - the bundleMap property maps a bundle name to the bundle inheritance hierarchy (most derived bundle first). |
||
419 | * |
||
420 | * @throws \LogicException if two bundles share a common name |
||
421 | * @throws \LogicException if a bundle tries to extend a non-registered bundle |
||
422 | * @throws \LogicException if a bundle tries to extend itself |
||
423 | * @throws \LogicException if two bundles extend the same ancestor |
||
424 | */ |
||
425 | protected function initializeBundles() |
||
477 | |||
478 | /** |
||
479 | * Gets the container class. |
||
480 | * |
||
481 | * @return string The container class |
||
482 | */ |
||
483 | protected function getContainerClass() |
||
487 | |||
488 | /** |
||
489 | * Gets the container's base class. |
||
490 | * |
||
491 | * All names except Container must be fully qualified. |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | protected function getContainerBaseClass() |
||
499 | |||
500 | /** |
||
501 | * Initializes the service container. |
||
502 | * |
||
503 | * The cached version of the service container is used when fresh, otherwise the |
||
504 | * container is built. |
||
505 | */ |
||
506 | protected function initializeContainer() |
||
528 | |||
529 | /** |
||
530 | * Returns the kernel parameters. |
||
531 | * |
||
532 | * @return array An array of kernel parameters |
||
533 | */ |
||
534 | protected function getKernelParameters() |
||
564 | |||
565 | /** |
||
566 | * Gets the environment parameters. |
||
567 | * |
||
568 | * Only the parameters starting with "SYMFONY__" are considered. |
||
569 | * |
||
570 | * @return array An array of parameters |
||
571 | */ |
||
572 | protected function getEnvParameters() |
||
583 | |||
584 | /** |
||
585 | * Builds the service container. |
||
586 | * |
||
587 | * @return ContainerBuilder The compiled service container |
||
588 | * |
||
589 | * @throws \RuntimeException |
||
590 | */ |
||
591 | protected function buildContainer() |
||
616 | |||
617 | /** |
||
618 | * Prepares the ContainerBuilder before it is compiled. |
||
619 | * |
||
620 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
621 | */ |
||
622 | protected function prepareContainer(ContainerBuilder $container) |
||
642 | |||
643 | /** |
||
644 | * Gets a new ContainerBuilder instance used to build the service container. |
||
645 | * |
||
646 | * @return ContainerBuilder |
||
647 | */ |
||
648 | protected function getContainerBuilder() |
||
658 | |||
659 | /** |
||
660 | * Dumps the service container to PHP code in the cache. |
||
661 | * |
||
662 | * @param ConfigCache $cache The config cache |
||
663 | * @param ContainerBuilder $container The service container |
||
664 | * @param string $class The name of the class to generate |
||
665 | * @param string $baseClass The name of the container's base class |
||
666 | */ |
||
667 | protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass) |
||
680 | |||
681 | /** |
||
682 | * Returns a loader for the container. |
||
683 | * |
||
684 | * @param ContainerInterface $container The service container |
||
685 | * |
||
686 | * @return DelegatingLoader The loader |
||
687 | */ |
||
688 | protected function getContainerLoader(ContainerInterface $container) |
||
701 | |||
702 | /** |
||
703 | * Removes comments from a PHP source string. |
||
704 | * |
||
705 | * We don't use the PHP php_strip_whitespace() function |
||
706 | * as we want the content to be readable and well-formatted. |
||
707 | * |
||
708 | * @param string $source A PHP string |
||
709 | * |
||
710 | * @return string The PHP string with the comments removed |
||
711 | */ |
||
712 | public static function stripComments($source) |
||
764 | |||
765 | public function serialize() |
||
769 | |||
770 | public function unserialize($data) |
||
776 | } |
||
777 |
If you suppress an error, we recommend checking for the error condition explicitly: