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 | class Utf8EncodingSniff implements Sniff |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Returns an array of tokens this test wants to listen for. |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | public function register() |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Processes this test, when one of its tokens is encountered. |
||
52 | * |
||
53 | * @param File $phpcsFile The current file being scanned. |
||
54 | * @param int $stackPtr The position of the current token |
||
55 | * in the stack passed in $tokens. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public function process(File $phpcsFile, $stackPtr) |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Checks that the string $content contains only valid UTF-8 chars |
||
88 | * using W3C's method. |
||
89 | * Returns true if $content contains only UTF-8 chars, false otherwise. |
||
90 | * |
||
91 | * @param string $content String to check. |
||
92 | * |
||
93 | * @return bool true if $content contains only UTF-8 chars, false otherwise. |
||
94 | * |
||
95 | * @see http://w3.org/International/questions/qa-forms-utf-8.html |
||
96 | */ |
||
97 | private static function _checkUtf8W3c($content) |
||
123 | |||
124 | /** |
||
125 | * Checks that the string $content contains only valid UTF-8 chars |
||
126 | * using the method described in RFC 3629. |
||
127 | * Returns true if $content contains only UTF-8 chars, false otherwise. |
||
128 | * |
||
129 | * @param string $content String to check. |
||
130 | * |
||
131 | * @return bool true if $content contains only UTF-8 chars, false otherwise. |
||
132 | * |
||
133 | * @see http://www.php.net/manual/en/function.mb-detect-encoding.php#85294 |
||
134 | */ |
||
135 | private static function _checkUtf8Rfc3629($content) |
||
169 | |||
170 | /** |
||
171 | * Splits a string to chunks of given size |
||
172 | * This helps to avoid segmentation fault errors when large text is given |
||
173 | * Returns array of strings after splitting |
||
174 | * |
||
175 | * @param string $str String to split. |
||
176 | * @param int $len number of characters per chunk |
||
177 | * |
||
178 | * @return array string array after splitting |
||
179 | * |
||
180 | * @see http://php.net/manual/en/function.chunk-split.php |
||
181 | */ |
||
182 | private static function mb_chunk_split($str, $len, $glue) |
||
198 | /** |
||
199 | * Supporting function for mb_chunk_split |
||
200 | * |
||
201 | * @param string $str |
||
202 | * |
||
203 | * @return array |
||
204 | * |
||
205 | * @see http://php.net/manual/en/function.chunk-split.php |
||
206 | */ |
||
207 | private static function mbStringToArray ($str) |
||
217 | |||
218 | |||
219 | |||
220 | }//end class |
||
221 | |||
223 |
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.