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 PhpArrayDumper 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 PhpArrayDumper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class PhpArrayDumper extends Dumper |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public function dump(array $options = array()) |
||
29 | |||
30 | /** |
||
31 | * Returns the service container as a PHP array. |
||
32 | * |
||
33 | * @return array |
||
34 | * A PHP array represention of the service container |
||
35 | */ |
||
36 | public function getArray() |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Returns parameters of the container as a PHP Array. |
||
47 | * |
||
48 | * @return array |
||
49 | * The escaped and prepared parameters of the container. |
||
50 | */ |
||
51 | View Code Duplication | protected function getParameters() |
|
61 | |||
62 | /** |
||
63 | * Returns services of the container as a PHP Array. |
||
64 | * |
||
65 | * @return array |
||
66 | * The service definitions. |
||
67 | */ |
||
68 | protected function getServiceDefinitions() |
||
89 | |||
90 | /** |
||
91 | * Prepares parameters. |
||
92 | * |
||
93 | * @param array $parameters |
||
94 | * @param bool $escape |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | View Code Duplication | protected function prepareParameters($parameters, $escape = true) |
|
114 | |||
115 | /** |
||
116 | * Escapes arguments. |
||
117 | * |
||
118 | * @param array $arguments |
||
119 | * The arguments to escape. |
||
120 | * |
||
121 | * @return array |
||
122 | * The escaped arguments. |
||
123 | */ |
||
124 | View Code Duplication | protected function escape($arguments) |
|
141 | |||
142 | /** |
||
143 | * Gets a service definition as PHP array. |
||
144 | * |
||
145 | * @param \Symfony\Component\DependencyInjection\Definition $definition |
||
146 | * The definition to process. |
||
147 | * |
||
148 | * @return array |
||
149 | * The service definition as PHP array. |
||
150 | */ |
||
151 | protected function getServiceDefinition($definition) |
||
215 | |||
216 | /** |
||
217 | * Returns a service alias definiton. |
||
218 | * |
||
219 | * @param string $alias |
||
220 | * @param Alias $id |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | protected function getServiceAliasDefinition($id) |
||
237 | /** |
||
238 | * Dumps callable to YAML format |
||
239 | * |
||
240 | * @param callable $callable |
||
241 | * |
||
242 | * @return callable |
||
243 | */ |
||
244 | protected function dumpCallable($callable) |
||
261 | |||
262 | /** |
||
263 | * Returns a private service definition in a suitable format. |
||
264 | * |
||
265 | * @param \Symfony\Component\DependencyInjection\Definition $definition |
||
266 | * The definition to process. |
||
267 | * |
||
268 | * @return \stdClass |
||
269 | * A very lightweight private service value object. |
||
270 | */ |
||
271 | protected function getPrivateService(Definition $definition) { |
||
280 | |||
281 | /** |
||
282 | * Dumps the value to YAML format. |
||
283 | * |
||
284 | * @param mixed $value |
||
285 | * |
||
286 | * @return mixed |
||
287 | * |
||
288 | * @throws RuntimeException When trying to dump object or resource |
||
289 | */ |
||
290 | protected function dumpValue($value) |
||
318 | |||
319 | /** |
||
320 | * Gets the service call. |
||
321 | * |
||
322 | * @param string $id |
||
323 | * @param Reference $reference |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | View Code Duplication | protected function getServiceCall($id, Reference $reference = null) |
|
335 | |||
336 | /** |
||
337 | * Gets parameter call. |
||
338 | * |
||
339 | * @param string $id |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | protected function getParameterCall($id) |
||
347 | |||
348 | protected function getExpressionCall($expression) |
||
352 | } |
||
353 |
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.