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 |
||
10 | class ContainerTest extends TestCase |
||
11 | { |
||
12 | /** |
||
13 | * @var \JumpGate\Menu\Container |
||
14 | */ |
||
15 | protected $menu; |
||
16 | |||
17 | 16 | public function setUp() |
|
23 | |||
24 | /** @test */ |
||
25 | 1 | public function it_adds_a_menu() |
|
31 | |||
32 | /** @test */ |
||
33 | 2 | public function it_checks_if_menu_exists_returns_false() |
|
37 | |||
38 | /** @test */ |
||
39 | 1 | public function it_checks_if_menu_exists_returns_true() |
|
46 | |||
47 | /** @test */ |
||
48 | 1 | public function it_checks_if_has_links_return_true() |
|
57 | |||
58 | /** @test */ |
||
59 | 2 | public function it_checks_if_has_links_return_false() |
|
66 | |||
67 | /** @test */ |
||
68 | 1 | public function it_checks_set_active() |
|
74 | |||
75 | /** @test */ |
||
76 | 1 | public function it_checks_if_get_menu_returns_the_existing_menu() |
|
85 | |||
86 | /** |
||
87 | * @test |
||
88 | * |
||
89 | * @expectedException Exception |
||
90 | * @expectedExceptionMessage Menu test not found. |
||
91 | */ |
||
92 | 1 | public function it_throws_an_exception_if_render_cant_find_the_menu() |
|
96 | |||
97 | /** @test */ |
||
98 | 1 | public function it_can_render_a_menu() |
|
107 | |||
108 | /** @test */ |
||
109 | 1 | public function it_sets_active_items() |
|
122 | |||
123 | /** @test */ |
||
124 | 1 | View Code Duplication | public function it_sets_active_items_in_dropdown() |
140 | |||
141 | /** @test */ |
||
142 | 1 | View Code Duplication | public function it_sets_active_items_in_dropdown_without_parentage() |
160 | |||
161 | /** @test */ |
||
162 | 1 | public function it_tests_insert_before_link() |
|
163 | { |
||
164 | 1 | $menu = $this->menu->getMenu('test'); |
|
165 | |||
166 | $menu->link('slug', function () { |
||
167 | // |
||
168 | 1 | }); |
|
169 | |||
170 | $menu->link('slug2', function () { |
||
171 | 1 | $link->insertBefore('slug'); |
|
172 | 1 | }); |
|
173 | |||
174 | $this->assertEquals('slug2', $menu->links->first()->slug); |
||
175 | } |
||
176 | |||
177 | /** @test */ |
||
178 | 1 | View Code Duplication | public function it_tests_insert_before_dropdown() |
194 | |||
195 | /** @test */ |
||
196 | 1 | View Code Duplication | public function it_tests_insert_after_link() |
218 | |||
219 | /** @test */ |
||
220 | 1 | public function it_tests_insert_after_dropdown() |
|
242 | } |
||
243 |
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.