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 |
||
21 | class RemovedConstantsSniff extends AbstractRemovedFeatureSniff |
||
22 | { |
||
23 | /* |
||
24 | @todo Verify this list in detail by checking the constants pages of all extensions! |
||
25 | List currently based on: http://php.net/manual/en/doc.changelog.php |
||
26 | |||
27 | Note: sniff currently uses tabs -> needs to be converted back to spaces. |
||
28 | */ |
||
29 | |||
30 | /** |
||
31 | * A list of removed PHP Constants. |
||
32 | * |
||
33 | * The array lists : version number with false (deprecated) or true (removed). |
||
34 | * If's sufficient to list the first version where the constant was deprecated/removed. |
||
35 | * |
||
36 | * Note: PHP Constants are case-sensitive! |
||
37 | * |
||
38 | * @var array(string => array(string => bool|string|null)) |
||
39 | */ |
||
40 | protected $removedConstants = array( |
||
41 | 'PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT' => array( |
||
42 | '7.0' => true, |
||
43 | ), |
||
44 | |||
45 | 'INTL_IDNA_VARIANT_2003' => array( |
||
46 | '7.2' => false, |
||
47 | // 8.0. => true, |
||
|
|||
48 | ), |
||
49 | |||
50 | ); |
||
51 | |||
52 | |||
53 | /** |
||
54 | * Returns an array of tokens this test wants to listen for. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function register() |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Processes this test, when one of its tokens is encountered. |
||
67 | * |
||
68 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
69 | * @param int $stackPtr The position of the current token in |
||
70 | * the stack passed in $tokens. |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | View Code Duplication | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
93 | |||
94 | |||
95 | /** |
||
96 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
97 | * |
||
98 | * @param array $itemInfo Base information about the item. |
||
99 | * |
||
100 | * @return array Version and other information about the item. |
||
101 | */ |
||
102 | public function getItemArray(array $itemInfo) |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Get the error message template for this sniff. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | protected function getErrorMsgTemplate() |
||
117 | |||
118 | }//end class |
||
119 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.