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 NodeRepositoryTest 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 NodeRepositoryTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class NodeRepositoryTest extends AbstractKernelTestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var NodeRepository |
||
19 | */ |
||
20 | protected $repository; |
||
21 | |||
22 | /** |
||
23 | * Set up test |
||
24 | */ |
||
25 | protected function setUp() |
||
32 | |||
33 | /** |
||
34 | * @param string $language |
||
35 | * @param int $version |
||
36 | * @param string $siteId |
||
37 | * |
||
38 | * @dataProvider provideLanguageLastVersionAndSiteId |
||
39 | */ |
||
40 | public function testFindOneCurrentlyPublished($language, $version, $siteId) |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | View Code Duplication | public function provideLanguageLastVersionAndSiteId() |
|
57 | |||
58 | /** |
||
59 | * @param $language |
||
60 | * @param $version |
||
61 | * @param $siteId |
||
62 | * |
||
63 | * @dataProvider provideLanguageLastVersionAndSiteId |
||
64 | */ |
||
65 | public function testFindOneByNodeIdAndLanguageAndVersionAndSiteIdWithPublishedDataSet($language, $version, $siteId) |
||
71 | |||
72 | /** |
||
73 | * @param string $language |
||
74 | * @param int $version |
||
75 | * @param string $siteId |
||
76 | * @param int $versionExpected |
||
77 | * |
||
78 | * @dataProvider provideLanguageLastVersionAndSiteIdNotPublished |
||
79 | */ |
||
80 | public function testFindOneByNodeIdAndLanguageAndVersionAndSiteIdWithNotPublishedDataSet($language, $version = null, $siteId, $versionExpected) |
||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | */ |
||
91 | View Code Duplication | public function provideLanguageLastVersionAndSiteIdNotPublished() |
|
98 | |||
99 | /** |
||
100 | * @param string $language |
||
101 | * @param int $version |
||
102 | * @param string $siteId |
||
103 | * @param int $versionExpected |
||
104 | * |
||
105 | * @dataProvider provideLanguageLastVersionAndSiteIdNotPublished |
||
106 | */ |
||
107 | public function testFindInLastVersion($language, $version = null, $siteId, $versionExpected) |
||
113 | |||
114 | /** |
||
115 | * @param int $countVersions |
||
116 | * @param string $language |
||
117 | * @param string $siteId |
||
118 | * |
||
119 | * @dataProvider provideLanguageAndVersionListAndSiteId |
||
120 | */ |
||
121 | public function testFindByNodeAndLanguageAndSite($countVersions, $language, $siteId) |
||
135 | |||
136 | /** |
||
137 | * @return array |
||
138 | */ |
||
139 | View Code Duplication | public function provideLanguageAndVersionListAndSiteId() |
|
146 | |||
147 | /** |
||
148 | * @param string $nodeId |
||
149 | * @param string $siteId |
||
150 | * @param int $count |
||
151 | * |
||
152 | * @dataProvider provideNodeSiteAndCount |
||
153 | */ |
||
154 | public function testFindByNodeAndSite($nodeId, $siteId, $count) |
||
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | public function provideNodeSiteAndCount() |
||
170 | |||
171 | /** |
||
172 | * @param string $parentId |
||
173 | * @param string $siteId |
||
174 | * @param int $count |
||
175 | * |
||
176 | * @dataProvider provideParentIdSiteIdAndCount |
||
177 | */ |
||
178 | public function testFindByParent($parentId, $siteId, $count) |
||
184 | |||
185 | /** |
||
186 | * @return array |
||
187 | */ |
||
188 | public function provideParentIdSiteIdAndCount() |
||
197 | |||
198 | /** |
||
199 | * @param string $path |
||
200 | * @param string $siteId |
||
201 | * @param int $count |
||
202 | * |
||
203 | * @dataProvider providePathSiteIdAndCount |
||
204 | */ |
||
205 | public function testFindByIncludedPathAndSiteId($path, $siteId, $count) |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | public function providePathSiteIdAndCount() |
||
223 | |||
224 | /** |
||
225 | * @param string $siteId |
||
226 | * @param int $version |
||
227 | * |
||
228 | * @dataProvider provideSiteIdAndLastVersion |
||
229 | */ |
||
230 | public function testFindLastVersionByType($siteId, $version) |
||
236 | |||
237 | /** |
||
238 | * @return array |
||
239 | */ |
||
240 | public function provideSiteIdAndLastVersion() |
||
246 | |||
247 | /** |
||
248 | * @param string $language |
||
249 | * @param int $version |
||
250 | * @param string $siteId |
||
251 | * @param NodeInterface $node |
||
252 | * @param string $nodeId |
||
253 | */ |
||
254 | protected function assertSameNode($language, $version, $siteId, $node, $nodeId = NodeInterface::ROOT_NODE_ID) |
||
263 | |||
264 | /** |
||
265 | * @param string $siteId |
||
266 | * @param int $nodeNumber |
||
267 | * @param int $version |
||
268 | * @param string $language |
||
269 | * @param string|null $nodeId |
||
270 | * |
||
271 | * @dataProvider provideForGetFooter() |
||
272 | */ |
||
273 | View Code Duplication | public function testGetFooterTree($siteId, $nodeNumber, $version, $language = 'fr', $nodeId = null) |
|
282 | |||
283 | /** |
||
284 | * @return array |
||
285 | */ |
||
286 | public function provideForGetFooter() |
||
294 | |||
295 | /** |
||
296 | * @param string $siteId |
||
297 | * @param int $nodeNumber |
||
298 | * @param int $version |
||
299 | * @param string $language |
||
300 | * |
||
301 | * @dataProvider provideForGetMenu() |
||
302 | */ |
||
303 | public function testGetMenuTree($siteId, $nodeNumber, $version, $language = 'fr') |
||
311 | |||
312 | /** |
||
313 | * @return array |
||
314 | */ |
||
315 | View Code Duplication | public function provideForGetMenu() |
|
322 | |||
323 | /** |
||
324 | * @param string $nodeId |
||
325 | * @param int $nbLevel |
||
326 | * @param int $nodeNumber |
||
327 | * @param int $version |
||
328 | * @param string $siteId |
||
329 | * @param string $local |
||
330 | * |
||
331 | * @dataProvider provideForGetSubMenu |
||
332 | */ |
||
333 | View Code Duplication | public function testGetSubMenu($nodeId, $nbLevel, $nodeNumber, $version, $siteId, $local) |
|
343 | |||
344 | /** |
||
345 | * @return array |
||
346 | */ |
||
347 | public function provideForGetSubMenu() |
||
359 | |||
360 | /** |
||
361 | * @param string $language |
||
362 | * @param string $siteId |
||
363 | * @param int $count |
||
364 | * |
||
365 | * @dataProvider provideLanguageSiteIdAndCount |
||
366 | */ |
||
367 | public function testFindCurrentlyPublishedVersion($language, $siteId, $count) |
||
376 | |||
377 | /** |
||
378 | * @return array |
||
379 | */ |
||
380 | View Code Duplication | public function provideLanguageSiteIdAndCount() |
|
387 | |||
388 | /** |
||
389 | * @param string $author |
||
390 | * @param string $siteId |
||
391 | * @param boolean|null $published |
||
392 | * @param int $limit |
||
393 | * @param array|null $sort |
||
394 | * @param int $count |
||
395 | * |
||
396 | * @dataProvider provideFindByAuthorAndSiteId |
||
397 | */ |
||
398 | public function testFindByAuthorAndSiteId($author, $siteId, $published, $limit, $sort, $count) |
||
405 | |||
406 | /** |
||
407 | * @return array |
||
408 | */ |
||
409 | public function provideFindByAuthorAndSiteId() |
||
410 | { |
||
411 | return array( |
||
412 | array('fake_admin', '2', null, 10, array('updatedAt' => -1), 3), |
||
413 | array('fake_admin', '2', false, 10, null, 1), |
||
414 | array('fake_admin', '2', true, 10, null, 2), |
||
415 | array('fake_admin', '2', true, 2, null, 2), |
||
416 | array('fake_contributor', '2', false, 10, null, 0), |
||
417 | array('fake_contributor', '2', null, 10, null, 0), |
||
418 | array('fake_admin', '3', true, 10, null, 1), |
||
419 | ); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @param string $nodeId |
||
424 | * @param string $language |
||
425 | * @param int $count |
||
426 | * |
||
427 | * @dataProvider provideFindPublishedSortedVersionData |
||
428 | */ |
||
429 | public function testFindPublishedSortedByVersion($nodeId, $language, $count) |
||
433 | |||
434 | /** |
||
435 | * @return array |
||
436 | */ |
||
437 | public function provideFindPublishedSortedVersionData() |
||
445 | |||
446 | /** |
||
447 | * @param string $language |
||
448 | * |
||
449 | * @dataProvider provideLanguage |
||
450 | */ |
||
451 | public function testFindSubTreeByPath($language) |
||
457 | |||
458 | /** |
||
459 | * @return array |
||
460 | */ |
||
461 | public function provideLanguage() |
||
468 | |||
469 | /** |
||
470 | * @param string $parentId |
||
471 | * @param string $routePattern |
||
472 | * @param string $nodeId |
||
473 | * |
||
474 | * @dataProvider provideParentRouteAndNodeId |
||
475 | */ |
||
476 | public function testFindByParentAndRoutePattern($parentId, $routePattern, $nodeId) |
||
480 | |||
481 | /** |
||
482 | * @return array |
||
483 | */ |
||
484 | public function provideParentRouteAndNodeId() |
||
491 | |||
492 | /** |
||
493 | * @param string $parentId |
||
494 | * @param int $order |
||
495 | * @param string $nodeId |
||
496 | * @param bool $expectedValue |
||
497 | * @param string $siteId |
||
498 | * |
||
499 | * @dataProvider provideParentAndOrder |
||
500 | */ |
||
501 | public function testHasOtherNodeWithSameParentAndOrder($parentId, $order, $nodeId, $expectedValue, $siteId = '2') |
||
505 | |||
506 | /** |
||
507 | * @return array |
||
508 | */ |
||
509 | public function provideParentAndOrder() |
||
519 | |||
520 | /** |
||
521 | * @param string $type |
||
522 | * @param int $count |
||
523 | * |
||
524 | * @dataProvider provideNodeTypeAndCount |
||
525 | */ |
||
526 | public function testFindAllNodesOfTypeInLastPublishedVersionForSite($type, $count) |
||
530 | |||
531 | /** |
||
532 | * @return array |
||
533 | */ |
||
534 | public function provideNodeTypeAndCount() |
||
542 | |||
543 | /** |
||
544 | * Test has statused element |
||
545 | */ |
||
546 | View Code Duplication | public function testHasStatusedElement() |
|
553 | |||
554 | /** |
||
555 | * Test find by site and defaultTheme |
||
556 | */ |
||
557 | public function testFindBySiteIdAndDefaultTheme() |
||
562 | |||
563 | /** |
||
564 | * @param string $nodeId |
||
565 | * @param string $language |
||
566 | * |
||
567 | * @dataProvider provideNodeIdAndLanguageForPublishedFlag |
||
568 | */ |
||
569 | public function testfindAllCurrentlyPublishedByElementId($nodeId, $language) |
||
578 | |||
579 | /** |
||
580 | * @return array |
||
581 | */ |
||
582 | public function provideNodeIdAndLanguageForPublishedFlag() |
||
591 | |||
592 | /** |
||
593 | * @param string $siteId |
||
594 | * @param integer $expectedCount |
||
595 | * |
||
596 | * @dataProvider provideFindLastVersionByTypeCurrentlyPublished |
||
597 | */ |
||
598 | public function testFindLastVersionByTypeCurrentlyPublished($siteId, $expectedCount) |
||
602 | |||
603 | /** |
||
604 | * @return array |
||
605 | */ |
||
606 | public function provideFindLastVersionByTypeCurrentlyPublished() |
||
613 | |||
614 | /** |
||
615 | * @param string $path |
||
616 | * @param string $siteId |
||
617 | * @param integer $expectedCount |
||
618 | * |
||
619 | * @dataProvider provideFindByPathCurrentlyPublished |
||
620 | */ |
||
621 | public function testFindByPathCurrentlyPublished($path, $siteId, $expectedCount) |
||
625 | |||
626 | /** |
||
627 | * @return array |
||
628 | */ |
||
629 | View Code Duplication | public function provideFindByPathCurrentlyPublished() |
|
636 | |||
637 | /** |
||
638 | * @param string $path |
||
639 | * @param string $siteId |
||
640 | * @param string $language |
||
641 | * @param integer $expectedCount |
||
642 | * |
||
643 | * @dataProvider provideFindByPathCurrentlyPublishedAndLanguage |
||
644 | */ |
||
645 | public function testFindByPathCurrentlyPublishedAndLanguage($path, $siteId, $language, $expectedCount) |
||
649 | |||
650 | /** |
||
651 | * @return array |
||
652 | */ |
||
653 | View Code Duplication | public function provideFindByPathCurrentlyPublishedAndLanguage() |
|
660 | |||
661 | /** |
||
662 | * @param string $path |
||
663 | * @param string $siteId |
||
664 | * @param string $language |
||
665 | * @param integer $expectedCount |
||
666 | * |
||
667 | * @dataProvider provideFindByIncludedPathSiteIdAndLanguage |
||
668 | */ |
||
669 | public function testFindByIncludedPathSiteIdAndLanguage($path, $siteId, $language, $expectedCount) |
||
673 | |||
674 | /** |
||
675 | * @return array |
||
676 | */ |
||
677 | View Code Duplication | public function provideFindByIncludedPathSiteIdAndLanguage() |
|
684 | } |
||
685 |
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.