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 GlHtml 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 GlHtml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class GlHtml |
||
31 | { |
||
32 | /** |
||
33 | * @var \DOMDocument |
||
34 | */ |
||
35 | private $dom; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $html; |
||
41 | |||
42 | /** |
||
43 | * @param string $html |
||
44 | */ |
||
45 | public function __construct($html) |
||
57 | |||
58 | /** |
||
59 | * Unify newlines |
||
60 | * |
||
61 | * @param string $text |
||
62 | * |
||
63 | * @return string the fixed text |
||
64 | */ |
||
65 | static private function fixNewlines($text) |
||
72 | |||
73 | /** |
||
74 | * return one dom element with $selector css filter |
||
75 | * |
||
76 | * @param string $selector CSS 3 Selector |
||
77 | * |
||
78 | * @return GlHtmlNode[] |
||
79 | */ |
||
80 | public function get($selector) |
||
99 | |||
100 | /** |
||
101 | * set a list of attributes |
||
102 | * |
||
103 | * @param string $selector |
||
104 | * @param array $attributes |
||
105 | */ |
||
106 | public function setAttributes($selector, array $attributes) |
||
114 | |||
115 | /** |
||
116 | * @param string $selector |
||
117 | */ |
||
118 | public function delete($selector) |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function html() |
||
133 | |||
134 | public function getText() |
||
140 | |||
141 | /** |
||
142 | * @param string $tagname |
||
143 | * @param string $attribute |
||
144 | * @param array $links |
||
145 | */ |
||
146 | private function getLinksByTagAttribute($tagname, $attribute, array &$links) |
||
156 | |||
157 | /** |
||
158 | * @param bool $all if true get url in text and params |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getLinks($all = false) |
||
208 | |||
209 | public function getSentences() |
||
236 | |||
237 | /** |
||
238 | * @return GlHtmlSummary[] |
||
239 | */ |
||
240 | public function getSummary() |
||
257 | |||
258 | private function convertHToTree( |
||
295 | |||
296 | public function getSummaryTree() |
||
306 | } |
||
307 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: