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 |
||
19 | class PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff extends PHPCompatibility_Sniff |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * A list of removed global variables with their alternative, if any |
||
24 | * Array codes : 0 = removed/unavailable, -1 = deprecated, 1 = active |
||
25 | * |
||
26 | * @var array(string|null) |
||
27 | */ |
||
28 | protected $removedGlobalVariables = array( |
||
29 | 'HTTP_RAW_POST_DATA' => array( |
||
30 | '5.5' => 1, |
||
31 | '5.6' => -1, |
||
32 | '7.0' => 0, |
||
33 | 'alternative' => 'php://input' |
||
34 | ), |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * Returns an array of tokens this test wants to listen for. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function register() |
||
47 | |||
48 | /** |
||
49 | * Processes this test, when one of its tokens is encountered. |
||
50 | * |
||
51 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
52 | * @param int $stackPtr The position of the current token in the |
||
53 | * stack passed in $tokens. |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
98 | |||
99 | }//end class |
||
100 |
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.