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_NewClassesSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|
|||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * A list of new classes, 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 class appears. |
||
| 30 | * |
||
| 31 | * @var array(string => array(string => bool)) |
||
| 32 | */ |
||
| 33 | protected $newClasses = array( |
||
| 34 | 'libXMLError' => array( |
||
| 35 | '5.0' => false, |
||
| 36 | '5.1' => true |
||
| 37 | ), |
||
| 38 | |||
| 39 | 'DateTime' => array( |
||
| 40 | '5.1' => false, |
||
| 41 | '5.2' => true |
||
| 42 | ), |
||
| 43 | 'DateTimeZone' => array( |
||
| 44 | '5.1' => false, |
||
| 45 | '5.2' => true |
||
| 46 | ), |
||
| 47 | 'RegexIterator' => array( |
||
| 48 | '5.1' => false, |
||
| 49 | '5.2' => true |
||
| 50 | ), |
||
| 51 | 'RecursiveRegexIterator' => array( |
||
| 52 | '5.1' => false, |
||
| 53 | '5.2' => true |
||
| 54 | ), |
||
| 55 | |||
| 56 | 'DateInterval' => array( |
||
| 57 | '5.2' => false, |
||
| 58 | '5.3' => true |
||
| 59 | ), |
||
| 60 | 'DatePeriod' => array( |
||
| 61 | '5.2' => false, |
||
| 62 | '5.3' => true |
||
| 63 | ), |
||
| 64 | 'Phar' => array( |
||
| 65 | '5.2' => false, |
||
| 66 | '5.3' => true |
||
| 67 | ), |
||
| 68 | 'PharData' => array( |
||
| 69 | '5.2' => false, |
||
| 70 | '5.3' => true |
||
| 71 | ), |
||
| 72 | 'PharException' => array( |
||
| 73 | '5.2' => false, |
||
| 74 | '5.3' => true |
||
| 75 | ), |
||
| 76 | 'PharFileInfo' => array( |
||
| 77 | '5.2' => false, |
||
| 78 | '5.3' => true |
||
| 79 | ), |
||
| 80 | 'FilesystemIterator' => array( |
||
| 81 | '5.2' => false, |
||
| 82 | '5.3' => true |
||
| 83 | ), |
||
| 84 | 'GlobIterator' => array( |
||
| 85 | '5.2' => false, |
||
| 86 | '5.3' => true |
||
| 87 | ), |
||
| 88 | 'MultipleIterator' => array( |
||
| 89 | '5.2' => false, |
||
| 90 | '5.3' => true |
||
| 91 | ), |
||
| 92 | 'RecursiveTreeIterator' => array( |
||
| 93 | '5.2' => false, |
||
| 94 | '5.3' => true |
||
| 95 | ), |
||
| 96 | 'SplDoublyLinkedList' => array( |
||
| 97 | '5.2' => false, |
||
| 98 | '5.3' => true |
||
| 99 | ), |
||
| 100 | 'SplFixedArray' => array( |
||
| 101 | '5.2' => false, |
||
| 102 | '5.3' => true |
||
| 103 | ), |
||
| 104 | 'SplHeap' => array( |
||
| 105 | '5.2' => false, |
||
| 106 | '5.3' => true |
||
| 107 | ), |
||
| 108 | 'SplMaxHeap' => array( |
||
| 109 | '5.2' => false, |
||
| 110 | '5.3' => true |
||
| 111 | ), |
||
| 112 | 'SplMinHeap' => array( |
||
| 113 | '5.2' => false, |
||
| 114 | '5.3' => true |
||
| 115 | ), |
||
| 116 | 'SplPriorityQueue' => array( |
||
| 117 | '5.2' => false, |
||
| 118 | '5.3' => true |
||
| 119 | ), |
||
| 120 | 'SplQueue' => array( |
||
| 121 | '5.2' => false, |
||
| 122 | '5.3' => true |
||
| 123 | ), |
||
| 124 | 'SplStack' => array( |
||
| 125 | '5.2' => false, |
||
| 126 | '5.3' => true |
||
| 127 | ), |
||
| 128 | |||
| 129 | 'CallbackFilterIterator' => array( |
||
| 130 | '5.3' => false, |
||
| 131 | '5.4' => true |
||
| 132 | ), |
||
| 133 | 'RecursiveCallbackFilterIterator' => array( |
||
| 134 | '5.3' => false, |
||
| 135 | '5.4' => true |
||
| 136 | ), |
||
| 137 | 'ReflectionZendExtension' => array( |
||
| 138 | '5.3' => false, |
||
| 139 | '5.4' => true |
||
| 140 | ), |
||
| 141 | 'SessionHandler' => array( |
||
| 142 | '5.3' => false, |
||
| 143 | '5.4' => true |
||
| 144 | ), |
||
| 145 | 'SNMP' => array( |
||
| 146 | '5.3' => false, |
||
| 147 | '5.4' => true |
||
| 148 | ), |
||
| 149 | 'Transliterator' => array( |
||
| 150 | '5.3' => false, |
||
| 151 | '5.4' => true |
||
| 152 | ), |
||
| 153 | 'Spoofchecker' => array( |
||
| 154 | '5.3' => false, |
||
| 155 | '5.4' => true |
||
| 156 | ), |
||
| 157 | |||
| 158 | 'CURLFile' => array( |
||
| 159 | '5.4' => false, |
||
| 160 | '5.5' => true |
||
| 161 | ), |
||
| 162 | 'DateTimeImmutable' => array( |
||
| 163 | '5.4' => false, |
||
| 164 | '5.5' => true |
||
| 165 | ), |
||
| 166 | 'IntlCalendar' => array( |
||
| 167 | '5.4' => false, |
||
| 168 | '5.5' => true |
||
| 169 | ), |
||
| 170 | 'IntlGregorianCalendar' => array( |
||
| 171 | '5.4' => false, |
||
| 172 | '5.5' => true |
||
| 173 | ), |
||
| 174 | 'IntlTimeZone' => array( |
||
| 175 | '5.4' => false, |
||
| 176 | '5.5' => true |
||
| 177 | ), |
||
| 178 | 'IntlBreakIterator' => array( |
||
| 179 | '5.4' => false, |
||
| 180 | '5.5' => true |
||
| 181 | ), |
||
| 182 | 'IntlRuleBasedBreakIterator' => array( |
||
| 183 | '5.4' => false, |
||
| 184 | '5.5' => true |
||
| 185 | ), |
||
| 186 | 'IntlCodePointBreakIterator' => array( |
||
| 187 | '5.4' => false, |
||
| 188 | '5.5' => true |
||
| 189 | ), |
||
| 190 | |||
| 191 | ); |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns an array of tokens this test wants to listen for. |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function register() |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Processes this test, when one of its tokens is encountered. |
||
| 223 | * |
||
| 224 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 225 | * @param int $stackPtr The position of the current token in |
||
| 226 | * the stack passed in $tokens. |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * Processes this test for when a token resulting in a singular class name is encountered. |
||
| 250 | * |
||
| 251 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 252 | * @param int $stackPtr The position of the current token in |
||
| 253 | * the stack passed in $tokens. |
||
| 254 | * |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | private function processSingularToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Processes this test for when a function token is encountered. |
||
| 298 | * |
||
| 299 | * - Detect new classes when used as a type hint. |
||
| 300 | * |
||
| 301 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 302 | * @param int $stackPtr The position of the current token in |
||
| 303 | * the stack passed in $tokens. |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | View Code Duplication | private function processFunctionToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 332 | * |
||
| 333 | * @param array $itemInfo Base information about the item. |
||
| 334 | * |
||
| 335 | * @return array Version and other information about the item. |
||
| 336 | */ |
||
| 337 | public function getItemArray(array $itemInfo) |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Get the error message template for this sniff. |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | protected function getErrorMsgTemplate() |
||
| 352 | |||
| 353 | |||
| 354 | }//end class |
||
| 355 |
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.