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 |
||
12 | View Code Duplication | class SimpleXmlDomBlank extends AbstractSimpleXmlDom implements \IteratorAggregate, SimpleXmlDomInterface |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @param string $name |
||
16 | * @param array $arguments |
||
17 | * |
||
18 | * @throws \BadMethodCallException |
||
19 | * |
||
20 | * @return SimpleXmlDomInterface|string|null |
||
21 | */ |
||
22 | public function __call($name, $arguments) |
||
32 | |||
33 | /** |
||
34 | * Find list of nodes with a CSS selector. |
||
35 | * |
||
36 | * @param string $selector |
||
37 | * @param int|null $idx |
||
38 | * |
||
39 | * @return SimpleXmlDomNodeInterface |
||
40 | */ |
||
41 | public function find(string $selector, $idx = null) |
||
45 | |||
46 | /** |
||
47 | * Returns an array of attributes. |
||
48 | * |
||
49 | * @return null |
||
50 | */ |
||
51 | public function getAllAttributes() |
||
55 | |||
56 | /** |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function hasAttributes(): bool |
||
63 | |||
64 | /** |
||
65 | * Return attribute value. |
||
66 | * |
||
67 | * @param string $name |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getAttribute(string $name): string |
||
75 | |||
76 | /** |
||
77 | * Determine if an attribute exists on the element. |
||
78 | * |
||
79 | * @param string $name |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function hasAttribute(string $name): bool |
||
87 | |||
88 | /** |
||
89 | * Get dom node's inner xml. |
||
90 | * |
||
91 | * @param bool $multiDecodeNewHtmlEntity |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
||
99 | |||
100 | /** |
||
101 | * Remove attribute. |
||
102 | * |
||
103 | * @param string $name <p>The name of the html-attribute.</p> |
||
104 | * |
||
105 | * @return SimpleXmlDomInterface |
||
106 | */ |
||
107 | public function removeAttribute(string $name): SimpleXmlDomInterface |
||
111 | |||
112 | protected function replaceChildWithString(string $string): SimpleXmlDomInterface |
||
116 | |||
117 | protected function replaceNodeWithString(string $string): SimpleXmlDomInterface |
||
121 | |||
122 | protected function replaceTextWithString($string): SimpleXmlDomInterface |
||
126 | |||
127 | /** |
||
128 | * Set attribute value. |
||
129 | * |
||
130 | * @param string $name <p>The name of the html-attribute.</p> |
||
131 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
132 | * @param bool $strict </p> |
||
133 | * $value must be NULL, to remove the attribute, |
||
134 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
135 | * </p> |
||
136 | * |
||
137 | * @return SimpleXmlDomInterface |
||
138 | */ |
||
139 | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface |
||
143 | |||
144 | /** |
||
145 | * Get dom node's plain text. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function text(): string |
||
153 | |||
154 | /** |
||
155 | * Get dom node's outer html. |
||
156 | * |
||
157 | * @param bool $multiDecodeNewHtmlEntity |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function xml(bool $multiDecodeNewHtmlEntity = false): string |
||
165 | |||
166 | /** |
||
167 | * Returns children of node. |
||
168 | * |
||
169 | * @param int $idx |
||
170 | * |
||
171 | * @return null |
||
172 | */ |
||
173 | public function childNodes(int $idx = -1) |
||
177 | |||
178 | /** |
||
179 | * Find nodes with a CSS selector. |
||
180 | * |
||
181 | * @param string $selector |
||
182 | * |
||
183 | * @return SimpleXmlDomNodeInterface |
||
184 | */ |
||
185 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
189 | |||
190 | /** |
||
191 | * Find nodes with a CSS selector or false, if no element is found. |
||
192 | * |
||
193 | * @param string $selector |
||
194 | * |
||
195 | * @return false |
||
196 | */ |
||
197 | public function findMultiOrFalse(string $selector) |
||
201 | |||
202 | /** |
||
203 | * Find one node with a CSS selector. |
||
204 | * |
||
205 | * @param string $selector |
||
206 | * |
||
207 | * @return SimpleXmlDomInterface |
||
208 | */ |
||
209 | public function findOne(string $selector): SimpleXmlDomInterface |
||
213 | |||
214 | /** |
||
215 | * Find one node with a CSS selector or false, if no element is found. |
||
216 | * |
||
217 | * @param string $selector |
||
218 | * |
||
219 | * @return false |
||
220 | */ |
||
221 | public function findOneOrFalse(string $selector) |
||
225 | |||
226 | /** |
||
227 | * Returns the first child of node. |
||
228 | * |
||
229 | * @return null |
||
230 | */ |
||
231 | public function firstChild() |
||
235 | |||
236 | /** |
||
237 | * Return elements by ".class". |
||
238 | * |
||
239 | * @param string $class |
||
240 | * |
||
241 | * @return SimpleXmlDomNodeInterface |
||
242 | */ |
||
243 | public function getElementByClass(string $class): SimpleXmlDomNodeInterface |
||
247 | |||
248 | /** |
||
249 | * Return element by #id. |
||
250 | * |
||
251 | * @param string $id |
||
252 | * |
||
253 | * @return SimpleXmlDomInterface |
||
254 | */ |
||
255 | public function getElementById(string $id): SimpleXmlDomInterface |
||
259 | |||
260 | /** |
||
261 | * Return element by tag name. |
||
262 | * |
||
263 | * @param string $name |
||
264 | * |
||
265 | * @return SimpleXmlDomInterface |
||
266 | */ |
||
267 | public function getElementByTagName(string $name): SimpleXmlDomInterface |
||
271 | |||
272 | /** |
||
273 | * Returns elements by "#id". |
||
274 | * |
||
275 | * @param string $id |
||
276 | * @param int|null $idx |
||
277 | * |
||
278 | * @return SimpleXmlDomNodeInterface |
||
279 | */ |
||
280 | public function getElementsById(string $id, $idx = null) |
||
284 | |||
285 | /** |
||
286 | * Returns elements by tag name. |
||
287 | * |
||
288 | * @param string $name |
||
289 | * @param int|null $idx |
||
290 | * |
||
291 | * @return SimpleXmlDomNodeInterface |
||
292 | */ |
||
293 | public function getElementsByTagName(string $name, $idx = null) |
||
297 | |||
298 | /** |
||
299 | * @return \DOMNode |
||
300 | */ |
||
301 | public function getNode(): \DOMNode |
||
305 | |||
306 | /** |
||
307 | * Create a new "XmlDomParser"-object from the current context. |
||
308 | * |
||
309 | * @return XmlDomParser |
||
310 | */ |
||
311 | public function getXmlDomParser(): XmlDomParser |
||
315 | |||
316 | /** |
||
317 | * Get dom node's inner html. |
||
318 | * |
||
319 | * @param bool $multiDecodeNewHtmlEntity |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
||
327 | |||
328 | /** |
||
329 | * Nodes can get partially destroyed in which they're still an |
||
330 | * actual DOM node (such as \DOMElement) but almost their entire |
||
331 | * body is gone, including the `nodeType` attribute. |
||
332 | * |
||
333 | * @return bool true if node has been destroyed |
||
334 | */ |
||
335 | public function isRemoved(): bool |
||
339 | |||
340 | /** |
||
341 | * Returns the last child of node. |
||
342 | * |
||
343 | * @return null |
||
344 | */ |
||
345 | public function lastChild() |
||
349 | |||
350 | /** |
||
351 | * Returns the next sibling of node. |
||
352 | * |
||
353 | * @return null |
||
354 | */ |
||
355 | public function nextSibling() |
||
359 | |||
360 | /** |
||
361 | * Returns the next sibling of node. |
||
362 | * |
||
363 | * @return null |
||
364 | */ |
||
365 | public function nextNonWhitespaceSibling() |
||
369 | |||
370 | /** |
||
371 | * Returns the parent of node. |
||
372 | * |
||
373 | * @return SimpleXmlDomInterface |
||
374 | */ |
||
375 | public function parentNode(): SimpleXmlDomInterface |
||
379 | |||
380 | /** |
||
381 | * Returns the previous sibling of node. |
||
382 | * |
||
383 | * @return null |
||
384 | */ |
||
385 | public function previousSibling() |
||
389 | |||
390 | /** |
||
391 | * @param string|string[]|null $value <p> |
||
392 | * null === get the current input value |
||
393 | * text === set a new input value |
||
394 | * </p> |
||
395 | * |
||
396 | * @return string|string[]|null |
||
397 | */ |
||
398 | public function val($value = null) |
||
402 | |||
403 | /** |
||
404 | * Retrieve an external iterator. |
||
405 | * |
||
406 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
407 | * |
||
408 | * @return SimpleXmlDomNodeInterface |
||
409 | * <p> |
||
410 | * An instance of an object implementing <b>Iterator</b> or |
||
411 | * <b>Traversable</b> |
||
412 | * </p> |
||
413 | */ |
||
414 | public function getIterator(): SimpleXmlDomNodeInterface |
||
418 | } |
||
419 |
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.