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 |
||
18 | class Util{ |
||
19 | |||
20 | /** |
||
21 | * @param string $s |
||
22 | * |
||
23 | * @return bool |
||
24 | */ |
||
25 | public static function isNumber($s){ |
||
38 | |||
39 | /** |
||
40 | * @param string $s |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | public static function isAlphaNum($s){ |
||
57 | |||
58 | /** |
||
59 | * @param string $s |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public static function isKanji($s){ |
||
83 | |||
84 | /** |
||
85 | * @param int $data |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | View Code Duplication | public static function getBCHTypeInfo($data){ |
|
98 | |||
99 | /** |
||
100 | * @param int $data |
||
101 | * |
||
102 | * @return int |
||
103 | */ |
||
104 | View Code Duplication | public static function getBCHTypeNumber($data){ |
|
113 | |||
114 | /** |
||
115 | * @param int $data |
||
116 | * |
||
117 | * @return int |
||
118 | */ |
||
119 | public static function getBCHDigit($data){ |
||
129 | |||
130 | /** |
||
131 | * @param int $typeNumber |
||
132 | * @param int $errorCorrectLevel |
||
133 | * |
||
134 | * @return array |
||
135 | * @throws \chillerlan\QRCode\QRCodeException |
||
136 | */ |
||
137 | public static function getRSBlocks($typeNumber, $errorCorrectLevel){ |
||
158 | |||
159 | /** |
||
160 | * @param int $typeNumber |
||
161 | * @param int $mode |
||
162 | * @param int $ecLevel |
||
163 | * |
||
164 | * @return int |
||
165 | * @throws \chillerlan\QRCode\QRCodeException |
||
166 | */ |
||
167 | public static function getMaxLength($typeNumber, $mode, $ecLevel){ |
||
182 | |||
183 | |||
184 | } |
||
185 |
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.