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 ByteOrderMarkSniff implements Sniff |
||
34 | { |
||
35 | /** |
||
36 | * Returns an array of tokens this test wants to listen for. |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function register() |
||
44 | |||
45 | /** |
||
46 | * List of supported BOM definitions. |
||
47 | * |
||
48 | * Use encoding names as keys and hex BOM representations as values. |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | protected function getBomDefinitions() |
||
62 | |||
63 | /** |
||
64 | * Process tokens. |
||
65 | * |
||
66 | * Actually, only proceed when we're at index 0, this should be the only case |
||
67 | * that will contain BOM. Then check if BOM definition matches what |
||
68 | * we've found as file's inline HTML. Inline HTML could be longer than just BOM |
||
69 | * so make sure you test as much as needed. |
||
70 | * |
||
71 | * @param File $phpcsFile The current file being scanned. |
||
72 | * @param int $stackPtr The position of the current token |
||
73 | * in the stack passed in $tokens. |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function process(File $phpcsFile, $stackPtr ) |
||
98 | } |
||
99 |
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.