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 UserRepositoryTest extends AbstractKernelTestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var UserRepository |
||
20 | */ |
||
21 | protected $repository; |
||
22 | |||
23 | /** |
||
24 | * Set up test |
||
25 | */ |
||
26 | View Code Duplication | protected function setUp() |
|
34 | |||
35 | /** |
||
36 | * @param PaginateFinderConfiguration $configuration |
||
37 | * @param int $count |
||
38 | * |
||
39 | * @dataProvider providePaginateAndSearch |
||
40 | */ |
||
41 | public function testFindForPaginate(PaginateFinderConfiguration $configuration, $count) |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | View Code Duplication | public function providePaginateAndSearch() |
|
64 | |||
65 | /** |
||
66 | * @param PaginateFinderConfiguration $configuration |
||
67 | * @param array $sitesId |
||
68 | * @param int $count |
||
69 | * |
||
70 | * @dataProvider providePaginateAndSearchWithSitesId |
||
71 | */ |
||
72 | public function testPaginateAndSearchWithSitesId(PaginateFinderConfiguration $configuration, array $sitesId, $count) |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | View Code Duplication | public function providePaginateAndSearchWithSitesId() |
|
97 | |||
98 | /** |
||
99 | * test count all user |
||
100 | */ |
||
101 | public function testCount() |
||
106 | |||
107 | /** |
||
108 | * test count all filter with site id |
||
109 | */ |
||
110 | public function testCountFilterBySiteId() |
||
116 | |||
117 | /** |
||
118 | * @param PaginateFinderConfiguration $configuration |
||
119 | * @param int $count |
||
120 | * |
||
121 | * @dataProvider provideCountWithFilter |
||
122 | */ |
||
123 | public function testCountWithFilter($configuration, $count) |
||
128 | |||
129 | /** |
||
130 | * @return array |
||
131 | */ |
||
132 | View Code Duplication | public function provideCountWithFilter() |
|
146 | |||
147 | /** |
||
148 | * @param PaginateFinderConfiguration $configuration |
||
149 | * @param array $sitesId |
||
150 | * @param int $count |
||
151 | * |
||
152 | * @dataProvider provideCountWithFilterAndSitesId |
||
153 | */ |
||
154 | public function testCountWithFilterAndSitesId($configuration, array $sitesId, $count) |
||
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | View Code Duplication | public function provideCountWithFilterAndSitesId() |
|
179 | |||
180 | /** |
||
181 | * @param $username |
||
182 | * @param $groupName |
||
183 | * @param $countUser |
||
184 | * |
||
185 | * @dataProvider provideUserAndGroup |
||
186 | */ |
||
187 | public function testFindByIncludedUsernameWithoutGroup($username, $groupName, $countUser) |
||
194 | |||
195 | public function provideUserAndGroup() |
||
204 | |||
205 | /** |
||
206 | * Test remove users |
||
207 | */ |
||
208 | View Code Duplication | public function testRemoveUsers() |
|
209 | { |
||
210 | $dm = static::$kernel->getContainer()->get('object_manager'); |
||
211 | $userDemo = $this->repository->findOneByUsername('demo'); |
||
212 | $userSAdmin = $this->repository->findOneByUsername('s-admin'); |
||
213 | |||
214 | $userIds = array($userDemo->geTId(), $userSAdmin->getId()); |
||
215 | |||
216 | $this->repository->removeUsers($userIds); |
||
217 | $this->assertNull($this->repository->findOneByUsername('demo')); |
||
218 | $this->assertNull($this->repository->findOneByUsername('s-admin')); |
||
219 | |||
220 | $dm->persist(clone $userDemo); |
||
221 | $dm->persist(clone $userSAdmin); |
||
222 | $dm->flush(); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Test remove users |
||
227 | */ |
||
228 | public function testGetCountsUsersByGroups() |
||
238 | |||
239 | /** |
||
240 | * @param array $sitesId |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | protected function convertSiteIdInMongoId(array $sitesId) |
||
254 | |||
255 | /** |
||
256 | * @param string $email |
||
257 | * |
||
258 | * @dataProvider provideEmail |
||
259 | */ |
||
260 | public function testFindOneByEmail($email) |
||
266 | |||
267 | /** |
||
268 | * @return array |
||
269 | */ |
||
270 | public function provideEmail() |
||
278 | |||
279 | /** |
||
280 | * test findUsersByGroups |
||
281 | */ |
||
282 | public function testFindUsersByGroups() |
||
288 | |||
289 | /** |
||
290 | * test removeGroupFromNotListedUsers |
||
291 | */ |
||
292 | public function testRemoveGroupFromNotListedUsers() |
||
319 | } |
||
320 |
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.