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() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $html |
||
| 81 | */ |
||
| 82 | public |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $html |
||
| 93 | */ |
||
| 94 | public function replaceInner($html) |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @return \DOMNode |
||
| 103 | */ |
||
| 104 | public function getDOMNode() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getName() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $name |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function getSentences() |
||
| 155 | |||
| 156 | public |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public |
||
| 176 | |||
| 177 | /** |
||
| 178 | * extract h tag from html |
||
| 179 | * |
||
| 180 | * @param \DOMNodeList $nodeList |
||
| 181 | * @param callable $fct |
||
| 182 | */ |
||
| 183 | private function recursiveCallChild(\DOMNodeList $nodeList, callable $fct) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param callable $fct |
||
| 198 | */ |
||
| 199 | public function callChild(callable $fct) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param \DOMNode $node |
||
| 206 | * @param string $sentence |
||
| 207 | * @param array $sentences |
||
| 208 | * |
||
| 209 | * @return int |
||
| 210 | */ |
||
| 211 | private static function iterateSentencesOverNode(\DOMNode $node, &$sentence, array &$sentences) |
||
| 291 | } |
||
| 292 |
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
SomeClassto useselfinstead: