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 XmlSerializationVisitor 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 XmlSerializationVisitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class XmlSerializationVisitor extends AbstractVisitor |
||
25 | { |
||
26 | /** |
||
27 | * @var AccessorInterface |
||
28 | */ |
||
29 | private $accessor; |
||
30 | |||
31 | /** |
||
32 | * @var \DOMDocument|null |
||
33 | */ |
||
34 | private $document; |
||
35 | |||
36 | /** |
||
37 | * @var \DOMElement|null |
||
38 | */ |
||
39 | private $node; |
||
40 | |||
41 | /** |
||
42 | * @var \DOMElement[] |
||
43 | */ |
||
44 | private $stack; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $version; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $encoding; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $formatOutput; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $root; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $entry; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $entryAttribute; |
||
75 | |||
76 | /** |
||
77 | * @param AccessorInterface $accessor |
||
78 | * @param string $version |
||
79 | * @param string $encoding |
||
80 | * @param bool $formatOutput |
||
81 | * @param string $root |
||
82 | * @param string $entry |
||
83 | * @param string $entryAttribute |
||
84 | */ |
||
85 | 1492 | public function __construct( |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 220 | public function prepare($data, ContextInterface $context) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 12 | public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context) |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 72 | public function visitData($data, TypeMetadataInterface $type, ContextInterface $context) |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 12 | View Code Duplication | public function visitFloat($data, TypeMetadataInterface $type, ContextInterface $context) |
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 148 | public function visitString($data, TypeMetadataInterface $type, ContextInterface $context) |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | 168 | public function startVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 220 | public function getResult() |
|
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | 168 | protected function doVisitObjectProperty( |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 56 | protected function doVisitArray($data, TypeMetadataInterface $type, ContextInterface $context) |
|
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | 80 | private function visitText($data) |
|
295 | |||
296 | /** |
||
297 | * @param \DOMNode $node |
||
298 | * |
||
299 | * @return \DOMNode |
||
300 | */ |
||
301 | 208 | private function visitNode(\DOMNode $node) |
|
309 | |||
310 | /** |
||
311 | * @param \DOMElement $node |
||
312 | */ |
||
313 | 184 | private function enterNodeScope(\DOMElement $node) |
|
318 | |||
319 | 184 | private function leaveNodeScope() |
|
323 | |||
324 | /** |
||
325 | * @param string|null $root |
||
326 | * |
||
327 | * @return \DOMDocument |
||
328 | */ |
||
329 | 220 | private function getDocument($root = null) |
|
333 | |||
334 | /** |
||
335 | * @param string $name |
||
336 | * @param string|null $entry |
||
337 | * @param string|null $entryAttribute |
||
338 | * |
||
339 | * @return \DOMElement |
||
340 | */ |
||
341 | 184 | private function createNode($name, $entry = null, $entryAttribute = null) |
|
357 | |||
358 | /** |
||
359 | * @param string|null $root |
||
360 | * |
||
361 | * @return \DOMDocument |
||
362 | */ |
||
363 | 220 | private function createDocument($root = null) |
|
373 | } |
||
374 |
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.