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