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:
Complex classes like ContentRepositoryTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentRepositoryTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ContentRepositoryTest extends AbstractKernelTestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var ContentRepositoryInterface |
||
22 | */ |
||
23 | protected $repository; |
||
24 | protected $keywordRepository; |
||
25 | protected $userRepository; |
||
26 | |||
27 | protected $currentsiteManager; |
||
28 | |||
29 | /** |
||
30 | * Set up test |
||
31 | */ |
||
32 | protected function setUp() |
||
44 | |||
45 | /** |
||
46 | * @param string $name |
||
47 | * @param boolean $exists |
||
48 | * |
||
49 | * @dataProvider provideTestUniquenessInContext |
||
50 | */ |
||
51 | public function testTestUniquenessInContext($name, $exists) |
||
58 | |||
59 | /** |
||
60 | * @return array |
||
61 | */ |
||
62 | public function provideTestUniquenessInContext() |
||
69 | |||
70 | /** |
||
71 | * @param string $contentId |
||
72 | * |
||
73 | * @dataProvider provideFindOneByContentId |
||
74 | */ |
||
75 | public function testFindOneByContentId($contentId) |
||
81 | |||
82 | /** |
||
83 | * @return array |
||
84 | */ |
||
85 | public function provideFindOneByContentId() |
||
92 | |||
93 | /** |
||
94 | * @param $contentId |
||
95 | * @param $version |
||
96 | * @param string|null $language |
||
97 | * |
||
98 | * @dataProvider providefindLastPublishedVersion |
||
99 | */ |
||
100 | public function testFindLastPublishedVersion($contentId, $version, $language) |
||
106 | |||
107 | /** |
||
108 | * @param $contentId |
||
109 | * @param $version |
||
110 | * @param string|null $language |
||
111 | * |
||
112 | * @dataProvider providefindLastPublishedVersion |
||
113 | */ |
||
114 | public function testFindOneCurrentlyPublished($contentId, $version, $language) |
||
120 | |||
121 | /** |
||
122 | * @return array |
||
123 | */ |
||
124 | View Code Duplication | public function providefindLastPublishedVersion() |
|
131 | |||
132 | /** |
||
133 | * @param string $contentType |
||
134 | * @param string $choiceType |
||
135 | * @param string|null $keywords |
||
136 | * @param int $count |
||
137 | * |
||
138 | * @dataProvider provideContentTypeKeywordAndCount |
||
139 | */ |
||
140 | public function testFindByContentTypeAndCondition($contentType = '', $choiceType, $keywords = null, $count) |
||
149 | |||
150 | /** |
||
151 | * @return array |
||
152 | */ |
||
153 | public function provideContentTypeKeywordAndCount() |
||
190 | |||
191 | /** |
||
192 | * @param string $contentId |
||
193 | * @param string $language |
||
194 | * |
||
195 | * @dataProvider provideFindOneByContentIdAndLanguage |
||
196 | */ |
||
197 | public function testFindOneByLanguage($contentId, $language) |
||
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | public function provideFindOneByContentIdAndLanguage() |
||
214 | |||
215 | /** |
||
216 | * @param string $contentId |
||
217 | * @param string $language |
||
218 | * |
||
219 | * @dataProvider provideFindByContentIdAndLanguage |
||
220 | */ |
||
221 | public function testFindByLanguage($contentId, $language) |
||
230 | |||
231 | /** |
||
232 | * @return array |
||
233 | */ |
||
234 | public function provideFindByContentIdAndLanguage() |
||
241 | |||
242 | /** |
||
243 | * @param string $contentId |
||
244 | * @param string $language |
||
245 | * @param int $version |
||
246 | * |
||
247 | * @dataProvider provideFindOneByContentIdAndLanguageAndVersion |
||
248 | */ |
||
249 | public function testFindOneByLanguageAndVersion($contentId, $language, $version) |
||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | View Code Duplication | public function provideFindOneByContentIdAndLanguageAndVersion() |
|
267 | |||
268 | /** |
||
269 | * @param string $contentType |
||
270 | * @param array $descriptionEntity |
||
271 | * @param array $search |
||
272 | * @param string $siteId |
||
273 | * @param int $skip |
||
274 | * @param int $limit |
||
275 | * @param integer $count |
||
276 | * |
||
277 | * @dataProvider provideContentTypeAndPaginateAndSearchAndsiteId |
||
278 | */ |
||
279 | public function testFindPaginatedLastVersionByContentTypeAndsite($contentType, $descriptionEntity, $search, $order, $siteId, $skip, $limit, $count, $name = null) |
||
290 | |||
291 | /** |
||
292 | * @return array |
||
293 | */ |
||
294 | public function provideContentTypeAndPaginateAndSearchAndsiteId() |
||
317 | |||
318 | /** |
||
319 | * @param string $contentType |
||
320 | * @param integer $count |
||
321 | * |
||
322 | * @dataProvider provideContentTypeCount |
||
323 | * @deprecated will be removed in 2.0, use countByContentTypeAndSiteInLastVersion |
||
324 | */ |
||
325 | public function testCountByContentTypeInLastVersion($contentType, $count) |
||
331 | |||
332 | /** |
||
333 | * @return array |
||
334 | */ |
||
335 | public function provideContentTypeCount() |
||
343 | |||
344 | /** |
||
345 | * @param string $contentType |
||
346 | * @param string $siteId |
||
347 | * @param integer $count |
||
348 | * |
||
349 | * @dataProvider provideCountByContentTypeAndSiteInLastVersion |
||
350 | */ |
||
351 | public function testCountByContentTypeAndSiteInLastVersion($contentType, $siteId, $count) |
||
356 | |||
357 | /** |
||
358 | * @return array |
||
359 | */ |
||
360 | public function provideCountByContentTypeAndSiteInLastVersion() |
||
371 | |||
372 | /** |
||
373 | * @param string $contentType |
||
374 | * @param array $descriptionEntity |
||
375 | * @param string $search |
||
376 | * @param string $siteId |
||
377 | * @param int $count |
||
378 | * |
||
379 | * @dataProvider provideColumnsAndSearchAndCount |
||
380 | */ |
||
381 | public function testCountByContentTypeInLastVersionWithSearchFilter( |
||
392 | /** |
||
393 | * @return array |
||
394 | */ |
||
395 | public function provideColumnsAndSearchAndCount() |
||
411 | |||
412 | /** |
||
413 | * @param string $author |
||
414 | * @param string $siteId |
||
415 | * @param boolean|null $published |
||
416 | * @param int $limit |
||
417 | * @param array|null $sort |
||
418 | * @param int $count |
||
419 | * |
||
420 | * @dataProvider provideFindByAuthorAndsiteId |
||
421 | */ |
||
422 | public function testFindByAuthorAndsiteId($author, $siteId, $published, $limit, $sort, $count) |
||
427 | |||
428 | /** |
||
429 | * @return array |
||
430 | */ |
||
431 | public function provideFindByAuthorAndsiteId() |
||
444 | |||
445 | /** |
||
446 | * @param string $user |
||
447 | * @param string $siteId |
||
448 | * @param array $eventTypes |
||
449 | * @param boolean|null $published |
||
450 | * @param int $limit |
||
451 | * @param array|null $sort |
||
452 | * @param int $count |
||
453 | * |
||
454 | * @dataProvider provideFindByHistoryAndSiteId |
||
455 | */ |
||
456 | View Code Duplication | public function testFindByHistoryAndSiteId($user, $siteId, array $eventTypes, $published, $limit, $sort, $count) |
|
463 | |||
464 | /** |
||
465 | * @return array |
||
466 | */ |
||
467 | public function provideFindByHistoryAndSiteId() |
||
477 | |||
478 | /** |
||
479 | * Generate columns of content with search value |
||
480 | * |
||
481 | * @param array|null $searchColumns |
||
482 | * @param string $globalSearch |
||
483 | * |
||
484 | * @return array |
||
485 | */ |
||
486 | View Code Duplication | protected function generateColumnsProvider($searchColumns = null, $globalSearch = '') |
|
503 | |||
504 | /** |
||
505 | * Generate relation between columns names and entities attributes |
||
506 | * |
||
507 | * @return array |
||
508 | */ |
||
509 | protected function getDescriptionColumnEntity() |
||
588 | |||
589 | /** |
||
590 | * @param string $language |
||
591 | * @param int $version |
||
592 | * @param string $siteId |
||
593 | * @param \OpenOrchestra\ModelInterface\Model\ContentInterface $content |
||
594 | * @param string $contentId |
||
595 | */ |
||
596 | protected function assertSameContent($language, $version, $siteId, $contentId, $content) |
||
611 | |||
612 | /** |
||
613 | * Test has statused element |
||
614 | */ |
||
615 | View Code Duplication | public function testHasStatusedElement() |
|
622 | |||
623 | /** |
||
624 | * @param string $condition |
||
625 | * |
||
626 | * @return array |
||
627 | */ |
||
628 | View Code Duplication | protected function replaceKeywordLabelById($condition) |
|
646 | } |
||
647 |
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.