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 DeprecatedNewReferenceSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | const TEST_FILE = 'sniff-examples/deprecated_new_reference.php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * testDeprecatedNewReference |
||
| 22 | * |
||
| 23 | * @dataProvider dataDeprecatedNewReference |
||
| 24 | * |
||
| 25 | * @param int $line The line number. |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | public function testDeprecatedNewReference($line) |
||
| 30 | { |
||
| 31 | $file = $this->sniffFile(self::TEST_FILE, '5.2'); |
||
| 32 | $this->assertNoViolation($file, $line); |
||
| 33 | |||
| 34 | $file = $this->sniffFile(self::TEST_FILE, '5.3'); |
||
| 35 | $this->assertWarning($file, $line, 'Assigning the return value of new by reference is deprecated in PHP 5.3'); |
||
| 36 | |||
| 37 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 38 | $this->assertError($file, $line, 'Assigning the return value of new by reference is deprecated in PHP 5.3 and forbidden in PHP 7.0'); |
||
| 39 | |||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Data provider. |
||
| 44 | * |
||
| 45 | * @see testDeprecatedNewReference() |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function dataDeprecatedNewReference() |
||
| 50 | { |
||
| 51 | return array( |
||
| 52 | array(9), |
||
| 53 | array(10), |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * testNoReference |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | public function testNoReference() |
||
| 63 | { |
||
| 64 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 65 | $this->assertNoViolation($file, 8); |
||
| 66 | } |
||
| 67 | |||
| 68 | } |
||
| 69 |