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 ForbiddenSwitchWithMultipleDefaultBlocksSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | const TEST_FILE = 'sniff-examples/forbidden_switch_with_multiple_default_blocks.php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * testForbiddenSwitchWithMultipleDefaultBlocks |
||
| 22 | * |
||
| 23 | * @group forbiddenSwitchMultipleDefault |
||
| 24 | * |
||
| 25 | * @dataProvider dataForbiddenSwitchWithMultipleDefaultBlocks |
||
| 26 | * |
||
| 27 | * @param int $line The line number. |
||
| 28 | * |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | public function testForbiddenSwitchWithMultipleDefaultBlocks($line) |
||
| 32 | { |
||
| 33 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 34 | $this->assertNoViolation($file, $line); |
||
| 35 | |||
| 36 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 37 | $this->assertError($file, $line, 'Switch statements can not have multiple default blocks since PHP 7.0'); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Data provider. |
||
| 42 | * |
||
| 43 | * @see testForbiddenSwitchWithMultipleDefaultBlocks() |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function dataForbiddenSwitchWithMultipleDefaultBlocks() |
||
| 48 | { |
||
| 49 | return array( |
||
| 50 | array(3), |
||
| 51 | array(47), |
||
| 52 | array(56), |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * testValidSwitchStatement |
||
| 59 | * |
||
| 60 | * @group forbiddenSwitchMultipleDefault |
||
| 61 | * |
||
| 62 | * @dataProvider dataValidSwitchStatement |
||
| 63 | * |
||
| 64 | * @param int $line The line number. |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function testValidSwitchStatement($line) |
||
| 69 | { |
||
| 70 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 71 | $this->assertNoViolation($file, $line); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Data provider. |
||
| 76 | * |
||
| 77 | * @see testValidSwitchStatement() |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function dataValidSwitchStatement() |
||
| 82 | { |
||
| 83 | return array( |
||
| 84 | array(14), |
||
| 85 | array(23), |
||
| 86 | array(43), |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 |