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 | abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
||
|
|
|||
| 23 | { |
||
| 24 | |||
| 25 | /* The testVersion configuration variable may be in any of the following formats: |
||
| 26 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
| 27 | * disables all the checks provided by this standard. |
||
| 28 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
| 29 | * the code will run on that version of PHP (no deprecated features or newer |
||
| 30 | * features being used). |
||
| 31 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
| 32 | * on all PHP versions in that range, and that it doesn't use any features that |
||
| 33 | * were deprecated by the final version in the list, or which were not available |
||
| 34 | * for the first version in the list. |
||
| 35 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
| 36 | * would be treated as invalid, and ignored. |
||
| 37 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
| 38 | * is recognised is "5.0". |
||
| 39 | */ |
||
| 40 | |||
| 41 | private function getTestVersion() |
||
| 82 | |||
| 83 | View Code Duplication | public function supportsAbove($phpVersion) |
|
| 96 | |||
| 97 | View Code Duplication | public function supportsBelow($phpVersion) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
| 113 | * |
||
| 114 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 115 | * |
||
| 116 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
| 117 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
| 118 | * that, this method can be removed and call to it replaced with |
||
| 119 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
| 120 | * |
||
| 121 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 122 | * @param int $stackPtr The position of the class token. |
||
| 123 | * |
||
| 124 | * @return array|false |
||
| 125 | */ |
||
| 126 | public function findImplementedInterfaceNames($phpcsFile, $stackPtr) |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Checks if a function call has parameters. |
||
| 173 | * |
||
| 174 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 175 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 176 | * |
||
| 177 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
| 178 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
| 179 | * |
||
| 180 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 181 | * @param int $stackPtr The position of the function call token. |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Count the number of parameters a function call has been passed. |
||
| 222 | * |
||
| 223 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
| 224 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
| 225 | * |
||
| 226 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
| 227 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
| 228 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
| 229 | * |
||
| 230 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 231 | * @param int $stackPtr The position of the function call token. |
||
| 232 | * |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 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.