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 |
||
| 20 | class ForbiddenBreakContinueVariableArgumentsSniffTest extends BaseSniffTest |
||
| 21 | { |
||
| 22 | const TEST_FILE = 'sniff-examples/forbidden_break_continue_variable_argument.php'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Sniffed file |
||
| 26 | * |
||
| 27 | * @var PHP_CodeSniffer_File |
||
| 28 | */ |
||
| 29 | protected $_sniffFile; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * setUp |
||
| 33 | * |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public function setUp() |
||
| 37 | { |
||
| 38 | parent::setUp(); |
||
| 39 | |||
| 40 | $this->_sniffFile = $this->sniffFile(self::TEST_FILE); |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * testAllowedBreakAndContinueVariableArgument |
||
| 46 | * |
||
| 47 | * In PHP 5.3, none of the statements should give an error. |
||
| 48 | * |
||
| 49 | * @group forbiddenBreakContinue |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | public function testAllowedBreakAndContinueVariableArgument() |
||
| 54 | { |
||
| 55 | $file = $this->sniffFile(self::TEST_FILE, '5.3'); |
||
| 56 | $this->assertNoViolation($file); |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * testBreakAndContinueVariableArgument |
||
| 62 | * |
||
| 63 | * @group forbiddenBreakContinue |
||
| 64 | * |
||
| 65 | * @dataProvider dataBreakAndContinueVariableArgument |
||
| 66 | * |
||
| 67 | * @param int $line The line number. |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function testBreakAndContinueVariableArgument($line) |
||
| 72 | { |
||
| 73 | $this->assertError($this->_sniffFile, $line, 'Using a variable argument on break or continue is forbidden since PHP 5.4'); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Data provider. |
||
| 78 | * |
||
| 79 | * @see testBreakAndContinueVariableArgument() |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function dataBreakAndContinueVariableArgument() |
||
| 84 | { |
||
| 85 | return array( |
||
| 86 | array(53), |
||
| 87 | array(57), |
||
| 88 | array(62), |
||
| 89 | array(66), |
||
| 90 | array(71), |
||
| 91 | array(75), |
||
| 92 | array(80), |
||
| 93 | array(84), |
||
| 94 | array(89), |
||
| 95 | array(93), |
||
| 96 | array(98), |
||
| 97 | array(102), |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * testNoViolation |
||
| 104 | * |
||
| 105 | * @group forbiddenBreakContinue |
||
| 106 | * |
||
| 107 | * @dataProvider dataNoViolation |
||
| 108 | * |
||
| 109 | * @param int $line The line number. |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function testNoViolation($line) |
||
| 114 | { |
||
| 115 | $this->assertNoViolation($this->_sniffFile, $line); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Data provider. |
||
| 120 | * |
||
| 121 | * @see testNoViolation() |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public function dataNoViolation() |
||
| 126 | { |
||
| 127 | return array( |
||
| 128 | array(8), |
||
| 129 | array(12), |
||
| 130 | array(17), |
||
| 131 | array(21), |
||
| 132 | array(26), |
||
| 133 | array(30), |
||
| 134 | array(35), |
||
| 135 | array(39), |
||
| 136 | array(44), |
||
| 137 | array(48), |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 |