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 ValidIntegersSniffTest extends BaseSniffTest |
||
|
|
|||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/valid_integers.php'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Sniffed file |
||
| 23 | * |
||
| 24 | * @var PHP_CodeSniffer_File |
||
| 25 | */ |
||
| 26 | protected $_sniffFile; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * setUp |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public function setUp() |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * testBinaryInteger |
||
| 43 | * |
||
| 44 | * @group ValidIntegers |
||
| 45 | * |
||
| 46 | * @dataProvider dataBinaryInteger |
||
| 47 | * |
||
| 48 | * @param int $line Line number where the error should occur. |
||
| 49 | * @param string $octal (Start of) Binary number as a string. |
||
| 50 | * @param bool $testNoViolation Whether or not to test for noViolation. |
||
| 51 | * Defaults to true. Set to false if another error is |
||
| 52 | * expected on the same line (invalid binary) |
||
| 53 | * |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function testBinaryInteger($line, $binary, $testNoViolation = true) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * dataBinaryInteger |
||
| 71 | * |
||
| 72 | * @see testBinaryInteger() |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function dataBinaryInteger() { |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * testInvalidBinaryInteger |
||
| 86 | * |
||
| 87 | * @group ValidIntegers |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function testInvalidBinaryInteger() |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * testInvalidOctalInteger |
||
| 99 | * |
||
| 100 | * @group ValidIntegers |
||
| 101 | * |
||
| 102 | * @dataProvider dataInvalidOctalInteger |
||
| 103 | * |
||
| 104 | * @param int $line Line number where the error should occur. |
||
| 105 | * @param string $octal Octal number as a string. |
||
| 106 | * |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | public function testInvalidOctalInteger($line, $octal) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * dataInvalidOctalInteger |
||
| 122 | * |
||
| 123 | * @see testInvalidOctalInteger() |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function dataInvalidOctalInteger() { |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * testValidOctalInteger |
||
| 138 | * |
||
| 139 | * @group ValidIntegers |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function testValidOctalInteger() { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * testHexNumericString |
||
| 150 | * |
||
| 151 | * @group ValidIntegers |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function testHexNumericString() |
|
| 167 | |||
| 168 | } |
||
| 169 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.