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 |
||
6 | class LocalRepository extends Repository |
||
7 | { |
||
8 | /** |
||
9 | * Get all modules. |
||
10 | * |
||
11 | * @return Collection |
||
12 | */ |
||
13 | public function all() |
||
24 | |||
25 | /** |
||
26 | * Get all module slugs. |
||
27 | * |
||
28 | * @return Collection |
||
29 | */ |
||
30 | public function slugs() |
||
40 | |||
41 | /** |
||
42 | * Get modules based on where clause. |
||
43 | * |
||
44 | * @param string $key |
||
45 | * @param mixed $value |
||
46 | * @return Collection |
||
47 | */ |
||
48 | public function where($key, $value) |
||
54 | |||
55 | /** |
||
56 | * Sort modules by given key in ascending order. |
||
57 | * |
||
58 | * @param string $key |
||
59 | * @return Collection |
||
60 | */ |
||
61 | public function sortBy($key) |
||
67 | |||
68 | /** |
||
69 | * Sort modules by given key in ascending order. |
||
70 | * |
||
71 | * @param string $key |
||
72 | * @return Collection |
||
73 | */ |
||
74 | public function sortByDesc($key) |
||
80 | |||
81 | /** |
||
82 | * Determines if the given module exists. |
||
83 | * |
||
84 | * @param string $slug |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function exists($slug) |
||
91 | |||
92 | /** |
||
93 | * Returns count of all modules. |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | public function count() |
||
101 | |||
102 | /** |
||
103 | * Get a module's properties. |
||
104 | * |
||
105 | * @param string $slug |
||
106 | * @return Collection|null |
||
107 | */ |
||
108 | public function getProperties($slug) |
||
125 | |||
126 | /** |
||
127 | * Get a module property value. |
||
128 | * |
||
129 | * @param string $property |
||
130 | * @param mixed $default |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function getProperty($property, $default = null) |
||
139 | |||
140 | /** |
||
141 | * Set the given module property value. |
||
142 | * |
||
143 | * @param string $property |
||
144 | * @param mixed $value |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function setProperty($property, $value) |
||
163 | |||
164 | /** |
||
165 | * Get all enabled modules. |
||
166 | * |
||
167 | * @return Collection |
||
168 | */ |
||
169 | View Code Duplication | public function enabled() |
|
181 | |||
182 | /** |
||
183 | * Get all disabled modules. |
||
184 | * |
||
185 | * @return Collection |
||
186 | */ |
||
187 | View Code Duplication | public function disabled() |
|
199 | |||
200 | /** |
||
201 | * Check if specified module is enabled. |
||
202 | * |
||
203 | * @param string $slug |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function isEnabled($slug) |
||
212 | |||
213 | /** |
||
214 | * Check if specified module is disabled. |
||
215 | * |
||
216 | * @param string $slug |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function isDisabled($slug) |
||
225 | |||
226 | /** |
||
227 | * Enables the specified module. |
||
228 | * |
||
229 | * @param string $slug |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function enable($slug) |
||
236 | |||
237 | /** |
||
238 | * Disables the specified module. |
||
239 | * |
||
240 | * @param string $slug |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function disable($slug) |
||
247 | |||
248 | /** |
||
249 | * Refresh the cache with any newly found modules. |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function cache() |
||
271 | |||
272 | /** |
||
273 | * Get the contents of the cache file. |
||
274 | * |
||
275 | * The cache file lists all module slugs and their |
||
276 | * enabled or disabled status. This can be used to |
||
277 | * filter out modules depending on their status. |
||
278 | * |
||
279 | * @return Collection |
||
280 | */ |
||
281 | protected function getCacheFile() |
||
302 | |||
303 | /** |
||
304 | * Set the given cache key value. |
||
305 | * |
||
306 | * @param string $key |
||
307 | * @param mixed $value |
||
308 | * @return int |
||
309 | */ |
||
310 | protected function setCacheFile($key, $value) |
||
321 | } |
||
322 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.