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 |
||
8 | class Environment |
||
9 | { |
||
10 | /** |
||
11 | * @return array |
||
12 | */ |
||
13 | protected $addons = null; |
||
14 | |||
15 | /** |
||
16 | * @return array |
||
17 | */ |
||
18 | protected $spacePaths = []; |
||
19 | |||
20 | /** |
||
21 | * @var \Illuminate\Contracts\Foundation\Application |
||
22 | */ |
||
23 | protected $app; |
||
24 | |||
25 | /** |
||
26 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
27 | */ |
||
28 | 28 | public function __construct(Application $app) |
|
34 | |||
35 | /** |
||
36 | * @return void |
||
37 | */ |
||
38 | 28 | private function makeAddonsPaths() |
|
47 | |||
48 | /** |
||
49 | * @return void |
||
50 | */ |
||
51 | public function addSpace($name, $path) |
||
55 | |||
56 | /** |
||
57 | * @param string $name |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 28 | public function path($name = null) |
|
69 | |||
70 | /** |
||
71 | * @param string $space |
||
72 | * @param string $name |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 3 | public function spacePath($space, $name = null) |
|
90 | |||
91 | /** |
||
92 | * @param string $name |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | 3 | public function exists($name) |
|
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 3 | public function existsOnSpace($space, $name) |
|
120 | |||
121 | /** |
||
122 | * @param string $relativeClassName |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 2 | public function classToPath($relativeClassName) |
|
130 | |||
131 | /** |
||
132 | * @param string $relativePath |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | 1 | public function pathToClass($relativePath) |
|
146 | |||
147 | /** |
||
148 | * @return array |
||
149 | */ |
||
150 | 12 | public function loadAddons() |
|
178 | |||
179 | /** |
||
180 | * @return array |
||
181 | */ |
||
182 | 12 | public function addons() |
|
190 | |||
191 | /** |
||
192 | * @return \Jumilla\Addomnipot\Laravel\Addons\Addon |
||
193 | */ |
||
194 | 8 | public function addon($name) |
|
198 | |||
199 | /** |
||
200 | * @return array |
||
201 | */ |
||
202 | 2 | public function addonConsoleCommands() |
|
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | 1 | View Code Duplication | public function addonHttpMiddlewares() |
226 | |||
227 | /** |
||
228 | * @return array |
||
229 | */ |
||
230 | 1 | View Code Duplication | public function addonRouteMiddlewares() |
240 | } |
||
241 |
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.