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 TernaryOperatorsSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Sniffed file |
||
| 20 | * |
||
| 21 | * @var PHP_CodeSniffer_File |
||
| 22 | */ |
||
| 23 | protected $_sniffFile; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Test ternary operators that are acceptable in all PHP versions. |
||
| 27 | * |
||
| 28 | * @group ternaryOperators |
||
| 29 | * |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function testStandardTernaryOperators() |
||
| 33 | { |
||
| 34 | $this->_sniffFile = $this->sniffFile('sniff-examples/ternary_operator.php'); |
||
| 35 | $this->assertNoViolation($this->_sniffFile, 5); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * 5.2 doesn't support elvis operator. |
||
| 40 | * |
||
| 41 | * @group ternaryOperators |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function testMissingMiddleExpression5dot2() |
||
| 46 | { |
||
| 47 | $this->_sniffFile = $this->sniffFile('sniff-examples/ternary_operator.php', '5.2-5.4'); |
||
| 48 | $this->assertWarning($this->_sniffFile, 8, |
||
| 49 | "Middle may not be omitted from ternary operators in PHP < 5.3"); |
||
| 50 | $this->assertWarning($this->_sniffFile, 10, |
||
| 51 | "Middle may not be omitted from ternary operators in PHP < 5.3"); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * 5.3 does support elvis operator. |
||
| 56 | * |
||
| 57 | * @group ternaryOperators |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function testMissingMiddleExpression5dot3() |
||
| 62 | { |
||
| 63 | $this->_sniffFile = $this->sniffFile('sniff-examples/ternary_operator.php', '5.3'); |
||
| 64 | $this->assertNoViolation($this->_sniffFile, 8, |
||
| 65 | "Middle may not be omitted from ternary operators in PHP < 5.3"); |
||
| 66 | $this->assertNoViolation($this->_sniffFile, 10, |
||
| 67 | "Middle may not be omitted from ternary operators in PHP < 5.3"); |
||
| 68 | } |
||
| 69 | |||
| 70 | } |
||
| 71 |