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 |
||
| 17 | class PHPCompatibility_Sniffs_PHP_NewExecutionDirectivesSniff |
||
|
|
|||
| 18 | extends PHPCompatibility_AbstractNewFeatureSniff |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * A list of new execution directives |
||
| 23 | * |
||
| 24 | * The array lists : version number with false (not present) or true (present). |
||
| 25 | * If the execution order is conditional, add the condition as a string to the version nr. |
||
| 26 | * If's sufficient to list the first version where the execution directive appears. |
||
| 27 | * |
||
| 28 | * @var array(string => array(string => int|string|null)) |
||
| 29 | */ |
||
| 30 | protected $newDirectives = array ( |
||
| 31 | 'ticks' => array( |
||
| 32 | '3.1' => false, |
||
| 33 | '4.0' => true, |
||
| 34 | 'valid_value_callback' => 'isNumeric', |
||
| 35 | ), |
||
| 36 | 'encoding' => array( |
||
| 37 | '5.2' => false, |
||
| 38 | '5.3' => '--enable-zend-multibyte', // Directive ignored unless. |
||
| 39 | '5.4' => true, |
||
| 40 | 'valid_value_callback' => 'validEncoding', |
||
| 41 | ), |
||
| 42 | 'strict_types' => array( |
||
| 43 | '5.6' => false, |
||
| 44 | '7.0' => true, |
||
| 45 | 'valid_values' => array(1), |
||
| 46 | ), |
||
| 47 | ); |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Tokens to ignore when trying to find the value for the directive. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $ignoreTokens = array(); |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns an array of tokens this test wants to listen for. |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function register() |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Processes this test, when one of its tokens is encountered. |
||
| 74 | * |
||
| 75 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 76 | * @param int $stackPtr The position of the current token in |
||
| 77 | * the stack passed in $tokens. |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Determine whether an error/warning should be thrown for an item based on collected information. |
||
| 139 | * |
||
| 140 | * @param array $errorInfo Detail information about an item. |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | protected function shouldThrowError(array $errorInfo) |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 152 | * |
||
| 153 | * @param array $itemInfo Base information about the item. |
||
| 154 | * |
||
| 155 | * @return array Version and other information about the item. |
||
| 156 | */ |
||
| 157 | public function getItemArray(array $itemInfo) |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | protected function getNonVersionArrayKeys() |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Retrieve the relevant detail (version) information for use in an error message. |
||
| 179 | * |
||
| 180 | * @param array $itemArray Version and other information about the item. |
||
| 181 | * @param array $itemInfo Base information about the item. |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Get the error message template for this sniff. |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | protected function getErrorMsgTemplate() |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Generates the error or warning for this item. |
||
| 220 | * |
||
| 221 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 222 | * @param int $stackPtr The position of the relevant token in |
||
| 223 | * the stack. |
||
| 224 | * @param array $itemInfo Base information about the item. |
||
| 225 | * @param array $errorInfo Array with detail (version) information |
||
| 226 | * relevant to the item. |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Generates a error or warning for this sniff. |
||
| 251 | * |
||
| 252 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 253 | * @param int $stackPtr The position of the execution directive value |
||
| 254 | * in the token array. |
||
| 255 | * @param string $directive The directive. |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | protected function addWarningOnInvalidValue($phpcsFile, $stackPtr, $directive) |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Check whether a value is numeric. |
||
| 296 | * |
||
| 297 | * Callback function to test whether the value for an execution directive is valid. |
||
| 298 | * |
||
| 299 | * @param mixed $value The value to test. |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | protected function isNumeric($value) |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Check whether a value is valid encoding. |
||
| 311 | * |
||
| 312 | * Callback function to test whether the value for an execution directive is valid. |
||
| 313 | * |
||
| 314 | * @param mixed $value The value to test. |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | protected function validEncoding($value) |
||
| 337 | |||
| 338 | |||
| 339 | }//end class |
||
| 340 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.