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 NewFunctionArrayDereferencingSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | const TEST_FILE = 'sniff-examples/new_function_array_dereferencing.php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * testArrayDereferencing |
||
| 22 | * |
||
| 23 | * @group functionArrayDereferencing |
||
| 24 | * |
||
| 25 | * @return void |
||
| 26 | */ |
||
| 27 | public function testArrayDereferencing() |
||
| 28 | { |
||
| 29 | $file = $this->sniffFile(self::TEST_FILE, '5.3'); |
||
| 30 | $this->assertError($file, 3, 'Function array dereferencing is not present in PHP version 5.3 or earlier'); |
||
| 31 | |||
| 32 | $file = $this->sniffFile(self::TEST_FILE, '5.4'); |
||
| 33 | $this->assertNoViolation($file, 3); |
||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * testNoViolation |
||
| 39 | * |
||
| 40 | * @group functionArrayDereferencing |
||
| 41 | * |
||
| 42 | * @dataProvider dataNoViolation |
||
| 43 | * |
||
| 44 | * @param int $line Line number with valid code. |
||
| 45 | * |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function testNoViolation($line) |
||
| 49 | { |
||
| 50 | $file = $this->sniffFile(self::TEST_FILE, '5.2'); |
||
| 51 | $this->assertNoViolation($file, $line); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * dataNoViolation |
||
| 56 | * |
||
| 57 | * @see testNoViolation() |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function dataNoViolation() { |
||
| 62 | return array( |
||
| 63 | array(5), |
||
| 64 | array(8), |
||
| 65 | array(9), |
||
| 66 | array(10), |
||
| 67 | array(11), |
||
| 68 | array(14), |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | } |
||
| 72 |