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 WebTestCase extends BaseWebTestCase |
||
10 | { |
||
11 | protected $faker; |
||
12 | |||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $fieldsList; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $fieldsDetails = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $links = [ |
||
27 | 'self', 'create', 'update', 'patch', 'remove', 'list', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $itemValues; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $itemsBadValues; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $route; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $username = null; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $password = null; |
||
54 | |||
55 | 13 | protected function setUp() |
|
59 | |||
60 | /** |
||
61 | * @param string $route |
||
62 | * @param int $expectedItems |
||
63 | */ |
||
64 | 1 | View Code Duplication | protected function checkGetAll(string $route, int $expectedItems) |
76 | |||
77 | /** |
||
78 | * @param string $route |
||
79 | */ |
||
80 | 1 | protected function checkGetAnElement(string $route) |
|
91 | |||
92 | /** |
||
93 | * @param string $route |
||
94 | */ |
||
95 | 1 | View Code Duplication | protected function checkGetAUnexistingElement(string $route) |
105 | |||
106 | /** |
||
107 | * @param string $route |
||
108 | * @param string $method |
||
109 | */ |
||
110 | 2 | protected function checkUpdateOrPatchAUnexistingElement(string $route, string $method) |
|
122 | |||
123 | /** |
||
124 | * @param string $route |
||
125 | * @param bool $mustHaveErrors |
||
126 | * @param string $method |
||
127 | */ |
||
128 | 6 | protected function checkAddOrUpdateOrPatchAnElement(string $route, bool $mustHaveErrors = false, string $method = 'POST') |
|
158 | |||
159 | /** |
||
160 | * @param Client $client |
||
161 | * @param int $statusCode |
||
162 | */ |
||
163 | 11 | protected function checkStatusCodeAndContentType(Client $client, int $statusCode) |
|
172 | |||
173 | /** |
||
174 | * @param string $route |
||
175 | */ |
||
176 | 2 | protected function checkDeleteAnElement(string $route) |
|
183 | |||
184 | /** |
||
185 | * @param string $method |
||
186 | * @param string $route |
||
187 | * |
||
188 | * @return Client |
||
189 | */ |
||
190 | 13 | protected function getClient($withAuthentification = true) |
|
201 | |||
202 | /** |
||
203 | * @param array $item |
||
204 | */ |
||
205 | 5 | protected function checkIfItemHasFields(array $item) |
|
211 | |||
212 | /** |
||
213 | * @param array $item |
||
214 | */ |
||
215 | 5 | protected function checkIfItemHasLinks(array $item) |
|
222 | |||
223 | /** |
||
224 | * @param array $item |
||
225 | */ |
||
226 | 5 | protected function checkIfItemHasTheRightFieldsNumber(array $item, $isDetails = false) |
|
234 | |||
235 | /** |
||
236 | * @param \stdClass $list |
||
237 | */ |
||
238 | 1 | protected function checkIfListHaveRightStructure(\stdClass $list) |
|
252 | |||
253 | /** |
||
254 | * @param \stdClass $items |
||
255 | * @param int $expectedSize |
||
256 | */ |
||
257 | 1 | protected function checkItemList(\stdClass $items, int $expectedSize) |
|
267 | |||
268 | /** |
||
269 | * @param string $username |
||
270 | * @param string $password |
||
271 | * |
||
272 | * @return string|bool |
||
273 | */ |
||
274 | 13 | protected function authorize($username = 'admin', $password = 'admin') |
|
298 | } |
||
299 |
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.