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 |
||
| 22 | class PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|
|||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * A list of new language constructs, not present in older versions. |
||
| 27 | * |
||
| 28 | * The array lists : version number with false (not present) or true (present). |
||
| 29 | * If's sufficient to list the first version where the keyword appears. |
||
| 30 | * |
||
| 31 | * @var array(string => array(string => int|string|null)) |
||
| 32 | */ |
||
| 33 | protected $newConstructs = array( |
||
| 34 | 'T_NS_SEPARATOR' => array( |
||
| 35 | '5.2' => false, |
||
| 36 | '5.3' => true, |
||
| 37 | 'description' => 'the \ operator (for namespaces)' |
||
| 38 | ), |
||
| 39 | 'T_POW' => array( |
||
| 40 | '5.5' => false, |
||
| 41 | '5.6' => true, |
||
| 42 | 'description' => 'power operator (**)' |
||
| 43 | ), // identified in PHPCS 1.5 as T_MULTIPLY + T_MULTIPLY |
||
| 44 | 'T_POW_EQUAL' => array( |
||
| 45 | '5.5' => false, |
||
| 46 | '5.6' => true, |
||
| 47 | 'description' => 'power assignment operator (**=)' |
||
| 48 | ), // identified in PHPCS 1.5 as T_MULTIPLY + T_MUL_EQUAL |
||
| 49 | 'T_ELLIPSIS' => array( |
||
| 50 | '5.5' => false, |
||
| 51 | '5.6' => true, |
||
| 52 | 'description' => 'variadic functions using ...' |
||
| 53 | ), |
||
| 54 | 'T_SPACESHIP' => array( |
||
| 55 | '5.6' => false, |
||
| 56 | '7.0' => true, |
||
| 57 | 'description' => 'spaceship operator (<=>)' |
||
| 58 | ), // identified in PHPCS 1.5 as T_IS_SMALLER_OR_EQUAL + T_GREATER_THAN |
||
| 59 | 'T_COALESCE' => array( |
||
| 60 | '5.6' => false, |
||
| 61 | '7.0' => true, |
||
| 62 | 'description' => 'null coalescing operator (??)' |
||
| 63 | ), // identified in PHPCS 1.5 as T_INLINE_THEN + T_INLINE_THEN |
||
| 64 | 'T_COALESCE_EQUAL' => array( |
||
| 65 | '7.1' => false, |
||
| 66 | '7.2' => true, |
||
| 67 | 'description' => 'null coalesce equal operator (??=)' |
||
| 68 | ), // identified in PHPCS 1.5 as T_INLINE_THEN + T_INLINE_THEN + T_EQUAL and pre-PHPCS 2.8.1 as T_COALESCE + T_EQUAL |
||
| 69 | ); |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * A list of new language constructs which are not recognized in PHPCS 1.x. |
||
| 74 | * |
||
| 75 | * The array lists an alternative token to listen for. |
||
| 76 | * |
||
| 77 | * @var array(string => int) |
||
| 78 | */ |
||
| 79 | protected $newConstructsPHPCSCompat = array( |
||
| 80 | 'T_POW' => T_MULTIPLY, |
||
| 81 | 'T_POW_EQUAL' => T_MUL_EQUAL, |
||
| 82 | 'T_SPACESHIP' => T_GREATER_THAN, |
||
| 83 | 'T_COALESCE' => T_INLINE_THEN, |
||
| 84 | 'T_COALESCE_EQUAL' => T_EQUAL, |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Translation table for PHPCS 1.x and older 2.x tokens. |
||
| 89 | * |
||
| 90 | * The 'before' index lists the token which would have to be directly before the |
||
| 91 | * token found for it to be one of the new language constructs. |
||
| 92 | * The 'real_token' index indicates which language construct was found in that case. |
||
| 93 | * |
||
| 94 | * If the token combination has multi-layer complexity, such as is the case |
||
| 95 | * with T_COALESCE(_EQUAL), a 'callback' index is added instead pointing to a |
||
| 96 | * separate function which can determine whether this is the targetted token across |
||
| 97 | * PHP and PHPCS versions. |
||
| 98 | * |
||
| 99 | * {@internal 'before' was chosen rather than 'after' as that allowed for a 1-on-1 |
||
| 100 | * translation list with the current tokens.}} |
||
| 101 | * |
||
| 102 | * @var array(string => array(string => string)) |
||
| 103 | */ |
||
| 104 | protected $PHPCSCompatTranslate = array( |
||
| 105 | 'T_MULTIPLY' => array( |
||
| 106 | 'before' => 'T_MULTIPLY', |
||
| 107 | 'real_token' => 'T_POW', |
||
| 108 | ), |
||
| 109 | 'T_MUL_EQUAL' => array( |
||
| 110 | 'before' => 'T_MULTIPLY', |
||
| 111 | 'real_token' => 'T_POW_EQUAL', |
||
| 112 | ), |
||
| 113 | 'T_GREATER_THAN' => array( |
||
| 114 | 'before' => 'T_IS_SMALLER_OR_EQUAL', |
||
| 115 | 'real_token' => 'T_SPACESHIP', |
||
| 116 | ), |
||
| 117 | 'T_INLINE_THEN' => array( |
||
| 118 | 'callback' => 'isTCoalesce', |
||
| 119 | 'real_token' => 'T_COALESCE', |
||
| 120 | ), |
||
| 121 | 'T_EQUAL' => array( |
||
| 122 | 'callback' => 'isTCoalesceEqual', |
||
| 123 | 'real_token' => 'T_COALESCE_EQUAL', |
||
| 124 | ), |
||
| 125 | ); |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns an array of tokens this test wants to listen for. |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function register() |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Processes this test, when one of its tokens is encountered. |
||
| 149 | * |
||
| 150 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 151 | * @param int $stackPtr The position of the current token in |
||
| 152 | * the stack passed in $tokens. |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 198 | * |
||
| 199 | * @param array $itemInfo Base information about the item. |
||
| 200 | * |
||
| 201 | * @return array Version and other information about the item. |
||
| 202 | */ |
||
| 203 | public function getItemArray(array $itemInfo) |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function getNonVersionArrayKeys() |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Retrieve the relevant detail (version) information for use in an error message. |
||
| 222 | * |
||
| 223 | * @param array $itemArray Version and other information about the item. |
||
| 224 | * @param array $itemInfo Base information about the item. |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
||
| 240 | * |
||
| 241 | * @param array $data The error data array which was created. |
||
| 242 | * @param array $itemInfo Base information about the item this error message applied to. |
||
| 243 | * @param array $errorInfo Detail information about an item this error message applied to. |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Callback function to determine whether a T_EQUAL token is really a T_COALESCE_EQUAL token. |
||
| 256 | * |
||
| 257 | * @param array $tokens The token stack. |
||
| 258 | * @param int $stackPtr The current position in the token stack. |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | private function isTCoalesceEqual($tokens, $stackPtr) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Callback function to determine whether a T_INLINE_THEN token is really a T_COALESCE token. |
||
| 283 | * |
||
| 284 | * @param array $tokens The token stack. |
||
| 285 | * @param int $stackPtr The current position in the token stack. |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | private function isTCoalesce($tokens, $stackPtr) |
||
| 305 | |||
| 306 | }//end class |
||
| 307 |
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.