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 |
||
9 | class Codepoint implements Comparable |
||
10 | { |
||
11 | const MIN = 0x0; |
||
12 | const MAX = 0x10FFFF; |
||
13 | |||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $value; |
||
18 | |||
19 | /** |
||
20 | * @param int $value |
||
21 | * @throws OutOfRangeException |
||
22 | * @throws InvalidArgumentException |
||
23 | */ |
||
24 | private function __construct($value) |
||
36 | |||
37 | /** |
||
38 | * @param string $value |
||
39 | * @return self |
||
40 | */ |
||
41 | public static function fromHex($value) |
||
45 | |||
46 | /** |
||
47 | * @param int $value |
||
48 | * @return self |
||
49 | */ |
||
50 | public static function fromInt($value) |
||
54 | |||
55 | /** |
||
56 | * @param string $value |
||
57 | * @return self |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | public static function fromUTF8($value) |
||
67 | |||
68 | /** |
||
69 | * @param string $value |
||
70 | * @return self |
||
71 | * @throws InvalidArgumentException |
||
72 | */ |
||
73 | public static function fromUTF16LE($value) |
||
80 | |||
81 | /** |
||
82 | * @param string $value |
||
83 | * @return self |
||
84 | * @throws InvalidArgumentException |
||
85 | */ |
||
86 | public static function fromUTF16BE($value) |
||
93 | |||
94 | /** |
||
95 | * @param string $value |
||
96 | * @return self |
||
97 | * @throws InvalidArgumentException |
||
98 | */ |
||
99 | public static function fromUTF32LE($value) |
||
106 | |||
107 | /** |
||
108 | * @param string $value |
||
109 | * @return self |
||
110 | * @throws InvalidArgumentException |
||
111 | */ |
||
112 | public static function fromUTF32BE($value) |
||
119 | |||
120 | /** |
||
121 | * @param string $character |
||
122 | * @param TransformationFormat $convertFrom |
||
123 | * @return self |
||
124 | * @throws InvalidArgumentException |
||
125 | */ |
||
126 | public static function fromEncodedCharacter($character, TransformationFormat $convertFrom) |
||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | public function toUTF8() |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function toUTF16LE() |
||
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | public function toUTF16BE() |
||
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | public function toUTF32LE() |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function toUTF32BE() |
||
186 | |||
187 | /** |
||
188 | * @param TransformationFormat $convertTo |
||
189 | * @return string |
||
190 | * @throws InvalidArgumentException |
||
191 | */ |
||
192 | public function toEncodedCharacter(TransformationFormat $convertTo) |
||
199 | |||
200 | /** |
||
201 | * @return int |
||
202 | */ |
||
203 | public function getValue() |
||
207 | |||
208 | /** |
||
209 | * @param mixed $other |
||
210 | * @return bool |
||
211 | */ |
||
212 | View Code Duplication | public function equals($other) |
|
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function __toString() |
||
229 | } |
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.