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 |
||
15 | class MediaRepositoryTest extends AbstractKernelTestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var MediaRepositoryInterface |
||
19 | */ |
||
20 | protected $repository; |
||
21 | protected $keywordRepository; |
||
22 | |||
23 | /** |
||
24 | * Set up test |
||
25 | */ |
||
26 | protected function setUp() |
||
27 | { |
||
28 | parent::setUp(); |
||
29 | |||
30 | static::bootKernel(); |
||
31 | $this->keywordRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.keyword'); |
||
32 | $this->repository = static::$kernel->getContainer()->get('open_orchestra_media.repository.media'); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param string $keywords |
||
37 | * @param int $count |
||
38 | * |
||
39 | * @dataProvider provideKeywordAndCount |
||
40 | */ |
||
41 | public function testFindByKeywords($keywords, $count) |
||
48 | |||
49 | /** |
||
50 | * @return array |
||
51 | */ |
||
52 | public function provideKeywordAndCount() |
||
61 | |||
62 | /** |
||
63 | * @param string $condition |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | View Code Duplication | protected function replaceKeywordLabelById($condition) |
|
85 | |||
86 | /** |
||
87 | * test findForPaginate |
||
88 | * |
||
89 | * @param PaginateFinderConfiguration $configuration |
||
90 | * @param int $expectedCount |
||
91 | * @param int $expectedFilteredCount |
||
92 | * |
||
93 | * @dataProvider providePaginateConfiguration |
||
94 | */ |
||
95 | public function testFindForPaginate(PaginateFinderConfiguration $configuration, $expectedCount, $expectedFilteredCount) |
||
99 | |||
100 | /** |
||
101 | * test count |
||
102 | */ |
||
103 | public function testCount() |
||
107 | |||
108 | /** |
||
109 | * test count with Filtertered |
||
110 | */ |
||
111 | public function testCountFiltered() |
||
112 | { |
||
113 | $this->assertSame(1, $this->repository->count('pdf')); |
||
114 | $this->assertSame(5, $this->repository->count('image')); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * test countWithFilter |
||
119 | * |
||
120 | * @param PaginateFinderConfiguration $configuration |
||
121 | * @param int $expectedCount |
||
122 | * @param int $expectedFilteredCount |
||
123 | * |
||
124 | * @dataProvider providePaginateConfiguration |
||
125 | */ |
||
126 | public function testCountWithFilter(PaginateFinderConfiguration $configuration, $expectedCount, $expectedFilteredCount) |
||
130 | |||
131 | /** |
||
132 | * Provide PaginateFinderConfiguration |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function providePaginateConfiguration() |
||
149 | |||
150 | /** |
||
151 | * Test remove medias |
||
152 | */ |
||
153 | View Code Duplication | public function testRemoveMedias() |
|
169 | } |
||
170 |
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.