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 |
||
| 16 | class NonStaticMagicMethodsSniffTest extends BaseSniffTest |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Whether or not traits will be recognized in PHPCS. |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected static $recognizesTraits = true; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Set up skip condition. |
||
| 28 | */ |
||
| 29 | public static function setUpBeforeClass() |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Get the correct test file. |
||
| 40 | * |
||
| 41 | * (@internal |
||
| 42 | * The test file has been split into two: |
||
| 43 | * - one covering classes and interfaces |
||
| 44 | * - one covering traits |
||
| 45 | * |
||
| 46 | * This is to avoid test failing because PHPCS 1.x gets confused about the scope |
||
| 47 | * openers/closers when run on PHP 5.3 or lower. |
||
| 48 | * In a 'normal' situation you won't often find classes, interfaces and traits all |
||
| 49 | * mixed in one file anyway, so this issue for which this is a work-around, |
||
| 50 | * should not cause real world issues anyway.}} |
||
| 51 | * |
||
| 52 | * @param bool $isTrait Whether to load the class/interface test file or the trait test file. |
||
| 53 | * |
||
| 54 | * @return PHP_CodeSniffer_File File object|false |
||
| 55 | */ |
||
| 56 | protected function getTestFile($isTrait) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * testCorrectImplementation |
||
| 67 | * |
||
| 68 | * @group MagicMethods |
||
| 69 | * |
||
| 70 | * @dataProvider dataCorrectImplementation |
||
| 71 | * |
||
| 72 | * @param int $line The line number. |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function testCorrectImplementation($line, $isTrait = false) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Data provider. |
||
| 89 | * |
||
| 90 | * @see testCorrectImplementation() |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function dataCorrectImplementation() |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * testWrongMethodVisibility |
||
| 178 | * |
||
| 179 | * @group MagicMethods |
||
| 180 | * |
||
| 181 | * @dataProvider dataWrongMethodVisibility |
||
| 182 | * |
||
| 183 | * @param string $methodName Method name. |
||
| 184 | * @param string $desiredVisibility The visibility the method should have. |
||
| 185 | * @param string $testVisibility The visibility the method actually has in the test. |
||
| 186 | * @param int $line The line number. |
||
| 187 | * @param bool $isTrait Whether the test relates to method in a trait. |
||
| 188 | * |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function testWrongMethodVisibility($methodName, $desiredVisibility, $testVisibility, $line, $isTrait = false) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Data provider. |
||
| 205 | * |
||
| 206 | * @see testWrongMethodVisibility() |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function dataWrongMethodVisibility() |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * testWrongStaticMethod |
||
| 260 | * |
||
| 261 | * @group MagicMethods |
||
| 262 | * |
||
| 263 | * @dataProvider dataWrongStaticMethod |
||
| 264 | * |
||
| 265 | * @param string $methodName Method name. |
||
| 266 | * @param int $line The line number. |
||
| 267 | * @param bool $isTrait Whether the test relates to a method in a trait. |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function testWrongStaticMethod($methodName, $line, $isTrait = false) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Data provider. |
||
| 284 | * |
||
| 285 | * @see testWrongStaticMethod() |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function dataWrongStaticMethod() |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * testWrongNonStaticMethod |
||
| 331 | * |
||
| 332 | * @group MagicMethods |
||
| 333 | * |
||
| 334 | * @dataProvider dataWrongNonStaticMethod |
||
| 335 | * |
||
| 336 | * @param string $methodName Method name. |
||
| 337 | * @param int $line The line number. |
||
| 338 | * @param bool $isTrait Whether the test relates to a method in a trait. |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function testWrongNonStaticMethod($methodName, $line, $isTrait = false) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Data provider. |
||
| 355 | * |
||
| 356 | * @see testWrongNonStaticMethod() |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function dataWrongNonStaticMethod() |
||
| 383 | |||
| 384 | } |
||
| 385 |
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.