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 namespace Anomaly\Streams\Platform\Addon; |
||
13 | class AddonPaths |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * The config repository. |
||
18 | * |
||
19 | * @var Repository |
||
20 | */ |
||
21 | protected $config; |
||
22 | |||
23 | /** |
||
24 | * The stream application. |
||
25 | * |
||
26 | * @var Application |
||
27 | */ |
||
28 | protected $application; |
||
29 | |||
30 | /** |
||
31 | * Create a new AddonPaths instance. |
||
32 | * |
||
33 | * @param Application $application |
||
34 | * @param Repository $config |
||
35 | */ |
||
36 | public function __construct(Application $application, Repository $config) |
||
37 | { |
||
38 | $this->config = $config; |
||
39 | $this->application = $application; |
||
40 | } |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Return all addon paths in a given folder. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function all() |
||
49 | { |
||
50 | $eager = $this->eager(); |
||
51 | $deferred = $this->deferred(); |
||
52 | |||
53 | $core = $this->core() ?: []; |
||
54 | $shared = $this->shared() ?: []; |
||
55 | $application = $this->application() ?: []; |
||
56 | |||
57 | // Testing only addons. |
||
58 | $testing = $this->testing() ?: []; |
||
59 | |||
60 | // Other configured addons. |
||
61 | $configured = $this->configured() ?: []; |
||
62 | |||
63 | /* |
||
64 | * Merge the eager and deferred |
||
65 | * onto the front and back of |
||
66 | * the paths respectively. |
||
67 | */ |
||
68 | |||
69 | return array_unique( |
||
70 | array_merge( |
||
71 | $eager, |
||
72 | array_reverse( |
||
73 | array_unique( |
||
74 | array_reverse( |
||
75 | array_merge( |
||
76 | array_filter( |
||
77 | array_merge($core, $shared, $application, $configured, $testing) |
||
78 | ), |
||
79 | $deferred |
||
80 | ) |
||
81 | ) |
||
82 | ) |
||
83 | ) |
||
84 | ) |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Return paths to eager loaded addons. |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function eager() |
||
102 | |||
103 | /** |
||
104 | * Return paths to deferred addons. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | protected function deferred() |
||
117 | |||
118 | /** |
||
119 | * Return all core addon paths in a given folder. |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | View Code Duplication | public function core() |
|
133 | |||
134 | /** |
||
135 | * Return vendor addons of a given type. |
||
136 | * |
||
137 | * @param $directories |
||
138 | * @return array |
||
139 | */ |
||
140 | protected function vendorAddons($directories) |
||
152 | |||
153 | /** |
||
154 | * Return all shared addon paths in a given folder. |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | View Code Duplication | public function shared() |
|
168 | |||
169 | /** |
||
170 | * Return all application addon paths in a given folder. |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | View Code Duplication | public function application() |
|
184 | |||
185 | /** |
||
186 | * Return paths to testing only addons. |
||
187 | * |
||
188 | * @return array|bool |
||
189 | */ |
||
190 | View Code Duplication | protected function testing() |
|
200 | |||
201 | /** |
||
202 | * Return paths to testing only addons. |
||
203 | * |
||
204 | * @return array|bool |
||
205 | */ |
||
206 | View Code Duplication | protected function directory($directory) |
|
216 | |||
217 | /** |
||
218 | * Return paths to configured addons. |
||
219 | * |
||
220 | * @return array|bool |
||
221 | */ |
||
222 | protected function configured() |
||
237 | } |
||
238 |
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.