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 NewGroupUseDeclarationsSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/new_group_use_declarations.php'; |
||
| 20 | |||
| 21 | protected function setUp() |
||
| 22 | { |
||
| 23 | if (version_compare(PHP_CodeSniffer::VERSION, '2.3.4', '<')) { |
||
| 24 | $this->markTestSkipped(); |
||
| 25 | } |
||
| 26 | else { |
||
| 27 | parent::setUp(); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * testGroupUseDeclaration |
||
| 33 | * |
||
| 34 | * @requires PHP 5.3 |
||
| 35 | * |
||
| 36 | * @dataProvider dataGroupUseDeclaration |
||
| 37 | * |
||
| 38 | * @param int $line The line number. |
||
| 39 | * |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | public function testGroupUseDeclaration($line) |
||
| 43 | { |
||
| 44 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 45 | $this->assertError($file, $line, 'Group use declarations are not allowed in PHP 5.6 or earlier'); |
||
| 46 | |||
| 47 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 48 | $this->assertNoViolation($file, $line); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Data provider. |
||
| 53 | * |
||
| 54 | * @see testGroupUseDeclaration() |
||
| 55 | * |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | public function dataGroupUseDeclaration() |
||
| 59 | { |
||
| 60 | return array( |
||
| 61 | array(13), |
||
| 62 | array(14), |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * testValidUseDeclaration |
||
| 69 | * |
||
| 70 | * @dataProvider dataValidUseDeclaration |
||
| 71 | * |
||
| 72 | * @param int $line The line number. |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function testValidUseDeclaration($line) |
||
| 77 | { |
||
| 78 | $file = $this->sniffFile(self::TEST_FILE); |
||
| 79 | $this->assertNoViolation($file, $line); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Data provider. |
||
| 84 | * |
||
| 85 | * @see testValidUseDeclaration() |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function dataValidUseDeclaration() |
||
| 90 | { |
||
| 91 | return array( |
||
| 92 | array(4), |
||
| 93 | array(5), |
||
| 94 | array(6), |
||
| 95 | array(8), |
||
| 96 | array(9), |
||
| 97 | array(10), |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 |