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 NewLanguageConstructsSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | const TEST_FILE = 'sniff-examples/new_language_constructs.php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * testNamespaceSeparator |
||
| 22 | * |
||
| 23 | * @group NewLanguageConstructs |
||
| 24 | * |
||
| 25 | * @requires PHP 5.3 |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | public function testNamespaceSeparator() |
||
| 30 | { |
||
| 31 | $file = $this->sniffFile(self::TEST_FILE, '5.2'); |
||
| 32 | $this->assertError($file, 3, 'the \ operator (for namespaces) is not present in PHP version 5.2 or earlier'); |
||
| 33 | |||
| 34 | $file = $this->sniffFile(self::TEST_FILE, '5.3'); |
||
| 35 | $this->assertNoViolation($file, 3); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * testPow |
||
| 40 | * |
||
| 41 | * @group NewLanguageConstructs |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function testPow() |
||
| 46 | { |
||
| 47 | $file = $this->sniffFile(self::TEST_FILE, '5.5'); |
||
| 48 | $this->assertError($file, 5, "power operator (**) is not present in PHP version 5.5 or earlier"); |
||
| 49 | |||
| 50 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 51 | $this->assertNoViolation($file, 5); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * testPowEquals |
||
| 56 | * |
||
| 57 | * @group NewLanguageConstructs |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function testPowEquals() |
||
| 62 | { |
||
| 63 | $file = $this->sniffFile(self::TEST_FILE, '5.5'); |
||
| 64 | $this->assertError($file, 6, "power assignment operator (**=) is not present in PHP version 5.5 or earlier"); |
||
| 65 | |||
| 66 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 67 | $this->assertNoViolation($file, 6); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * testSpaceship |
||
| 72 | * |
||
| 73 | * @group NewLanguageConstructs |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public function testSpaceship() |
||
| 78 | { |
||
| 79 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 80 | $this->assertError($file, 12, "spaceship operator (<=>) is not present in PHP version 5.6 or earlier"); |
||
| 81 | |||
| 82 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 83 | $this->assertNoViolation($file, 12); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Coalescing operator |
||
| 88 | * |
||
| 89 | * @group NewLanguageConstructs |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function testCoalescing() |
||
| 94 | { |
||
| 95 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 96 | $this->assertError($file, 8, "null coalescing operator (??) is not present in PHP version 5.6 or earlier"); |
||
| 97 | $this->assertError($file, 10, "null coalescing operator (??) is not present in PHP version 5.6 or earlier"); |
||
| 98 | |||
| 99 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 100 | $this->assertNoViolation($file, 8); |
||
| 101 | $this->assertNoViolation($file, 10); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Variadic functions using ... |
||
| 106 | * |
||
| 107 | * @group NewLanguageConstructs |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function testEllipsis() |
||
| 112 | { |
||
| 113 | $file = $this->sniffFile(self::TEST_FILE, '5.5'); |
||
| 114 | $this->assertError($file, 14, "variadic functions using ... is not present in PHP version 5.5 or earlier"); |
||
| 115 | |||
| 116 | $file = $this->sniffFile(self::TEST_FILE, '5.6'); |
||
| 117 | $this->assertNoViolation($file, 14); |
||
| 118 | } |
||
| 119 | } |
||
| 120 |