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 NewFunctionArrayDereferencingSniffTest extends BaseSniffTest |
||
|
|
|||
| 17 | { |
||
| 18 | const TEST_FILE = 'sniff-examples/new_function_array_dereferencing.php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * testArrayDereferencing |
||
| 22 | * |
||
| 23 | * @group functionArrayDereferencing |
||
| 24 | * |
||
| 25 | * @dataProvider dataArrayDereferencing |
||
| 26 | * |
||
| 27 | * @param int $line Line number with valid code. |
||
| 28 | * |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | View Code Duplication | public function testArrayDereferencing($line) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * dataArrayDereferencing |
||
| 42 | * |
||
| 43 | * @see testArrayDereferencing() |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function dataArrayDereferencing() { |
|
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * testNoViolation |
||
| 59 | * |
||
| 60 | * @group functionArrayDereferencing |
||
| 61 | * |
||
| 62 | * @dataProvider dataNoViolation |
||
| 63 | * |
||
| 64 | * @param int $line Line number with valid code. |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function testNoViolation($line) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * dataNoViolation |
||
| 76 | * |
||
| 77 | * @see testNoViolation() |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function dataNoViolation() { |
|
| 91 | } |
||
| 92 |
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.