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 GlHtmlNode 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 GlHtmlNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class GlHtmlNode |
||
28 | { |
||
29 | /** |
||
30 | * @var \DOMNode |
||
31 | */ |
||
32 | private $node; |
||
33 | |||
34 | /** |
||
35 | * @param \DOMNode $node |
||
36 | */ |
||
37 | public function __construct(\DOMNode $node) |
||
41 | |||
42 | /** |
||
43 | * @param array $attributes |
||
44 | */ |
||
45 | public function setAttributes(array $attributes) |
||
46 | { |
||
47 | foreach ($attributes as $name => $value) { |
||
48 | $this->node->setAttribute($name, $value); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $value |
||
54 | */ |
||
55 | public function setValue($value) |
||
59 | |||
60 | /** |
||
61 | * @param string $html |
||
62 | */ |
||
63 | public |
||
71 | |||
72 | /** |
||
73 | * @return GlHtmlNode |
||
74 | */ |
||
75 | public function getParent() |
||
79 | |||
80 | /** |
||
81 | * @param string $html |
||
82 | */ |
||
83 | public |
||
91 | |||
92 | /** |
||
93 | * @param string $html |
||
94 | */ |
||
95 | public function replaceInner($html) |
||
100 | |||
101 | |||
102 | /** |
||
103 | * @return \DOMNode |
||
104 | */ |
||
105 | public function getDOMNode() |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getName() |
||
117 | |||
118 | /** |
||
119 | * @param array $hasAttributesList |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function hasAttributes(array $hasAttributesList) |
||
135 | |||
136 | /** |
||
137 | * @param string $name |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public |
||
156 | |||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | public function getSentences() |
||
174 | |||
175 | public |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | public |
||
195 | |||
196 | /** |
||
197 | * extract h tag from html |
||
198 | * |
||
199 | * @param \DOMNodeList $nodeList |
||
200 | * @param callable $fct |
||
201 | */ |
||
202 | private function recursiveCallChild(\DOMNodeList $nodeList, callable $fct) |
||
214 | |||
215 | /** |
||
216 | * @param callable $fct |
||
217 | */ |
||
218 | public function callChild(callable $fct) |
||
222 | |||
223 | /** |
||
224 | * @param \DOMNode $node |
||
225 | * @param string $sentence |
||
226 | * @param array $sentences |
||
227 | * |
||
228 | * @return int |
||
229 | */ |
||
230 | private static function iterateSentencesOverNode(\DOMNode $node, &$sentence, array &$sentences) |
||
310 | } |
||
311 |
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.