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 |
||
| 9 | class ManagerTest extends PHPUnit_Framework_TestCase |
||
| 10 | { |
||
| 11 | protected $sampleConfigData = [ |
||
| 12 | 'name' => 'Glenn', |
||
| 13 | 'age' => 18, |
||
| 14 | 'favourites' => [ |
||
| 15 | 'food' => 'pizza', |
||
| 16 | 'drink' => 'coke', |
||
| 17 | 'colour' => 'black', |
||
| 18 | ], |
||
| 19 | ]; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Test the @has method. |
||
| 23 | * |
||
| 24 | * @author Glenn McEwan <[email protected]> |
||
| 25 | */ |
||
| 26 | public function testHasMethod() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Test the has method works successfully for nested querying. |
||
| 35 | * Checking if sub-level nodes in the config exist using dot notation. |
||
| 36 | * |
||
| 37 | * @author Glenn McEwan <[email protected]> |
||
| 38 | */ |
||
| 39 | public function testNestedHasMethod() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Test the @get method. |
||
| 52 | * |
||
| 53 | * @author Glenn McEwan <[email protected]> |
||
| 54 | */ |
||
| 55 | public function testGetMethod() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Test the @set method. |
||
| 66 | * |
||
| 67 | * @author Glenn McEwan <[email protected]> |
||
| 68 | */ |
||
| 69 | public function testSetMethod() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Test the @get method works properly when passing a default. |
||
| 85 | * |
||
| 86 | * @author Glenn McEwan <[email protected]> |
||
| 87 | */ |
||
| 88 | public function testGetDefaultMethod() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Test the @all method. |
||
| 99 | * |
||
| 100 | * @author Glenn McEwan <[email protected]> |
||
| 101 | */ |
||
| 102 | public function testAllMethod() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Test the @set method sets a config entry, |
||
| 119 | * and is retrievable with @get. |
||
| 120 | * |
||
| 121 | * @get with a default parameter should still return null if the |
||
| 122 | * original @set call had a null value. |
||
| 123 | * |
||
| 124 | * @author Glenn McEwan <[email protected]> |
||
| 125 | */ |
||
| 126 | public function testSetNullMethod() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Test the set array method |
||
| 147 | * |
||
| 148 | * @author Glenn McEwan <[email protected]> |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function testSetArrayMethod() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Test the set array method with a key passed in as a second parameter. |
||
| 178 | * |
||
| 179 | * @author Glenn McEwan <[email protected]> |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function testSetArrayMethodWithKey() |
|
| 207 | } |
||
| 208 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.