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 |
||
38 | class AlphabeticalUseStatementsSniff extends UseDeclarationSniff |
||
39 | { |
||
40 | |||
41 | const NAMESPACE_SEPRATOR_STRING = '\\'; |
||
42 | |||
43 | /** |
||
44 | * Sorting order, can be one of: |
||
45 | * 'dictionary', 'string', 'string-locale' or 'string-case-insensitive' |
||
46 | * |
||
47 | * Unknown types will be mapped to 'string'. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | public $order = 'dictionary'; |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Supported ordering methods |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $supportedOrderingMethods = [ |
||
60 | 'dictionary', |
||
61 | 'string', |
||
62 | 'string', |
||
63 | 'string-locale', |
||
64 | 'string-case-insensitive', |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Last import seen in group |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $lastImport = ''; |
||
73 | |||
74 | /** |
||
75 | * Line number of the last seen use statement |
||
76 | * |
||
77 | * @var integer |
||
78 | */ |
||
79 | private $lastLine = -1; |
||
80 | |||
81 | /** |
||
82 | * Current file |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | private $currentFile = null; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Returns an array of tokens this test wants to listen for. |
||
91 | * |
||
92 | * @return array |
||
|
|||
93 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
||
94 | */ |
||
95 | public function register() |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Processes this test, when one of its tokens is encountered. |
||
115 | * |
||
116 | * @param File $phpcsFile The file being scanned. |
||
117 | * @param int $stackPtr The position of the current token in |
||
118 | * the stack passed in $tokens. |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function process(File $phpcsFile, $stackPtr) |
||
182 | |||
183 | |||
184 | /** |
||
185 | * Get the import class name for use statement pointed by $stackPtr. |
||
186 | * |
||
187 | * @param File $phpcsFile PHP CS File |
||
188 | * @param int $stackPtr pointer |
||
189 | * |
||
190 | * @return array|false |
||
191 | */ |
||
192 | private function getUseImport(File $phpcsFile, $stackPtr) |
||
220 | |||
221 | |||
222 | /** |
||
223 | * Get the full use statement as string, including trailing white space. |
||
224 | * |
||
225 | * @param File $phpcsFile PHP CS File |
||
226 | * @param int $stackPtr pointer |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | private function getUseStatementAsString( |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Check if "use" token is not used for import. |
||
251 | * E.g. function () use () {...}. |
||
252 | * |
||
253 | * @param File $phpcsFile PHP CS File |
||
254 | * @param int $stackPtr pointer |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | private function checkIsNonImportUse(File $phpcsFile, $stackPtr) |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Replace all the token in same line as the element pointed to by $stackPtr |
||
286 | * the by the empty string. |
||
287 | * This will delete the line. |
||
288 | * |
||
289 | * @param File $phpcsFile PHP CS file |
||
290 | * @param int $stackPtr pointer |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | private function fixerClearLine(File $phpcsFile, $stackPtr) |
||
308 | |||
309 | |||
310 | /** |
||
311 | * Find a new destination pointer for the given import string in current |
||
312 | * use block. |
||
313 | * |
||
314 | * @param File $phpcsFile PHP CS File |
||
315 | * @param int $stackPtr pointer |
||
316 | * @param string $import import string requiring new position |
||
317 | * |
||
318 | * @return int |
||
319 | */ |
||
320 | private function findNewDestination( |
||
351 | |||
352 | |||
353 | /** |
||
354 | * Compare namespace strings according defined order function. |
||
355 | * |
||
356 | * @param string $a first namespace string |
||
357 | * @param string $b second namespace string |
||
358 | * |
||
359 | * @return int |
||
360 | */ |
||
361 | private function compareString($a, $b) |
||
376 | |||
377 | |||
378 | /** |
||
379 | * Lexicographical namespace string compare. |
||
380 | * |
||
381 | * Example: |
||
382 | * |
||
383 | * use Doctrine\ORM\Query; |
||
384 | * use Doctrine\ORM\Query\Expr; |
||
385 | * use Doctrine\ORM\QueryBuilder; |
||
386 | * |
||
387 | * @param string $a first namespace string |
||
388 | * @param string $b second namespace string |
||
389 | * |
||
390 | * @return int |
||
391 | */ |
||
392 | private function dictionaryCompare($a, $b) |
||
421 | |||
422 | |||
423 | }//end class |
||
424 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.