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 |
||
7 | class ResolvesViewsTest extends TestCase |
||
8 | { |
||
9 | /** |
||
10 | * Setup the test environment. |
||
11 | */ |
||
12 | 9 | public function setUp() |
|
24 | |||
25 | /** |
||
26 | * Get package providers. At a minimum this is the package being tested, but also |
||
27 | * would include packages upon which our package depends, e.g. Cartalyst/Sentry |
||
28 | * In a normal app environment these would be added to the 'providers' array in |
||
29 | * the config/app.php file. |
||
30 | * |
||
31 | * @param \Illuminate\Foundation\Application $app |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | 9 | protected function getPackageProviders($app) |
|
41 | |||
42 | /** |
||
43 | * @test |
||
44 | * |
||
45 | * @expectedException InvalidArgumentException |
||
46 | * @expectedExceptionMessage The layoutOptions must be an array. |
||
47 | */ |
||
48 | 1 | public function it_errors_when_no_config_is_set() |
|
52 | |||
53 | /** |
||
54 | * @test |
||
55 | * |
||
56 | * @expectedException InvalidArgumentException |
||
57 | * @expectedExceptionMessage The layoutOptions must have a default layout view. |
||
58 | */ |
||
59 | 1 | public function it_fails_when_no_default_layout_is_present() |
|
66 | |||
67 | /** |
||
68 | * @test |
||
69 | * |
||
70 | * @expectedException InvalidArgumentException |
||
71 | * @expectedExceptionMessage The layoutOptions must have a ajax layout view. |
||
72 | */ |
||
73 | 1 | public function it_fails_when_no_ajax_layout_is_present() |
|
82 | |||
83 | /** |
||
84 | * @test |
||
85 | */ |
||
86 | 1 | View Code Duplication | public function it_can_use_the_config_for_defaults() |
99 | |||
100 | /** |
||
101 | * @test |
||
102 | */ |
||
103 | 1 | View Code Duplication | public function it_can_use_the_config_for_default_layout_options() |
125 | |||
126 | /** |
||
127 | * @test |
||
128 | */ |
||
129 | 1 | View Code Duplication | public function it_can_automatically_resolve_a_view() |
150 | |||
151 | /** |
||
152 | * @test |
||
153 | */ |
||
154 | 1 | View Code Duplication | public function it_can_manually_specify_a_view() |
168 | |||
169 | /** |
||
170 | * @test |
||
171 | */ |
||
172 | 1 | View Code Duplication | public function it_can_manually_specify_a_layout() |
190 | |||
191 | /** |
||
192 | * @test |
||
193 | */ |
||
194 | 1 | View Code Duplication | public function it_can_get_a_view_from_the_config() |
217 | |||
218 | 5 | private function loadLayout($boolean = true) |
|
222 | |||
223 | 4 | private function createView($view) |
|
227 | |||
228 | 3 | private function deleteView($view) |
|
232 | |||
233 | 1 | private function makeViewDir($directory) |
|
237 | |||
238 | 1 | private function deleteViewDir($directory) |
|
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.