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 | class SimpleHtmlAttributes implements SimpleHtmlAttributesInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $attributeName; |
||
16 | |||
17 | /** |
||
18 | * @var null|\DOMElement |
||
19 | */ |
||
20 | private $element; |
||
21 | |||
22 | /** |
||
23 | * @var string[] |
||
24 | */ |
||
25 | private $tokens = []; |
||
26 | |||
27 | /** |
||
28 | * @var null|string |
||
29 | */ |
||
30 | private $previousValue; |
||
31 | |||
32 | /** |
||
33 | * Creates a list of space-separated tokens based on the attribute value of an element. |
||
34 | * |
||
35 | * @param null|\DOMElement $element |
||
36 | * <p>The DOM element.</p> |
||
37 | * @param string $attributeName |
||
38 | * <p>The name of the attribute.</p> |
||
39 | */ |
||
40 | 12 | public function __construct($element, string $attributeName) |
|
47 | |||
48 | /** @noinspection MagicMethodsValidityInspection */ |
||
49 | /** |
||
50 | * Returns the value for the property specified. |
||
51 | * |
||
52 | * @param string $name The name of the property |
||
53 | * |
||
54 | * @return string The value of the property specified |
||
55 | */ |
||
56 | 3 | public function __get(string $name) |
|
70 | |||
71 | /** |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | 4 | public function __toString(): string |
|
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function add(string ...$tokens) |
||
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | */ |
||
105 | 1 | public function contains(string $token): bool |
|
111 | |||
112 | /** |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | 1 | public function entries(): \ArrayIterator |
|
121 | |||
122 | 1 | public function item(int $index) |
|
131 | |||
132 | /** |
||
133 | * {@inheritDoc} |
||
134 | */ |
||
135 | 1 | public function remove(string ...$tokens) |
|
156 | |||
157 | /** |
||
158 | * {@inheritDoc} |
||
159 | */ |
||
160 | 1 | public function replace(string $old, string $new) |
|
181 | |||
182 | /** |
||
183 | * {@inheritDoc} |
||
184 | */ |
||
185 | 2 | public function toggle(string $token, bool $force = null): bool |
|
216 | |||
217 | /** |
||
218 | * @return \DOMAttr|false|null |
||
219 | */ |
||
220 | 4 | private function setAttributeValue() |
|
235 | |||
236 | /** |
||
237 | * @return void |
||
238 | */ |
||
239 | 12 | private function tokenize() |
|
268 | |||
269 | } |
||
270 |
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.