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 ForbiddenCallTimePassByReferenceSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | const TEST_FILE = 'sniff-examples/call_time_pass_by_reference.php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Sniffed file |
||
| 22 | * |
||
| 23 | * @var PHP_CodeSniffer_File |
||
| 24 | */ |
||
| 25 | protected $_sniffFile; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * setUp |
||
| 29 | * |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function setUp() |
||
| 33 | { |
||
| 34 | parent::setUp(); |
||
| 35 | |||
| 36 | $this->_sniffFile = $this->sniffFile(self::TEST_FILE); |
||
| 37 | } |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * testForbiddenCallTimePassByReference |
||
| 42 | * |
||
| 43 | * @group forbiddenCallTimePassByReference |
||
| 44 | * |
||
| 45 | * @dataProvider dataForbiddenCallTimePassByReference |
||
| 46 | * |
||
| 47 | * @param int $line Line number where the error should occur. |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public function testForbiddenCallTimePassByReference($line) |
||
| 52 | { |
||
| 53 | $file = $this->sniffFile(self::TEST_FILE, '5.2'); |
||
| 54 | $this->assertNoViolation($file, $line); |
||
| 55 | |||
| 56 | $file = $this->sniffFile(self::TEST_FILE, '5.3'); |
||
| 57 | $this->assertError($file, $line, 'Using a call-time pass-by-reference is deprecated since PHP 5.3'); |
||
| 58 | |||
| 59 | $file = $this->sniffFile(self::TEST_FILE, '5.4'); |
||
| 60 | $this->assertError($file, $line, 'Using a call-time pass-by-reference is deprecated since PHP 5.3 and prohibited since PHP 5.4'); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * dataForbiddenCallTimePassByReference |
||
| 65 | * |
||
| 66 | * @see testForbiddenCallTimePassByReference() |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function dataForbiddenCallTimePassByReference() { |
||
| 71 | return array( |
||
| 72 | array(10), // Bad: call time pass by reference. |
||
| 73 | array(14), // Bad: call time pass by reference with multi-parameter call. |
||
| 74 | array(17), // Bad: nested call time pass by reference. |
||
| 75 | array(25), // Bad: call time pass by reference with space. |
||
| 76 | array(44), // Bad: call time pass by reference. |
||
| 77 | array(45), // Bad: call time pass by reference. |
||
| 78 | array(49), // Bad: multiple call time pass by reference. |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * testNoViolation |
||
| 85 | * |
||
| 86 | * @group forbiddenCallTimePassByReference |
||
| 87 | * |
||
| 88 | * @dataProvider dataNoViolation |
||
| 89 | * |
||
| 90 | * @param int $line The line number. |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function testNoViolation($line) |
||
| 95 | { |
||
| 96 | $this->assertNoViolation($this->_sniffFile, $line); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Data provider. |
||
| 101 | * |
||
| 102 | * @see testNoViolation() |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function dataNoViolation() |
||
| 107 | { |
||
| 108 | return array( |
||
| 109 | array(4), // OK: Declaring a parameter by reference. |
||
| 110 | array(9), // OK: Call time passing without reference. |
||
| 111 | |||
| 112 | // OK: Bitwise operations as parameter. |
||
| 113 | array(20), |
||
| 114 | array(21), |
||
| 115 | array(22), |
||
| 116 | array(23), |
||
| 117 | array(24), |
||
| 118 | array(39), |
||
| 119 | array(40), |
||
| 120 | //array(41), // Currently not yet covered. |
||
| 121 | |||
| 122 | array(51), // OK: No variables. |
||
| 123 | array(53), // OK: Outside scope of this sniff. |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | } |
||
| 128 | |||
| 129 |