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 |
||
13 | class Extension implements ExtensionInterface |
||
14 | { |
||
15 | public function initialize(ExtensionManager $extensionManager) |
||
18 | |||
19 | public function load(ContainerBuilder $container, array $config) |
||
20 | { |
||
21 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/services')); |
||
22 | $loader->load('core.yml'); |
||
23 | $loader->load('fakers.yml'); |
||
24 | $loader->load('guessers.yml'); |
||
25 | $loader->load('builder.yml'); |
||
26 | |||
27 | $container->setParameter('friendly.parameters', $config); |
||
28 | |||
29 | $container->addCompilerPass(new Compiler\ConfigPass); |
||
30 | $container->addCompilerPass(new Compiler\FormatGuesserPass); |
||
31 | $container->addCompilerPass(new Compiler\FakerProviderPass); |
||
32 | $container->addCompilerPass(new Compiler\ApiUrlPass); |
||
33 | $container->addCompilerPass(new Compiler\KernelPass); |
||
34 | } |
||
35 | |||
36 | public function configure(ArrayNodeDefinition $builder) |
||
37 | { |
||
38 | $builder |
||
39 | ->children() |
||
40 | ->arrayNode('symfony_kernel') |
||
41 | ->addDefaultsIfNotSet() |
||
42 | ->children() |
||
43 | ->scalarNode('bootstrap') |
||
44 | ->defaultValue('app/autoload.php') |
||
45 | ->end() |
||
46 | ->scalarNode('path') |
||
47 | ->defaultValue('app/AppKernel.php') |
||
48 | ->end() |
||
49 | ->scalarNode('class') |
||
50 | ->defaultValue('AppKernel') |
||
51 | ->end() |
||
52 | ->scalarNode('env') |
||
53 | ->defaultValue('test') |
||
54 | ->end() |
||
55 | ->booleanNode('debug') |
||
56 | ->defaultTrue() |
||
57 | ->end() |
||
58 | ->end() |
||
59 | ->end() |
||
60 | ->arrayNode('alice') |
||
61 | ->addDefaultsIfNotSet() |
||
62 | ->children() |
||
63 | ->scalarNode('locale') |
||
64 | ->defaultValue('en_US') |
||
65 | ->end() |
||
66 | ->arrayNode('fixtures') |
||
67 | ->prototype('scalar')->end() |
||
68 | ->end() |
||
69 | ->arrayNode('dependencies') |
||
70 | ->useAttributeAsKey('name') |
||
71 | ->prototype('array') |
||
72 | ->prototype('scalar')->end() |
||
73 | ->end() |
||
74 | ->end() |
||
75 | ->arrayNode('providers') |
||
76 | ->prototype('scalar')->end() |
||
77 | ->end() |
||
78 | ->end() |
||
79 | ->end() |
||
80 | ->arrayNode('entities') |
||
81 | ->addDefaultsIfNotSet() |
||
82 | ->children() |
||
83 | ->arrayNode('namespaces') |
||
84 | ->prototype('scalar')->end() |
||
85 | ->end() |
||
86 | ->end() |
||
87 | ->end() |
||
88 | ->arrayNode('page') |
||
89 | ->addDefaultsIfNotSet() |
||
90 | ->children() |
||
91 | ->scalarNode('namespace') |
||
92 | ->defaultValue('Page') |
||
93 | ->end() |
||
94 | ->end() |
||
95 | ->end() |
||
96 | ->arrayNode('api') |
||
97 | ->addDefaultsIfNotSet() |
||
98 | ->children() |
||
99 | ->scalarNode('base_url') |
||
100 | ->defaultValue('') |
||
101 | ->end() |
||
102 | ->end() |
||
103 | ->end() |
||
104 | ->scalarNode('smartTag') |
||
105 | ->defaultValue('smartStep') |
||
106 | ->end() |
||
107 | ->end() |
||
108 | ; |
||
109 | } |
||
110 | |||
111 | public function process(ContainerBuilder $container) |
||
114 | |||
115 | public function getConfigKey() |
||
116 | { |
||
117 | return 'friendly'; |
||
118 | } |
||
119 | |||
120 | View Code Duplication | protected function buildParameters($name, &$parameters, $config) |
|
|
|||
121 | { |
||
122 | foreach ($config as $key => $element) { |
||
123 | if (is_array($element) && $this->arrayHasStringKeys($element)) { |
||
124 | $this->buildParameters(sprintf('%s.%s', $name, $key), $parameters, $element); |
||
125 | } |
||
126 | $parameters[sprintf('%s.%s', $name, $key)] = $element; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | protected function arrayHasStringKeys(array $array) |
||
141 | } |
||
142 |
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.