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 |
||
| 13 | class GistsTest extends AbstractTest |
||
| 14 | { |
||
| 15 | const PUBLIC_GIST = '76e253825bb3c6c084cf31f92997eb72'; |
||
| 16 | |||
| 17 | /** @var Gists */ |
||
| 18 | protected $gists; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * GistsTest constructor. |
||
| 22 | * |
||
| 23 | * @param null $name |
||
| 24 | * @param array $data |
||
| 25 | * @param string $dataName |
||
| 26 | */ |
||
| 27 | View Code Duplication | public function __construct($name = null, array $data = [], $dataName = '') |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Test list gists of current users |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function testListGists() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Test list public gists |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function testPublicListGists() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Test list user's starred gists |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function testListUsersStarredGists() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Test getting gist by ID |
||
| 91 | */ |
||
| 92 | public function testGetGistById() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Test creating a new gist |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function testCreateGist(): string |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Test updating an existing gist |
||
| 129 | * |
||
| 130 | * @depends testCreateGist |
||
| 131 | * |
||
| 132 | * @param string $gistId |
||
| 133 | */ |
||
| 134 | public function testUpdateGist(string $gistId) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Test list commits of a gist |
||
| 147 | * |
||
| 148 | * @depends testCreateGist |
||
| 149 | * |
||
| 150 | * @param string $gistId |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function testListGistsCommit(string $gistId) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Test starring a gist |
||
| 166 | * |
||
| 167 | * @depends testCreateGist |
||
| 168 | * |
||
| 169 | * @param string $gistId |
||
| 170 | */ |
||
| 171 | public function testStarGist(string $gistId) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Test gist is starred |
||
| 178 | * |
||
| 179 | * @depends testCreateGist |
||
| 180 | * |
||
| 181 | * @param string $gistId |
||
| 182 | */ |
||
| 183 | public function testGistIsStarred(string $gistId) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Test unstar a gist |
||
| 190 | * |
||
| 191 | * @depends testCreateGist |
||
| 192 | * |
||
| 193 | * @param string $gistId |
||
| 194 | */ |
||
| 195 | public function testUnStarGist(string $gistId) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Test fork a public gist |
||
| 202 | */ |
||
| 203 | public function testForkGist() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Test list forks of a specific gist |
||
| 214 | */ |
||
| 215 | public function testListGistForks() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Test deleting an existing gist |
||
| 226 | * |
||
| 227 | * @depends testCreateGist |
||
| 228 | * |
||
| 229 | * @param string $gistId |
||
| 230 | */ |
||
| 231 | public function testDeleteGist(string $gistId) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Test deleting a forked gist |
||
| 238 | * |
||
| 239 | * @depends testForkGist |
||
| 240 | * |
||
| 241 | * @param string $gistId |
||
| 242 | */ |
||
| 243 | public function testDeleteForkedGist(string $gistId) |
||
| 247 | } |
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.