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:
1 | <?php |
||
8 | class BasicProperties { |
||
9 | private $data; |
||
10 | private $headers; |
||
11 | private $formatter; |
||
12 | |||
13 | public function __construct($data, &$headers, Formatter $formatter) { |
||
14 | $this->data = $data; |
||
15 | $this->headers = &$headers; |
||
16 | $this->formatter = $formatter; |
||
17 | } |
||
18 | |||
19 | public function content($value, $element, $rule) { |
||
20 | $value = $this->formatter->format($value, $rule->getRules()); |
||
21 | if (!$this->processPseudo($value, $element, $rule)) { |
||
22 | //Remove the current contents |
||
23 | $this->removeAllChildren($element); |
||
24 | //Now make a text node |
||
25 | if ($this->getContentMode($rule->getRules()) === 'replace') $this->replaceContent($element, $value); |
||
26 | else $this->appendContent($element, $value); |
||
27 | } |
||
28 | } |
||
29 | |||
30 | private function getContentMode($rules) { |
||
31 | return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
||
32 | } |
||
33 | |||
34 | private function processPseudo($value, $element, $rule) { |
||
35 | return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule); |
||
36 | } |
||
37 | |||
38 | private function pseudoAttr($value, $element, $rule) { |
||
39 | if ($attr = $rule->getPseudoMatcher()->attr()) { |
||
40 | $element->setAttribute($attr, implode('', $value)); |
||
41 | return true; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | private function pseudoHeader($value, $element, $rule) { |
||
46 | if ($header = $rule->getPseudoMatcher()->header($element)) { |
||
47 | $this->headers[] = [$header, implode('', $value)]; |
||
48 | return true; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | private function pseudoBefore($value, $element, $rule) { |
||
53 | View Code Duplication | if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) { |
|
|
|||
54 | $element->firstChild->nodeValue = implode('', $value) . $element->firstChild->nodeValue; |
||
55 | return true; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | private function pseudoAfter($value, $element, $rule) { |
||
60 | View Code Duplication | if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) { |
|
61 | $element->firstChild->nodeValue .= implode('', $value); |
||
62 | return true; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | private function appendToIfNode($element, $content, $appendTo) { |
||
67 | if (isset($content[0]) && $content[0] instanceof \DomNode) { |
||
68 | foreach ($content as $node) { |
||
69 | $node = $element->ownerDocument->importNode($node, true); |
||
70 | $appendTo->appendChild($node); |
||
71 | } |
||
72 | return true; |
||
73 | } |
||
74 | return false; |
||
75 | } |
||
76 | |||
77 | private function replaceContent($element, $content) { |
||
78 | View Code Duplication | if (!$this->appendToIfNode($element, $content, $element->parentNode)) { |
|
79 | $element->parentNode->appendChild($element->ownerDocument->createElement('span', implode('', $content))); |
||
80 | } |
||
81 | $element->setAttribute('transphporm', 'remove'); |
||
82 | } |
||
83 | |||
84 | private function appendContent($element, $content) { |
||
85 | View Code Duplication | if (!$this->appendToIfNode($element, $content, $element)) { |
|
86 | $element->appendChild($element->ownerDocument->createTextNode(implode('', $content))); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | private function removeAllChildren($element) { |
||
91 | while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
||
92 | } |
||
93 | |||
94 | private function createHook($newRules, $rule) { |
||
95 | $hook = new Rule($newRules, $rule->getPseudoMatcher(), $this->data); |
||
96 | foreach ($rule->getProperties() as $obj) $hook->registerProperties($obj); |
||
97 | return $hook; |
||
98 | } |
||
99 | |||
100 | public function repeat($value, $element, $rule) { |
||
101 | if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element); |
||
102 | |||
103 | foreach ($value as $key => $iteration) { |
||
104 | $clone = $element->cloneNode(true); |
||
105 | //Mark this node as having been added by transphporm |
||
106 | $clone->setAttribute('transphporm', 'added'); |
||
107 | $this->data->bind($clone, $iteration, 'iteration'); |
||
108 | $this->data->bind($clone, $key, 'key'); |
||
109 | $element->parentNode->insertBefore($clone, $element); |
||
110 | |||
111 | //Re-run the hook on the new element, but use the iterated data |
||
112 | $newRules = $rule->getRules(); |
||
113 | //Don't run repeat on the clones element or it will loop forever |
||
114 | unset($newRules['repeat']); |
||
115 | |||
116 | $this->createHook($newRules, $rule)->run($clone); |
||
117 | } |
||
118 | //Flag the original element for removal |
||
119 | $element->setAttribute('transphporm', 'remove'); |
||
120 | return false; |
||
121 | } |
||
122 | |||
123 | public function display($value, $element) { |
||
124 | if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove'); |
||
125 | else $element->setAttribute('transphporm', 'show'); |
||
126 | } |
||
127 | |||
128 | public function bind($value, $element) { |
||
131 | |||
132 | } |
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.