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 |
||
7 | abstract class AbstractSimpleHtmlDom |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected static $functionAliases = [ |
||
13 | 'children' => 'childNodes', |
||
14 | 'first_child' => 'firstChild', |
||
15 | 'last_child' => 'lastChild', |
||
16 | 'next_sibling' => 'nextSibling', |
||
17 | 'prev_sibling' => 'previousSibling', |
||
18 | 'parent' => 'parentNode', |
||
19 | 'outertext' => 'html', |
||
20 | 'outerhtml' => 'html', |
||
21 | 'innertext' => 'innerHtml', |
||
22 | 'innerhtml' => 'innerHtml', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var \DOMElement|\DOMNode|null |
||
27 | */ |
||
28 | protected $node; |
||
29 | |||
30 | /** |
||
31 | * @param string $name |
||
32 | * @param array $arguments |
||
33 | * |
||
34 | * @throws \BadMethodCallException |
||
35 | * |
||
36 | * @return SimpleHtmlDomInterface|string|null |
||
37 | */ |
||
38 | public function __call($name, $arguments) |
||
39 | { |
||
40 | $name = \strtolower($name); |
||
41 | |||
42 | if (isset(self::$functionAliases[$name])) { |
||
43 | return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments); |
||
44 | } |
||
45 | |||
46 | throw new \BadMethodCallException('Method does not exist'); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param string $name |
||
51 | * |
||
52 | * @return array|string|null |
||
53 | */ |
||
54 | 56 | public function __get($name) |
|
55 | { |
||
56 | 56 | $nameOrig = $name; |
|
57 | 56 | $name = \strtolower($name); |
|
58 | |||
59 | 56 | switch ($name) { |
|
60 | 56 | case 'outerhtml': |
|
61 | 51 | case 'outertext': |
|
62 | 41 | case 'html': |
|
63 | 24 | return $this->html(); |
|
64 | 41 | case 'innerhtml': |
|
65 | 34 | case 'innertext': |
|
66 | 12 | return $this->innerHtml(); |
|
67 | 31 | case 'text': |
|
68 | 26 | case 'plaintext': |
|
69 | 20 | return $this->text(); |
|
70 | 15 | case 'tag': |
|
71 | 5 | return $this->node ? $this->node->nodeName : ''; |
|
72 | 13 | case 'attr': |
|
73 | return $this->getAllAttributes(); |
||
74 | default: |
||
75 | 13 | if ($this->node && \property_exists($this->node, $nameOrig)) { |
|
76 | 2 | return HtmlDomParser::putReplacedBackToPreserveHtmlEntities($this->node->{$nameOrig}); |
|
77 | } |
||
78 | |||
79 | 12 | return $this->getAttribute($name); |
|
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param string $selector |
||
85 | * @param int $idx |
||
86 | * |
||
87 | * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface |
||
88 | */ |
||
89 | 12 | public function __invoke($selector, $idx = null) |
|
93 | |||
94 | /** |
||
95 | * @param string $name |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 1 | View Code Duplication | public function __isset($name) |
|
|||
100 | { |
||
101 | 1 | $nameOrig = $name; |
|
102 | 1 | $name = \strtolower($name); |
|
103 | |||
104 | 1 | switch ($name) { |
|
105 | 1 | case 'outertext': |
|
106 | 1 | case 'outerhtml': |
|
107 | 1 | case 'innertext': |
|
108 | 1 | case 'innerhtml': |
|
109 | 1 | case 'plaintext': |
|
110 | 1 | case 'text': |
|
111 | 1 | case 'tag': |
|
112 | return true; |
||
113 | default: |
||
114 | 1 | if ($this->node && \property_exists($this->node, $nameOrig)) { |
|
115 | return isset($this->node->{$nameOrig}); |
||
116 | } |
||
117 | |||
118 | 1 | return $this->hasAttribute($name); |
|
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $name |
||
124 | * @param mixed $value |
||
125 | * |
||
126 | * @return SimpleHtmlDomInterface|null |
||
127 | */ |
||
128 | 17 | View Code Duplication | public function __set($name, $value) |
129 | { |
||
130 | 17 | $nameOrig = $name; |
|
131 | 17 | $name = \strtolower($name); |
|
132 | |||
133 | 17 | switch ($name) { |
|
134 | 17 | case 'outerhtml': |
|
135 | 15 | case 'outertext': |
|
136 | 4 | return $this->replaceNodeWithString($value); |
|
137 | 13 | case 'innertext': |
|
138 | 11 | case 'innerhtml': |
|
139 | 7 | return $this->replaceChildWithString($value); |
|
140 | 10 | case 'plaintext': |
|
141 | 1 | return $this->replaceTextWithString($value); |
|
142 | default: |
||
143 | 9 | if ($this->node && \property_exists($this->node, $nameOrig)) { |
|
144 | return $this->node->{$nameOrig} = $value; |
||
145 | } |
||
146 | |||
147 | 9 | return $this->setAttribute($name, $value); |
|
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | 2 | public function __toString() |
|
158 | |||
159 | /** |
||
160 | * @param string $name |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function __unset($name) |
||
169 | |||
170 | abstract public function find(string $selector, $idx = null); |
||
171 | |||
172 | abstract public function getAllAttributes(); |
||
173 | |||
174 | abstract public function getAttribute(string $name): string; |
||
175 | |||
176 | abstract public function hasAttribute(string $name): bool; |
||
177 | |||
178 | abstract public function html(bool $multiDecodeNewHtmlEntity = false): string; |
||
179 | |||
180 | abstract public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string; |
||
181 | |||
182 | abstract public function removeAttribute(string $name): SimpleHtmlDomInterface; |
||
183 | |||
184 | abstract protected function replaceChildWithString(string $string): SimpleHtmlDomInterface; |
||
185 | |||
186 | abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface; |
||
187 | |||
188 | abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface; |
||
189 | |||
190 | abstract public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface; |
||
191 | |||
192 | abstract public function text(): string; |
||
193 | } |
||
194 |
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.