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 IvoryCKEditorExtension 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 IvoryCKEditorExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class IvoryCKEditorExtension extends ConfigurableExtension |
||
27 | { |
||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | protected function loadInternal(array $config, ContainerBuilder $container) |
||
53 | |||
54 | /** |
||
55 | * Registers the CKEditor config. |
||
56 | * |
||
57 | * @param array $config The CKEditor configuration |
||
58 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container The container. |
||
59 | */ |
||
60 | private function registerConfig(array $config, ContainerBuilder $container) |
||
100 | |||
101 | /** |
||
102 | * Registers the CKEditor configs. |
||
103 | * |
||
104 | * @param array $config The CKEditor configuration. |
||
105 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container The container. |
||
106 | * |
||
107 | * @throws \Ivory\CKEditorBundle\Exception\DependencyInjectionException If the default config does not exist. |
||
108 | */ |
||
109 | private function registerConfigs(array $config, ContainerBuilder $container) |
||
130 | |||
131 | /** |
||
132 | * Registers the CKEditor plugins. |
||
133 | * |
||
134 | * @param array $config The CKEditor configuration. |
||
135 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container The container. |
||
136 | */ |
||
137 | private function registerPlugins(array $config, ContainerBuilder $container) |
||
149 | |||
150 | /** |
||
151 | * Registers the CKEditor styles set. |
||
152 | * |
||
153 | * @param array $config The CKEditor configuration. |
||
154 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container The container. |
||
155 | */ |
||
156 | View Code Duplication | private function registerStylesSet(array $config, ContainerBuilder $container) |
|
168 | |||
169 | /** |
||
170 | * Registers the CKEditor templates. |
||
171 | * |
||
172 | * @param array $config The CKEditor configuration. |
||
173 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container The container. |
||
174 | */ |
||
175 | View Code Duplication | private function registerTemplates(array $config, ContainerBuilder $container) |
|
187 | |||
188 | /** |
||
189 | * Merges the toolbars into the CKEditor configs. |
||
190 | * |
||
191 | * @param array $config The CKEditor configuration. |
||
192 | * |
||
193 | * @throws \Ivory\CKEditorBundle\Exception\DependencyInjectionException If a toolbar does not exist. |
||
194 | * |
||
195 | * @return array The CKEditor configuration with merged toolbars. |
||
196 | */ |
||
197 | private function mergeToolbars(array $config) |
||
216 | |||
217 | /** |
||
218 | * Resolves the CKEditor toolbars. |
||
219 | * |
||
220 | * @param array $config The CKEditor configuration. |
||
221 | * |
||
222 | * @return array The resolved CKEditor toolbars. |
||
223 | */ |
||
224 | private function resolveToolbars(array $config) |
||
238 | |||
239 | /** |
||
240 | * Resolves a CKEditor toolbar item. |
||
241 | * |
||
242 | * @param string|array $item The CKEditor item. |
||
243 | * @param array $items The CKEditor items. |
||
244 | * |
||
245 | * @throws \Ivory\CKEditorBundle\Exception\DependencyInjectionException If the toolbar item does not exist. |
||
246 | * |
||
247 | * @return array|string The resolved CKEditor toolbar item. |
||
248 | */ |
||
249 | private function resolveToolbarItem($item, array $items) |
||
263 | |||
264 | /** |
||
265 | * Fixes the CKEditor styles set. |
||
266 | * |
||
267 | * @param array $stylesSet The CKEditor styles set. |
||
268 | * |
||
269 | * @return array The fixed CKEditor styles set. |
||
270 | */ |
||
271 | private function fixStylesSet(array $stylesSet) |
||
279 | |||
280 | /** |
||
281 | * Gets the default CKEditor toolbars. |
||
282 | * |
||
283 | * @return array The default CKEditor toolbars. |
||
284 | */ |
||
285 | private function getDefaultToolbars() |
||
293 | |||
294 | /** |
||
295 | * Gets the full CKEditor toolbar. |
||
296 | * |
||
297 | * @return array The full CKEditor toolbar. |
||
298 | */ |
||
299 | private function getFullToolbar() |
||
323 | |||
324 | /** |
||
325 | * Gets the standard CKEditor toolbar. |
||
326 | * |
||
327 | * @return array The standard CKEditor toolbar. |
||
328 | */ |
||
329 | private function getStandardToolbar() |
||
344 | |||
345 | /** |
||
346 | * Gets the basic CKEditor toolbar. |
||
347 | * |
||
348 | * @return array The basic CKEditor toolbar. |
||
349 | */ |
||
350 | private function getBasicToolbar() |
||
359 | } |
||
360 |
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.