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 |
||
33 | trait UtilityTrait |
||
34 | { |
||
35 | /** |
||
36 | * epoch for this uuid lib |
||
37 | * |
||
38 | * @var string |
||
39 | * @access protected |
||
40 | * @staticvar |
||
41 | */ |
||
42 | protected static $epoch = '2016/01/01'; |
||
43 | |||
44 | /** |
||
45 | * {@inheritDoc} |
||
46 | */ |
||
47 | public static function isValid(/*# string */ $uuid)/*# : bool */ |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | public static function info(/*# string */ $uuid)/*# : array */ |
||
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | */ |
||
78 | View Code Duplication | public static function encode( |
|
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | View Code Duplication | public static function decode( |
|
107 | |||
108 | /** |
||
109 | * Convert numerical string between bases |
||
110 | * |
||
111 | * @param string $input |
||
112 | * @param string $fromBase |
||
113 | * @param string $toBase |
||
114 | * @return string |
||
115 | * @access protected |
||
116 | * @static |
||
117 | */ |
||
118 | protected static function convertBase( |
||
133 | |||
134 | /** |
||
135 | * Convert to decimal string |
||
136 | * |
||
137 | * @param string $input |
||
138 | * @param string $fromBase |
||
139 | * @return string |
||
140 | * @access protected |
||
141 | */ |
||
142 | protected static function toBase10( |
||
153 | |||
154 | /** |
||
155 | * Convert from decimal string |
||
156 | * |
||
157 | * @param string $input |
||
158 | * @param string $toBase |
||
159 | * @return string |
||
160 | * @access protected |
||
161 | */ |
||
162 | protected static function fromBase10( |
||
175 | |||
176 | /** |
||
177 | * Reverse of getTimestamp(), convert 15-char string to unix time |
||
178 | * |
||
179 | * @param string $hexString |
||
180 | * @return int |
||
181 | * @access protected |
||
182 | * @static |
||
183 | */ |
||
184 | protected static function toTimeStamp(/*# string */ $hexString)/*# : int */ |
||
189 | |||
190 | /** |
||
191 | * Time related part |
||
192 | * |
||
193 | * @return string 15-char string |
||
194 | * @access protected |
||
195 | */ |
||
196 | protected function getTimestamp()/*# : string */ |
||
204 | |||
205 | /** |
||
206 | * Get a pseudo sequence number |
||
207 | * |
||
208 | * @return int |
||
209 | * @access protected |
||
210 | * @static |
||
211 | */ |
||
212 | protected function getSequence()/*# : int */ |
||
217 | } |
||
218 |
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.