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 Element 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 Element, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Element |
||
29 | extends \DOMElement |
||
30 | implements |
||
31 | \ArrayAccess, |
||
32 | \Countable, |
||
33 | \IteratorAggregate, |
||
34 | Node, |
||
35 | Node\ChildNode, |
||
36 | Node\NonDocumentTypeChildNode, |
||
37 | Node\ParentNode { |
||
38 | |||
39 | use |
||
40 | Node\ChildNode\Implementation, |
||
41 | Node\NonDocumentTypeChildNode\Implementation, |
||
42 | Node\QuerySelector\Implementation, |
||
43 | Node\StringCast, |
||
44 | Node\Xpath, |
||
45 | Node\ParentNode\Implementation |
||
46 | { |
||
47 | Node\ParentNode\Implementation::append as appendToParentNode; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $name |
||
52 | * @return bool |
||
53 | */ |
||
54 | 9 | public function __isset(string $name) { |
|
55 | switch ($name) { |
||
56 | 9 | case 'nextElementSibling' : |
|
57 | 2 | return $this->getNextElementSibling() !== NULL; |
|
58 | 7 | case 'previousElementSibling' : |
|
59 | 2 | return $this->getPreviousElementSibling() !== NULL; |
|
60 | 5 | case 'firstElementChild' : |
|
61 | 2 | return $this->getFirstElementChild() !== NULL; |
|
62 | 3 | case 'lastElementChild' : |
|
63 | 2 | return $this->getLastElementChild() !== NULL; |
|
64 | } |
||
65 | 1 | return FALSE; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $name |
||
70 | * @return \DOMNode|Element|NULL |
||
71 | */ |
||
72 | 5 | public function __get(string $name) { |
|
73 | switch ($name) { |
||
74 | 5 | case 'nextElementSibling' : |
|
75 | 1 | return $this->getNextElementSibling(); |
|
76 | 4 | case 'previousElementSibling' : |
|
77 | 1 | return $this->getPreviousElementSibling(); |
|
78 | 3 | case 'firstElementChild' : |
|
79 | 1 | return $this->getFirstElementChild(); |
|
80 | 2 | case 'lastElementChild' : |
|
81 | 1 | return $this->getLastElementChild(); |
|
82 | } |
||
83 | 1 | return $this->$name; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $name |
||
88 | * @param $value |
||
89 | * @throws \BadMethodCallException |
||
90 | */ |
||
91 | 5 | public function __set(string $name, $value) { |
|
95 | |||
96 | /** |
||
97 | * @param string $name |
||
98 | * @throws \BadMethodCallException |
||
99 | */ |
||
100 | 8 | public function __unset(string $name) { |
|
104 | |||
105 | /** |
||
106 | * @param string $name |
||
107 | * @throws \BadMethodCallException |
||
108 | */ |
||
109 | 13 | private function blockReadOnlyProperties(string $name) { |
|
123 | |||
124 | /** |
||
125 | * Validate if an attribute exists |
||
126 | * |
||
127 | * @param string $name |
||
128 | * @return bool |
||
129 | * @throws \LogicException |
||
130 | */ |
||
131 | 6 | public function hasAttribute($name): bool { |
|
138 | |||
139 | /** |
||
140 | * Get an attribute value |
||
141 | * |
||
142 | * @param string $name |
||
143 | * @return string|NULL |
||
144 | * @throws \LogicException |
||
145 | */ |
||
146 | 4 | View Code Duplication | public function getAttribute($name) { |
153 | |||
154 | /** |
||
155 | * Get an attribute value |
||
156 | * |
||
157 | * @param string $name |
||
158 | * @return Attribute|\DOMAttr|NULL |
||
159 | * @throws \LogicException |
||
160 | */ |
||
161 | 2 | public function getAttributeNode($name) { |
|
168 | |||
169 | /** |
||
170 | * Set an attribute on an element |
||
171 | * |
||
172 | * @todo validate return value |
||
173 | * @param string $name |
||
174 | * @param string $value |
||
175 | * @return Attribute |
||
176 | * @throws \LogicException |
||
177 | */ |
||
178 | 6 | View Code Duplication | public function setAttribute($name, $value) { |
186 | |||
187 | /** |
||
188 | * Set an attribute on an element |
||
189 | * |
||
190 | * @param string $name |
||
191 | * @return bool |
||
192 | * @throws \LogicException |
||
193 | */ |
||
194 | 3 | View Code Duplication | public function removeAttribute($name): bool { |
201 | |||
202 | /** |
||
203 | * Set an attribute on an element |
||
204 | * |
||
205 | * @param string $name |
||
206 | * @param bool $isId |
||
207 | * @throws \LogicException |
||
208 | */ |
||
209 | 2 | View Code Duplication | public function setIdAttribute($name, $isId) { |
217 | |||
218 | /** |
||
219 | * Append a value to the element node |
||
220 | * |
||
221 | * The value can be: |
||
222 | * |
||
223 | * - a node (automatically imported and cloned) |
||
224 | * - an object implementing FluentDOM\Appendable (method appendTo()) |
||
225 | * - a scalar or object castable to string (adds a text node) |
||
226 | * - an array (sets attributes) |
||
227 | * |
||
228 | * @param \Traversable|\DOMNode|Appendable|array|string|callable $value |
||
229 | * @return $this|Element |
||
230 | * @throws \LogicException |
||
231 | */ |
||
232 | 14 | public function append($value): Element { |
|
259 | |||
260 | /** |
||
261 | * Append an child element |
||
262 | * |
||
263 | * @param string $name |
||
264 | * @param string $content |
||
265 | * @param array $attributes |
||
266 | * @return Element |
||
267 | * @throws \LogicException |
||
268 | */ |
||
269 | 1 | public function appendElement(string $name, $content = '', array $attributes = NULL): Element { |
|
275 | |||
276 | /** |
||
277 | * Append an xml fragment to the element node |
||
278 | * |
||
279 | * @param string $xmlFragment |
||
280 | * @throws \InvalidArgumentException |
||
281 | */ |
||
282 | 2 | public function appendXml(string $xmlFragment) { |
|
287 | |||
288 | /** |
||
289 | * save the element node as XML |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | 1 | public function saveXml(): string { |
|
296 | |||
297 | /** |
||
298 | * Save the child nodes of this element as an XML fragment. |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | 1 | public function saveXmlFragment(): string { |
|
309 | |||
310 | /** |
||
311 | * save the element node as HTML |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 2 | public function saveHtml(): string { |
|
318 | |||
319 | /** |
||
320 | * Allow getElementsByTagName to use the defined namespaces. |
||
321 | * |
||
322 | * @param string $name |
||
323 | * @return \DOMNodeList |
||
324 | * @throws \LogicException |
||
325 | */ |
||
326 | 2 | View Code Duplication | public function getElementsByTagName($name): \DOMNodeList { |
333 | |||
334 | /*************************** |
||
335 | * Array Access Interface |
||
336 | ***************************/ |
||
337 | |||
338 | /** |
||
339 | * Validate if an offset exists. If a integer is provided |
||
340 | * it will check for a child node, if a string is provided for an attribute. |
||
341 | * |
||
342 | * @param int|string $offset |
||
343 | * @return bool |
||
344 | * @throws \InvalidArgumentException |
||
345 | */ |
||
346 | 5 | public function offsetExists($offset): bool { |
|
352 | |||
353 | /** |
||
354 | * Get a child node by its numeric index, or an attribute by its name. |
||
355 | * |
||
356 | * @param int|string $offset |
||
357 | * @return \DOMNode|mixed|string |
||
358 | * @throws \InvalidArgumentException |
||
359 | */ |
||
360 | 4 | public function offsetGet($offset) { |
|
366 | |||
367 | /** |
||
368 | * @param int|string $offset |
||
369 | * @param \DOMNode|string $value |
||
370 | * @throws \LogicException |
||
371 | */ |
||
372 | 4 | public function offsetSet($offset, $value) { |
|
390 | |||
391 | /** |
||
392 | * Remove a child node using its index or an attribute node using its name. |
||
393 | * |
||
394 | * @param int|string $offset |
||
395 | * @throws \InvalidArgumentException |
||
396 | */ |
||
397 | 2 | public function offsetUnset($offset) { |
|
404 | |||
405 | /** |
||
406 | * Node offsets are integers, or strings containing only digits. |
||
407 | * |
||
408 | * @param mixed $offset |
||
409 | * @throws \InvalidArgumentException |
||
410 | * @return bool |
||
411 | */ |
||
412 | 14 | private function isNodeOffset($offset): bool { |
|
423 | |||
424 | /** |
||
425 | * Attribute offsets are strings that can not only contains digits. |
||
426 | * |
||
427 | * @param mixed $offset |
||
428 | * @return bool |
||
429 | */ |
||
430 | 7 | private function isAttributeOffset($offset): bool { |
|
433 | |||
434 | /************************* |
||
435 | * Iterator |
||
436 | ************************/ |
||
437 | |||
438 | /** |
||
439 | * Return Iterator for child nodes. |
||
440 | * |
||
441 | * @return \Iterator |
||
442 | */ |
||
443 | 1 | public function getIterator(): \Iterator { |
|
446 | |||
447 | /************************* |
||
448 | * Countable |
||
449 | ************************/ |
||
450 | |||
451 | /** |
||
452 | * Return child node count |
||
453 | * |
||
454 | * @return int |
||
455 | */ |
||
456 | 4 | public function count(): int { |
|
460 | |||
461 | /** |
||
462 | * Resolves a provided tag name into namespace and local name |
||
463 | * |
||
464 | * @param string $name |
||
465 | * @return string[] |
||
466 | * @throws \LogicException |
||
467 | */ |
||
468 | 13 | private function resolveTagName(string $name): array { |
|
476 | |||
477 | /** |
||
478 | * A getter for the owner document |
||
479 | * |
||
480 | * @return Document |
||
481 | */ |
||
482 | 6 | private function getDocument(): Document { |
|
485 | |||
486 | /** |
||
487 | * Sets all namespaces registered on the document as xmlns attributes on the element. |
||
488 | * |
||
489 | * @param NULL|string|array $prefixes |
||
490 | * @throws \LogicException |
||
491 | */ |
||
492 | 4 | public function applyNamespaces($prefixes = NULL) { |
|
508 | |||
509 | /** |
||
510 | * Return TRUE if the provided namespace is the same as the one on the element |
||
511 | * |
||
512 | * @param string $prefix |
||
513 | * @param string $namespaceURI |
||
514 | * @return bool |
||
515 | */ |
||
516 | 4 | private function isCurrentNamespace(string $prefix, string $namespaceURI): bool { |
|
522 | } |
||
523 | } |