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() |
||
| 14 | { |
||
| 15 | $basenames = $this->getAllBasenames(); |
||
| 16 | $modules = collect(); |
||
| 17 | |||
| 18 | $basenames->each(function($module, $key) use ($modules) { |
||
|
|
|||
| 19 | $modules->put($module, $this->getProperties($module)); |
||
| 20 | }); |
||
| 21 | |||
| 22 | return $modules->sortBy('order'); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Get all module slugs. |
||
| 27 | * |
||
| 28 | * @return Collection |
||
| 29 | */ |
||
| 30 | public function slugs() |
||
| 31 | { |
||
| 32 | $slugs = collect(); |
||
| 33 | |||
| 34 | $this->all()->each(function($item, $key) use ($slugs) { |
||
| 35 | $slugs->push($item['slug']); |
||
| 36 | }); |
||
| 37 | |||
| 38 | return $slugs; |
||
| 39 | } |
||
| 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) |
||
| 49 | { |
||
| 50 | $collection = $this->all(); |
||
| 51 | |||
| 52 | return $collection->where($key, $value); |
||
| 53 | } |
||
| 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) |
||
| 62 | { |
||
| 63 | $collection = $this->all(); |
||
| 64 | |||
| 65 | return $collection->sortBy($key); |
||
| 66 | } |
||
| 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) |
||
| 75 | { |
||
| 76 | $collection = $this->all(); |
||
| 77 | |||
| 78 | return $collection->sortByDesc($key); |
||
| 79 | } |
||
| 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) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get a module property value. |
||
| 136 | * |
||
| 137 | * @param string $property |
||
| 138 | * @param mixed $default |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function getProperty($property, $default = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set the given module property value. |
||
| 150 | * |
||
| 151 | * @param string $property |
||
| 152 | * @param mixed $value |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function setProperty($property, $value) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get all enabled modules. |
||
| 174 | * |
||
| 175 | * @return Collection |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function enabled() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get all disabled modules. |
||
| 192 | * |
||
| 193 | * @return Collection |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function disabled() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Check if specified module is enabled. |
||
| 210 | * |
||
| 211 | * @param string $slug |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function isEnabled($slug) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Check if specified module is disabled. |
||
| 223 | * |
||
| 224 | * @param string $slug |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function isDisabled($slug) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Enables the specified module. |
||
| 236 | * |
||
| 237 | * @param string $slug |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function enable($slug) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Disables the specified module. |
||
| 247 | * |
||
| 248 | * @param string $slug |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function disable($slug) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Refresh the cache with any newly found modules. |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public function cache() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the contents of the cache file. |
||
| 282 | * |
||
| 283 | * The cache file lists all module slugs and their |
||
| 284 | * enabled or disabled status. This can be used to |
||
| 285 | * filter out modules depending on their status. |
||
| 286 | * |
||
| 287 | * @return Collection |
||
| 288 | */ |
||
| 289 | public function getCache() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set the given cache key value. |
||
| 313 | * |
||
| 314 | * @param string $key |
||
| 315 | * @param mixed $value |
||
| 316 | * @return int |
||
| 317 | */ |
||
| 318 | public function setCache($key, $value) |
||
| 329 | } |
||
| 330 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.