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 |
||
| 24 | class RemovedGlobalVariablesSniff extends AbstractRemovedFeatureSniff |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * A list of removed global variables with their alternative, if any. |
||
| 29 | * |
||
| 30 | * The array lists : version number with false (deprecated) and true (removed). |
||
| 31 | * If's sufficient to list the first version where the variable was deprecated/removed. |
||
| 32 | * |
||
| 33 | * @var array(string|null) |
||
| 34 | */ |
||
| 35 | protected $removedGlobalVariables = array( |
||
| 36 | 'HTTP_POST_VARS' => array( |
||
| 37 | '5.3' => false, |
||
| 38 | '5.4' => true, |
||
| 39 | 'alternative' => '$_POST', |
||
| 40 | ), |
||
| 41 | 'HTTP_GET_VARS' => array( |
||
| 42 | '5.3' => false, |
||
| 43 | '5.4' => true, |
||
| 44 | 'alternative' => '$_GET', |
||
| 45 | ), |
||
| 46 | 'HTTP_ENV_VARS' => array( |
||
| 47 | '5.3' => false, |
||
| 48 | '5.4' => true, |
||
| 49 | 'alternative' => '$_ENV', |
||
| 50 | ), |
||
| 51 | 'HTTP_SERVER_VARS' => array( |
||
| 52 | '5.3' => false, |
||
| 53 | '5.4' => true, |
||
| 54 | 'alternative' => '$_SERVER', |
||
| 55 | ), |
||
| 56 | 'HTTP_COOKIE_VARS' => array( |
||
| 57 | '5.3' => false, |
||
| 58 | '5.4' => true, |
||
| 59 | 'alternative' => '$_COOKIE', |
||
| 60 | ), |
||
| 61 | 'HTTP_SESSION_VARS' => array( |
||
| 62 | '5.3' => false, |
||
| 63 | '5.4' => true, |
||
| 64 | 'alternative' => '$_SESSION', |
||
| 65 | ), |
||
| 66 | 'HTTP_POST_FILES' => array( |
||
| 67 | '5.3' => false, |
||
| 68 | '5.4' => true, |
||
| 69 | 'alternative' => '$_FILES', |
||
| 70 | ), |
||
| 71 | |||
| 72 | 'HTTP_RAW_POST_DATA' => array( |
||
| 73 | '5.6' => false, |
||
| 74 | '7.0' => true, |
||
| 75 | 'alternative' => 'php://input', |
||
| 76 | ), |
||
| 77 | |||
| 78 | 'php_errormsg' => array( |
||
| 79 | '7.2' => false, |
||
| 80 | 'alternative' => 'error_get_last()', |
||
| 81 | ), |
||
| 82 | ); |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns an array of tokens this test wants to listen for. |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function register() |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Processes this test, when one of its tokens is encountered. |
||
| 99 | * |
||
| 100 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 101 | * @param int $stackPtr The position of the current token in the |
||
| 102 | * stack passed in $tokens. |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 150 | * |
||
| 151 | * @param array $itemInfo Base information about the item. |
||
| 152 | * |
||
| 153 | * @return array Version and other information about the item. |
||
| 154 | */ |
||
| 155 | public function getItemArray(array $itemInfo) |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the error message template for this sniff. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | protected function getErrorMsgTemplate() |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Filter the error message before it's passed to PHPCS. |
||
| 174 | * |
||
| 175 | * @param string $error The error message which was created. |
||
| 176 | * @param array $itemInfo Base information about the item this error message applied to. |
||
| 177 | * @param array $errorInfo Detail information about an item this error message applied to. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Run some additional checks for the `$php_errormsg` variable. |
||
| 191 | * |
||
| 192 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 193 | * @param int $stackPtr The position of the current token in the |
||
| 194 | * stack passed in $tokens. |
||
| 195 | * @param array $tokens Token array of the current file. |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | private function isTargetPHPErrormsgVar(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $tokens) |
||
| 303 | |||
| 304 | |||
| 305 | }//end class |
||
| 306 |