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 |
||
10 | View Code Duplication | class SimpleXmlDomNode extends AbstractSimpleXmlDomNode implements SimpleXmlDomNodeInterface |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * Find list of nodes with a CSS or xPath selector. |
||
14 | * |
||
15 | * @param string $selector |
||
16 | * @param int|null $idx |
||
17 | * |
||
18 | * @return SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|SimpleXmlDomNodeInterface[]|null |
||
19 | */ |
||
20 | 3 | public function find(string $selector, $idx = null) |
|
21 | { |
||
22 | // init |
||
23 | 3 | $elements = new static(); |
|
24 | |||
25 | 3 | foreach ($this as $node) { |
|
26 | \assert($node instanceof SimpleXmlDomInterface); |
||
27 | 3 | foreach ($node->find($selector) as $res) { |
|
28 | 3 | $elements->append($res); |
|
29 | } |
||
30 | } |
||
31 | |||
32 | // return all elements |
||
33 | 3 | if ($idx === null) { |
|
34 | 3 | if (\count($elements) === 0) { |
|
35 | return new SimpleXmlDomNodeBlank(); |
||
36 | } |
||
37 | |||
38 | 3 | return $elements; |
|
39 | } |
||
40 | |||
41 | // handle negative values |
||
42 | if ($idx < 0) { |
||
43 | $idx = \count($elements) + $idx; |
||
44 | } |
||
45 | |||
46 | // return one element |
||
47 | return $elements[$idx] ?? null; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Find nodes with a CSS or xPath selector. |
||
52 | * |
||
53 | * @param string $selector |
||
54 | * |
||
55 | * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
56 | */ |
||
57 | public function findMulti(string $selector): SimpleXmlDomNodeInterface |
||
61 | |||
62 | /** |
||
63 | * Find nodes with a CSS or xPath selector. |
||
64 | * |
||
65 | * @param string $selector |
||
66 | * |
||
67 | * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
68 | */ |
||
69 | public function findMultiOrFalse(string $selector) |
||
79 | |||
80 | /** |
||
81 | * Find one node with a CSS or xPath selector. |
||
82 | * |
||
83 | * @param string $selector |
||
84 | * |
||
85 | * @return SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null |
||
86 | */ |
||
87 | public function findOne(string $selector) |
||
91 | |||
92 | /** |
||
93 | * Find one node with a CSS or xPath selector or false, if no element is found. |
||
94 | * |
||
95 | * @param string $selector |
||
96 | * |
||
97 | * @return false|SimpleXmlDomNodeInterface<SimpleXmlDomInterface> |
||
98 | */ |
||
99 | public function findOneOrFalse(string $selector) |
||
110 | |||
111 | /** |
||
112 | * Get html of elements. |
||
113 | * |
||
114 | * @return string[] |
||
115 | */ |
||
116 | public function innerHtml(): array |
||
127 | |||
128 | /** |
||
129 | * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
||
130 | * |
||
131 | * @return string[] |
||
132 | */ |
||
133 | public function innertext() |
||
137 | |||
138 | /** |
||
139 | * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x) |
||
140 | * |
||
141 | * @return string[] |
||
142 | */ |
||
143 | public function outertext() |
||
147 | |||
148 | /** |
||
149 | * Get plain text. |
||
150 | * |
||
151 | * @return string[] |
||
152 | */ |
||
153 | public function text(): array |
||
164 | } |
||
165 |
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.