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 |
||
| 19 | class PHPCompatibility_Sniffs_PHP_NewInterfacesSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|
|||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * A list of new interfaces, not present in older versions. |
||
| 24 | * |
||
| 25 | * The array lists : version number with false (not present) or true (present). |
||
| 26 | * If's sufficient to list the first version where the interface appears. |
||
| 27 | * |
||
| 28 | * @var array(string => array(string => int|string|null)) |
||
| 29 | */ |
||
| 30 | protected $newInterfaces = array( |
||
| 31 | 'Traversable' => array( |
||
| 32 | '4.4' => false, |
||
| 33 | '5.0' => true |
||
| 34 | ), |
||
| 35 | |||
| 36 | 'Countable' => array( |
||
| 37 | '5.0' => false, |
||
| 38 | '5.1' => true |
||
| 39 | ), |
||
| 40 | 'OuterIterator' => array( |
||
| 41 | '5.0' => false, |
||
| 42 | '5.1' => true |
||
| 43 | ), |
||
| 44 | 'RecursiveIterator' => array( |
||
| 45 | '5.0' => false, |
||
| 46 | '5.1' => true |
||
| 47 | ), |
||
| 48 | 'SeekableIterator' => array( |
||
| 49 | '5.0' => false, |
||
| 50 | '5.1' => true |
||
| 51 | ), |
||
| 52 | 'Serializable' => array( |
||
| 53 | '5.0' => false, |
||
| 54 | '5.1' => true, |
||
| 55 | ), |
||
| 56 | 'SplObserver' => array( |
||
| 57 | '5.0' => false, |
||
| 58 | '5.1' => true |
||
| 59 | ), |
||
| 60 | 'SplSubject' => array( |
||
| 61 | '5.0' => false, |
||
| 62 | '5.1' => true |
||
| 63 | ), |
||
| 64 | |||
| 65 | 'JsonSerializable' => array( |
||
| 66 | '5.3' => false, |
||
| 67 | '5.4' => true |
||
| 68 | ), |
||
| 69 | 'SessionHandlerInterface' => array( |
||
| 70 | '5.3' => false, |
||
| 71 | '5.4' => true |
||
| 72 | ), |
||
| 73 | |||
| 74 | 'DateTimeInterface' => array( |
||
| 75 | '5.4' => false, |
||
| 76 | '5.5' => true |
||
| 77 | ), |
||
| 78 | |||
| 79 | 'Throwable' => array( |
||
| 80 | '5.6' => false, |
||
| 81 | '7.0' => true |
||
| 82 | ), |
||
| 83 | |||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * A list of methods which cannot be used in combination with particular interfaces. |
||
| 88 | * |
||
| 89 | * @var array(string => array(string => string)) |
||
| 90 | */ |
||
| 91 | protected $unsupportedMethods = array( |
||
| 92 | 'Serializable' => array( |
||
| 93 | '__sleep' => 'http://php.net/serializable', |
||
| 94 | '__wakeup' => 'http://php.net/serializable', |
||
| 95 | ), |
||
| 96 | ); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns an array of tokens this test wants to listen for. |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function register() |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Processes this test, when one of its tokens is encountered. |
||
| 126 | * |
||
| 127 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 128 | * @param int $stackPtr The position of the current token in |
||
| 129 | * the stack passed in $tokens. |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Processes this test for when a class token is encountered. |
||
| 158 | * |
||
| 159 | * - Detect classes implementing the new interfaces. |
||
| 160 | * - Detect classes implementing the new interfaces with unsupported functions. |
||
| 161 | * |
||
| 162 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 163 | * @param int $stackPtr The position of the current token in |
||
| 164 | * the stack passed in $tokens. |
||
| 165 | * |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | private function processClassToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Processes this test for when a function token is encountered. |
||
| 223 | * |
||
| 224 | * - Detect new interfaces when used as a type hint. |
||
| 225 | * |
||
| 226 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 227 | * @param int $stackPtr The position of the current token in |
||
| 228 | * the stack passed in $tokens. |
||
| 229 | * |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | private function processFunctionToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 256 | * |
||
| 257 | * @param array $itemInfo Base information about the item. |
||
| 258 | * |
||
| 259 | * @return array Version and other information about the item. |
||
| 260 | */ |
||
| 261 | public function getItemArray(array $itemInfo) |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the error message template for this sniff. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | protected function getErrorMsgTemplate() |
||
| 276 | |||
| 277 | |||
| 278 | }//end class |
||
| 279 |
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.