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 |
||
8 | class Codepoint implements Comparable |
||
9 | { |
||
10 | const MIN = 0x0; |
||
11 | const MAX = 0x10FFFF; |
||
12 | |||
13 | /** |
||
14 | * @var int |
||
15 | */ |
||
16 | private $value; |
||
17 | |||
18 | /** |
||
19 | * @param int $value |
||
20 | * @throws OutOfRangeException |
||
21 | * @throws InvalidArgumentException |
||
22 | */ |
||
23 | private function __construct($value) |
||
35 | |||
36 | /** |
||
37 | * @param string $value |
||
38 | * @return self |
||
39 | */ |
||
40 | public static function fromHex($value) |
||
44 | |||
45 | /** |
||
46 | * @param int $value |
||
47 | * @return self |
||
48 | */ |
||
49 | public static function fromInt($value) |
||
53 | |||
54 | /** |
||
55 | * @param string $value |
||
56 | * @return self |
||
57 | * @throws InvalidArgumentException |
||
58 | */ |
||
59 | public static function fromUTF8($value) |
||
66 | |||
67 | /** |
||
68 | * @param string $value |
||
69 | * @return self |
||
70 | * @throws InvalidArgumentException |
||
71 | */ |
||
72 | public static function fromUTF16($value) |
||
79 | |||
80 | /** |
||
81 | * @param string $character |
||
82 | * @param TransformationFormat $encoding |
||
83 | * @return self |
||
84 | * @throws InvalidArgumentException |
||
85 | */ |
||
86 | View Code Duplication | public static function fromEncodedCharacter($character, TransformationFormat $encoding) |
|
93 | |||
94 | /** |
||
95 | * @param string $value |
||
96 | * @return self |
||
97 | * @throws InvalidArgumentException |
||
98 | */ |
||
99 | public static function fromUTF32($value) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public function toUTF8() |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function toUTF16() |
||
131 | |||
132 | /** |
||
133 | * @param TransformationFormat $encoding |
||
134 | * @return string |
||
135 | * @throws InvalidArgumentException |
||
136 | */ |
||
137 | View Code Duplication | public function toEncodedCharacter(TransformationFormat $encoding) |
|
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function toUTF32() |
||
152 | |||
153 | /** |
||
154 | * @return int |
||
155 | */ |
||
156 | public function getValue() |
||
160 | |||
161 | /** |
||
162 | * @param mixed $other |
||
163 | * @return bool |
||
164 | */ |
||
165 | View Code Duplication | public function equals($other) |
|
174 | |||
175 | /** |
||
176 | * @return string |
||
177 | */ |
||
178 | public function __toString() |
||
182 | } |
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.