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 NewKeywordsSniff extends AbstractNewFeatureSniff |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * A list of new keywords, 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 last version which did not contain the keyword. |
||
| 31 | * |
||
| 32 | * Description will be used as part of the error message. |
||
| 33 | * Condition is the name of a callback method within this class or the parent class |
||
| 34 | * which checks whether the token complies with a certain condition. |
||
| 35 | * The callback function will be passed the $phpcsFile and the $stackPtr. |
||
| 36 | * The callback function should return `true` if the condition is met and the |
||
| 37 | * error should *not* be thrown. |
||
| 38 | * |
||
| 39 | * @var array(string => array(string => int|string|null)) |
||
| 40 | */ |
||
| 41 | protected $newKeywords = array( |
||
| 42 | 'T_HALT_COMPILER' => array( |
||
| 43 | '5.0' => false, |
||
| 44 | '5.1' => true, |
||
| 45 | 'description' => '"__halt_compiler" keyword', |
||
| 46 | ), |
||
| 47 | 'T_CONST' => array( |
||
| 48 | '5.2' => false, |
||
| 49 | '5.3' => true, |
||
| 50 | 'description' => '"const" keyword', |
||
| 51 | 'condition' => 'isClassConstant', // Keyword is only new when not in class context. |
||
| 52 | ), |
||
| 53 | 'T_CALLABLE' => array( |
||
| 54 | '5.3' => false, |
||
| 55 | '5.4' => true, |
||
| 56 | 'description' => '"callable" keyword', |
||
| 57 | 'content' => 'callable', |
||
| 58 | ), |
||
| 59 | 'T_DIR' => array( |
||
| 60 | '5.2' => false, |
||
| 61 | '5.3' => true, |
||
| 62 | 'description' => '__DIR__ magic constant', |
||
| 63 | 'content' => '__DIR__', |
||
| 64 | ), |
||
| 65 | 'T_GOTO' => array( |
||
| 66 | '5.2' => false, |
||
| 67 | '5.3' => true, |
||
| 68 | 'description' => '"goto" keyword', |
||
| 69 | 'content' => 'goto', |
||
| 70 | ), |
||
| 71 | 'T_INSTEADOF' => array( |
||
| 72 | '5.3' => false, |
||
| 73 | '5.4' => true, |
||
| 74 | 'description' => '"insteadof" keyword (for traits)', |
||
| 75 | 'content' => 'insteadof', |
||
| 76 | ), |
||
| 77 | 'T_NAMESPACE' => array( |
||
| 78 | '5.2' => false, |
||
| 79 | '5.3' => true, |
||
| 80 | 'description' => '"namespace" keyword', |
||
| 81 | 'content' => 'namespace', |
||
| 82 | ), |
||
| 83 | 'T_NS_C' => array( |
||
| 84 | '5.2' => false, |
||
| 85 | '5.3' => true, |
||
| 86 | 'description' => '__NAMESPACE__ magic constant', |
||
| 87 | 'content' => '__NAMESPACE__', |
||
| 88 | ), |
||
| 89 | 'T_USE' => array( |
||
| 90 | '5.2' => false, |
||
| 91 | '5.3' => true, |
||
| 92 | 'description' => '"use" keyword (for traits/namespaces/anonymous functions)', |
||
| 93 | ), |
||
| 94 | 'T_START_NOWDOC' => array( |
||
| 95 | '5.2' => false, |
||
| 96 | '5.3' => true, |
||
| 97 | 'description' => 'nowdoc functionality', |
||
| 98 | ), |
||
| 99 | 'T_END_NOWDOC' => array( |
||
| 100 | '5.2' => false, |
||
| 101 | '5.3' => true, |
||
| 102 | 'description' => 'nowdoc functionality', |
||
| 103 | ), |
||
| 104 | 'T_START_HEREDOC' => array( |
||
| 105 | '5.2' => false, |
||
| 106 | '5.3' => true, |
||
| 107 | 'description' => '(Double) quoted Heredoc identifier', |
||
| 108 | 'condition' => 'isNotQuoted', // Heredoc is only new with quoted identifier. |
||
| 109 | ), |
||
| 110 | 'T_TRAIT' => array( |
||
| 111 | '5.3' => false, |
||
| 112 | '5.4' => true, |
||
| 113 | 'description' => '"trait" keyword', |
||
| 114 | 'content' => 'trait', |
||
| 115 | ), |
||
| 116 | 'T_TRAIT_C' => array( |
||
| 117 | '5.3' => false, |
||
| 118 | '5.4' => true, |
||
| 119 | 'description' => '__TRAIT__ magic constant', |
||
| 120 | 'content' => '__TRAIT__', |
||
| 121 | ), |
||
| 122 | // The specifics for distinguishing between 'yield' and 'yield from' are dealt |
||
| 123 | // with in the translation logic. |
||
| 124 | // This token has to be placed above the `T_YIELD` token in this array to allow for this. |
||
| 125 | 'T_YIELD_FROM' => array( |
||
| 126 | '5.6' => false, |
||
| 127 | '7.0' => true, |
||
| 128 | 'description' => '"yield from" keyword (for generators)', |
||
| 129 | 'content' => 'yield', |
||
| 130 | ), |
||
| 131 | 'T_YIELD' => array( |
||
| 132 | '5.4' => false, |
||
| 133 | '5.5' => true, |
||
| 134 | 'description' => '"yield" keyword (for generators)', |
||
| 135 | 'content' => 'yield', |
||
| 136 | ), |
||
| 137 | 'T_FINALLY' => array( |
||
| 138 | '5.4' => false, |
||
| 139 | '5.5' => true, |
||
| 140 | 'description' => '"finally" keyword (in exception handling)', |
||
| 141 | 'content' => 'finally', |
||
| 142 | ), |
||
| 143 | ); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Translation table for T_STRING tokens. |
||
| 147 | * |
||
| 148 | * Will be set up from the register() method. |
||
| 149 | * |
||
| 150 | * @var array(string => string) |
||
| 151 | */ |
||
| 152 | protected $translateContentToToken = array(); |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns an array of tokens this test wants to listen for. |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function register() |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Processes this test, when one of its tokens is encountered. |
||
| 189 | * |
||
| 190 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 191 | * @param int $stackPtr The position of the current token in |
||
| 192 | * the stack passed in $tokens. |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 293 | * |
||
| 294 | * @param array $itemInfo Base information about the item. |
||
| 295 | * |
||
| 296 | * @return array Version and other information about the item. |
||
| 297 | */ |
||
| 298 | public function getItemArray(array $itemInfo) |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | protected function getNonVersionArrayKeys() |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Retrieve the relevant detail (version) information for use in an error message. |
||
| 321 | * |
||
| 322 | * @param array $itemArray Version and other information about the item. |
||
| 323 | * @param array $itemInfo Base information about the item. |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
||
| 339 | * |
||
| 340 | * @param array $data The error data array which was created. |
||
| 341 | * @param array $itemInfo Base information about the item this error message applied to. |
||
| 342 | * @param array $errorInfo Detail information about an item this error message applied to. |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Callback for the quoted heredoc identifier condition. |
||
| 355 | * |
||
| 356 | * A double quoted identifier will have the opening quote on position 3 |
||
| 357 | * in the string: `<<<"ID"`. |
||
| 358 | * |
||
| 359 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 360 | * @param int $stackPtr The position of the current token in |
||
| 361 | * the stack passed in $tokens. |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public function isNotQuoted(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 370 | |||
| 371 | |||
| 372 | }//end class |
||
| 373 |