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 |
||
22 | class Configuration implements ConfigurationInterface |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function getConfigTreeBuilder() |
||
55 | |||
56 | /** |
||
57 | * Creates the configs node. |
||
58 | * |
||
59 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The configs node. |
||
60 | */ |
||
61 | private function createConfigsNode() |
||
72 | |||
73 | /** |
||
74 | * Creates the plugins node. |
||
75 | * |
||
76 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The plugins node. |
||
77 | */ |
||
78 | private function createPluginsNode() |
||
90 | |||
91 | /** |
||
92 | * Creates the styles node. |
||
93 | * |
||
94 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The styles node. |
||
95 | */ |
||
96 | View Code Duplication | private function createStylesNode() |
|
122 | |||
123 | /** |
||
124 | * Creates the templates node. |
||
125 | * |
||
126 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The templates node. |
||
127 | */ |
||
128 | View Code Duplication | private function createTemplatesNode() |
|
129 | { |
||
130 | return $this->createNode('templates') |
||
131 | ->normalizeKeys(false) |
||
132 | ->useAttributeAsKey('name') |
||
133 | ->prototype('array') |
||
134 | ->children() |
||
135 | ->scalarNode('imagesPath')->end() |
||
136 | ->arrayNode('templates') |
||
137 | ->prototype('array') |
||
138 | ->children() |
||
139 | ->scalarNode('title')->end() |
||
140 | ->scalarNode('image')->end() |
||
141 | ->scalarNode('description')->end() |
||
142 | ->scalarNode('html')->end() |
||
143 | ->scalarNode('template')->end() |
||
144 | ->arrayNode('template_parameters') |
||
145 | ->normalizeKeys(false) |
||
146 | ->useAttributeAsKey('name') |
||
147 | ->prototype('scalar')->end() |
||
148 | ->end() |
||
149 | ->end() |
||
150 | ->end() |
||
151 | ->end() |
||
152 | ->end() |
||
153 | ->end(); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Creates the filebrowsers node. |
||
158 | * |
||
159 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The filebrowsers node. |
||
160 | */ |
||
161 | private function createFilebrowsersNode() |
||
168 | |||
169 | /** |
||
170 | * Creates the toolbars node. |
||
171 | * |
||
172 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The toolbars node. |
||
173 | */ |
||
174 | private function createToolbarsNode() |
||
193 | |||
194 | /** |
||
195 | * Creates a node. |
||
196 | * |
||
197 | * @param string $name The node name. |
||
198 | * |
||
199 | * @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The node. |
||
200 | */ |
||
201 | private function createNode($name) |
||
205 | |||
206 | /** |
||
207 | * Creates a tree builder. |
||
208 | * |
||
209 | * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder. |
||
210 | */ |
||
211 | private function createTreeBuilder() |
||
215 | } |
||
216 |
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.