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 NewLanguageConstructsSniff extends AbstractNewFeatureSniff |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * A list of new language constructs, not present in older versions. |
||
| 28 | * |
||
| 29 | * The array lists : version number with false (not present) or true (present). |
||
| 30 | * If's sufficient to list the first version where the keyword appears. |
||
| 31 | * |
||
| 32 | * @var array(string => array(string => int|string|null)) |
||
| 33 | */ |
||
| 34 | protected $newConstructs = array( |
||
| 35 | 'T_NS_SEPARATOR' => array( |
||
| 36 | '5.2' => false, |
||
| 37 | '5.3' => true, |
||
| 38 | 'description' => 'the \ operator (for namespaces)', |
||
| 39 | ), |
||
| 40 | 'T_POW' => array( |
||
| 41 | '5.5' => false, |
||
| 42 | '5.6' => true, |
||
| 43 | 'description' => 'power operator (**)', |
||
| 44 | ), // Identified in PHPCS 1.5 as T_MULTIPLY + T_MULTIPLY. |
||
| 45 | 'T_POW_EQUAL' => array( |
||
| 46 | '5.5' => false, |
||
| 47 | '5.6' => true, |
||
| 48 | 'description' => 'power assignment operator (**=)', |
||
| 49 | ), // Identified in PHPCS 1.5 as T_MULTIPLY + T_MUL_EQUAL. |
||
| 50 | 'T_ELLIPSIS' => array( |
||
| 51 | '5.5' => false, |
||
| 52 | '5.6' => true, |
||
| 53 | 'description' => 'variadic functions using ...', |
||
| 54 | ), |
||
| 55 | 'T_SPACESHIP' => array( |
||
| 56 | '5.6' => false, |
||
| 57 | '7.0' => true, |
||
| 58 | 'description' => 'spaceship operator (<=>)', |
||
| 59 | ), // Identified in PHPCS 1.5 as T_IS_SMALLER_OR_EQUAL + T_GREATER_THAN. |
||
| 60 | 'T_COALESCE' => array( |
||
| 61 | '5.6' => false, |
||
| 62 | '7.0' => true, |
||
| 63 | 'description' => 'null coalescing operator (??)', |
||
| 64 | ), // Identified in PHPCS 1.5 as T_INLINE_THEN + T_INLINE_THEN. |
||
| 65 | /* |
||
| 66 | * Was slated for 7.2, but still not implemented. PHPCS however does already tokenize it. |
||
| 67 | * @link https://wiki.php.net/rfc/null_coalesce_equal_operator |
||
| 68 | */ |
||
| 69 | 'T_COALESCE_EQUAL' => array( |
||
| 70 | '7.2' => false, |
||
| 71 | '7.3' => true, |
||
| 72 | 'description' => 'null coalesce equal operator (??=)', |
||
| 73 | ), // 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. |
||
| 74 | ); |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * A list of new language constructs which are not recognized in PHPCS 1.x. |
||
| 79 | * |
||
| 80 | * The array lists an alternative token to listen for. |
||
| 81 | * |
||
| 82 | * @var array(string => int) |
||
| 83 | */ |
||
| 84 | protected $newConstructsPHPCSCompat = array( |
||
| 85 | 'T_POW' => T_MULTIPLY, |
||
| 86 | 'T_POW_EQUAL' => T_MUL_EQUAL, |
||
| 87 | 'T_SPACESHIP' => T_GREATER_THAN, |
||
| 88 | 'T_COALESCE' => T_INLINE_THEN, |
||
| 89 | 'T_COALESCE_EQUAL' => T_EQUAL, |
||
| 90 | ); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Translation table for PHPCS 1.x and older 2.x tokens. |
||
| 94 | * |
||
| 95 | * The 'before' index lists the token which would have to be directly before the |
||
| 96 | * token found for it to be one of the new language constructs. |
||
| 97 | * The 'real_token' index indicates which language construct was found in that case. |
||
| 98 | * |
||
| 99 | * If the token combination has multi-layer complexity, such as is the case |
||
| 100 | * with T_COALESCE(_EQUAL), a 'callback' index is added instead pointing to a |
||
| 101 | * separate function which can determine whether this is the targetted token across |
||
| 102 | * PHP and PHPCS versions. |
||
| 103 | * |
||
| 104 | * {@internal 'before' was chosen rather than 'after' as that allowed for a 1-on-1 |
||
| 105 | * translation list with the current tokens.}} |
||
| 106 | * |
||
| 107 | * @var array(string => array(string => string)) |
||
| 108 | */ |
||
| 109 | protected $PHPCSCompatTranslate = array( |
||
| 110 | 'T_MULTIPLY' => array( |
||
| 111 | 'before' => 'T_MULTIPLY', |
||
| 112 | 'real_token' => 'T_POW', |
||
| 113 | ), |
||
| 114 | 'T_MUL_EQUAL' => array( |
||
| 115 | 'before' => 'T_MULTIPLY', |
||
| 116 | 'real_token' => 'T_POW_EQUAL', |
||
| 117 | ), |
||
| 118 | 'T_GREATER_THAN' => array( |
||
| 119 | 'before' => 'T_IS_SMALLER_OR_EQUAL', |
||
| 120 | 'real_token' => 'T_SPACESHIP', |
||
| 121 | ), |
||
| 122 | 'T_INLINE_THEN' => array( |
||
| 123 | 'callback' => 'isTCoalesce', |
||
| 124 | 'real_token' => 'T_COALESCE', |
||
| 125 | ), |
||
| 126 | 'T_EQUAL' => array( |
||
| 127 | 'callback' => 'isTCoalesceEqual', |
||
| 128 | 'real_token' => 'T_COALESCE_EQUAL', |
||
| 129 | ), |
||
| 130 | ); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns an array of tokens this test wants to listen for. |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function register() |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Processes this test, when one of its tokens is encountered. |
||
| 153 | * |
||
| 154 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 155 | * @param int $stackPtr The position of the current token in |
||
| 156 | * the stack passed in $tokens. |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | 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 |