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:
| 1 | <?php |
||
| 34 | class Symfony2Extension implements ExtensionInterface |
||
| 35 | { |
||
| 36 | const KERNEL_ID = 'symfony2_extension.kernel'; |
||
| 37 | const DEFAULT_KERNEL_BOOTSTRAP = 'app/autoload.php'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function getConfigKey() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | public function initialize(ExtensionManager $extensionManager) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function configure(ArrayNodeDefinition $builder) |
||
| 61 | { |
||
| 62 | $boolFilter = function ($v) { |
||
| 63 | $filtered = filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
||
| 64 | |||
| 65 | return (null === $filtered) ? $v : $filtered; |
||
| 66 | }; |
||
| 67 | |||
| 68 | $builder |
||
| 69 | ->addDefaultsIfNotSet() |
||
| 70 | ->children() |
||
| 71 | ->arrayNode('kernel') |
||
| 72 | ->addDefaultsIfNotSet() |
||
| 73 | ->children() |
||
| 74 | ->scalarNode('bootstrap')->defaultValue(class_exists('App\Kernel') ? null : self::DEFAULT_KERNEL_BOOTSTRAP)->end() |
||
| 75 | ->scalarNode('path')->defaultValue(class_exists('App\Kernel') ? null : 'app/AppKernel.php')->end() |
||
| 76 | ->scalarNode('class')->defaultValue(class_exists('App\Kernel') ? 'App\Kernel' : 'AppKernel')->end() |
||
| 77 | ->scalarNode('env')->defaultValue('test')->end() |
||
| 78 | ->booleanNode('debug') |
||
| 79 | ->beforeNormalization() |
||
| 80 | ->ifString()->then($boolFilter) |
||
| 81 | ->end() |
||
| 82 | ->defaultTrue() |
||
| 83 | ->end() |
||
| 84 | ->end() |
||
| 85 | ->end() |
||
| 86 | ->arrayNode('context') |
||
| 87 | ->addDefaultsIfNotSet() |
||
| 88 | ->children() |
||
| 89 | ->scalarNode('path_suffix') |
||
| 90 | ->defaultValue('Features') |
||
| 91 | ->end() |
||
| 92 | ->scalarNode('class_suffix') |
||
| 93 | ->defaultValue('Features\Context\FeatureContext') |
||
| 94 | ->end() |
||
| 95 | ->end() |
||
| 96 | ->end() |
||
| 97 | ->end() |
||
| 98 | ->end(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function load(ContainerBuilder $container, array $config) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function process(ContainerBuilder $container) |
||
| 144 | |||
| 145 | private function loadClassGenerator(ContainerBuilder $container) |
||
| 151 | |||
| 152 | View Code Duplication | private function loadContextInitializer(ContainerBuilder $container) |
|
| 161 | |||
| 162 | View Code Duplication | private function loadFeatureLocator(ContainerBuilder $container) |
|
| 173 | |||
| 174 | private function loadKernel(ContainerBuilder $container, array $config) |
||
| 185 | |||
| 186 | private function loadSuiteGenerator(ContainerBuilder $container, array $config) |
||
| 196 | |||
| 197 | private function loadServiceArgumentResolver(ContainerBuilder $container) |
||
| 205 | } |
||
| 206 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.