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 \DOMElement|\DOMNode|null |
||
11 | */ |
||
12 | protected $node; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected static $functionAliases = [ |
||
18 | 'children' => 'childNodes', |
||
19 | 'first_child' => 'firstChild', |
||
20 | 'last_child' => 'lastChild', |
||
21 | 'next_sibling' => 'nextSibling', |
||
22 | 'prev_sibling' => 'previousSibling', |
||
23 | 'parent' => 'parentNode', |
||
24 | 'outertext' => 'html', |
||
25 | 'outerhtml' => 'html', |
||
26 | 'innertext' => 'innerHtml', |
||
27 | 'innerhtml' => 'innerHtml', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @param string $name |
||
32 | * @param array $arguments |
||
33 | * |
||
34 | * @throws \BadMethodCallException |
||
35 | * |
||
36 | * @return SimpleHtmlDomInterface|string|null |
||
37 | */ |
||
38 | View Code Duplication | public function __call($name, $arguments) |
|
48 | |||
49 | /** |
||
50 | * @param string $name |
||
51 | * |
||
52 | * @return array|string|null |
||
53 | */ |
||
54 | 53 | public function __get($name) |
|
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 | public function __isset($name) |
|
121 | |||
122 | /** |
||
123 | * @param string $name |
||
124 | * @param mixed $value |
||
125 | * |
||
126 | * @return SimpleHtmlDomInterface|null |
||
127 | */ |
||
128 | 17 | public function __set($name, $value) |
|
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 setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface; |
||
171 | |||
172 | abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface; |
||
173 | |||
174 | abstract protected function replaceChildWithString(string $string): SimpleHtmlDomInterface; |
||
175 | |||
176 | abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface; |
||
177 | |||
178 | abstract public function hasAttribute(string $name): bool; |
||
179 | |||
180 | abstract public function find(string $selector, $idx = null); |
||
181 | |||
182 | abstract public function getAttribute(string $name): string; |
||
183 | |||
184 | abstract public function getAllAttributes(); |
||
185 | |||
186 | abstract public function text(): string; |
||
187 | |||
188 | abstract public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string; |
||
189 | |||
190 | abstract public function html(bool $multiDecodeNewHtmlEntity = false): string; |
||
191 | |||
192 | abstract public function removeAttribute(string $name): SimpleHtmlDomInterface; |
||
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.