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 | View Code Duplication | public static function setUpBeforeClass() |
|
| 30 | { |
||
| 31 | // When using PHPCS 1.x combined with PHP 5.3 or lower, traits are not recognized. |
||
| 32 | if (version_compare(PHP_CodeSniffer::VERSION, '2.0', '<') && version_compare(phpversion(), '5.4', '<')) { |
||
| 33 | self::$recognizesTraits = false; |
||
| 34 | } |
||
| 35 | } |
||
| 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 | * @param bool $isTrait Whether to load the class/interface test file or the trait test file. |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public function testCorrectImplementation($line, $isTrait = false) |
||
| 78 | { |
||
| 79 | if ($isTrait === true && self::$recognizesTraits === false) { |
||
| 80 | $this->markTestSkipped(); |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | $file = $this->getTestFile($isTrait); |
||
| 85 | $this->assertNoViolation($file, $line); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Data provider. |
||
| 90 | * |
||
| 91 | * @see testCorrectImplementation() |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function dataCorrectImplementation() |
||
| 96 | { |
||
| 97 | return array( |
||
| 98 | /* |
||
| 99 | * nonstatic_magic_methods.php |
||
| 100 | */ |
||
| 101 | // Plain class. |
||
| 102 | array(5), |
||
| 103 | array(6), |
||
| 104 | array(7), |
||
| 105 | array(8), |
||
| 106 | array(9), |
||
| 107 | array(10), |
||
| 108 | array(11), |
||
| 109 | array(12), |
||
| 110 | array(13), |
||
| 111 | // Normal class. |
||
| 112 | array(18), |
||
| 113 | array(19), |
||
| 114 | array(20), |
||
| 115 | array(21), |
||
| 116 | array(22), |
||
| 117 | array(23), |
||
| 118 | array(24), |
||
| 119 | array(25), |
||
| 120 | array(26), |
||
| 121 | array(27), |
||
| 122 | |||
| 123 | // Alternative property order & stacked. |
||
| 124 | array(58), |
||
| 125 | |||
| 126 | // Plain interface. |
||
| 127 | array(71), |
||
| 128 | array(72), |
||
| 129 | array(73), |
||
| 130 | array(74), |
||
| 131 | array(75), |
||
| 132 | array(76), |
||
| 133 | array(77), |
||
| 134 | array(78), |
||
| 135 | array(79), |
||
| 136 | // Normal interface. |
||
| 137 | array(84), |
||
| 138 | array(85), |
||
| 139 | array(86), |
||
| 140 | array(87), |
||
| 141 | array(88), |
||
| 142 | array(89), |
||
| 143 | array(90), |
||
| 144 | array(91), |
||
| 145 | array(92), |
||
| 146 | array(93), |
||
| 147 | |||
| 148 | /* |
||
| 149 | * nonstatic_magic_methods_traits.php |
||
| 150 | */ |
||
| 151 | // Plain trait. |
||
| 152 | array(5, true), |
||
| 153 | array(6, true), |
||
| 154 | array(7, true), |
||
| 155 | array(8, true), |
||
| 156 | array(9, true), |
||
| 157 | array(10, true), |
||
| 158 | array(11, true), |
||
| 159 | array(12, true), |
||
| 160 | array(13, true), |
||
| 161 | // Normal trait. |
||
| 162 | array(18, true), |
||
| 163 | array(19, true), |
||
| 164 | array(20, true), |
||
| 165 | array(21, true), |
||
| 166 | array(22, true), |
||
| 167 | array(23, true), |
||
| 168 | array(24, true), |
||
| 169 | array(25, true), |
||
| 170 | array(26, true), |
||
| 171 | array(27, true), |
||
| 172 | |||
| 173 | ); |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * testWrongMethodVisibility |
||
| 179 | * |
||
| 180 | * @group MagicMethods |
||
| 181 | * |
||
| 182 | * @dataProvider dataWrongMethodVisibility |
||
| 183 | * |
||
| 184 | * @param string $methodName Method name. |
||
| 185 | * @param string $desiredVisibility The visibility the method should have. |
||
| 186 | * @param string $testVisibility The visibility the method actually has in the test. |
||
| 187 | * @param int $line The line number. |
||
| 188 | * @param bool $isTrait Whether the test relates to method in a trait. |
||
| 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.