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:
Complex classes like Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Settings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Settings implements ConfigContract |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * The tag for the cache. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public static $cache_tag = "Settings"; |
||
| 43 | /** |
||
| 44 | * The amount of time in minutes to store values in cache |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | private $cache_time; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Settings constructor. |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Check if the key is defined in the Settings store. |
||
| 60 | * |
||
| 61 | * @param string $key Only full paths will return true. |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function has($key) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get all settings defined in the Settings store. |
||
| 71 | * |
||
| 72 | * @return array A nested array of all settings. |
||
| 73 | */ |
||
| 74 | public function all() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Convert an Eloquent Collection into a nested array |
||
| 84 | * |
||
| 85 | * @param $data \Illuminate\Database\Eloquent\Collection The Collection. |
||
| 86 | * @param string $prefix Path to prepend. Do not include trailing . |
||
| 87 | * @return array The resulting nested array. |
||
| 88 | */ |
||
| 89 | private static function collectionToArray($data, $prefix = "") |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Prepend a value onto an array configuration value. |
||
| 111 | * |
||
| 112 | * @param string $key |
||
| 113 | * @param mixed $value |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function prepend($key, $value) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get a value from the Settings store. |
||
| 138 | * |
||
| 139 | * @param string $key A full or partial . separated key. |
||
| 140 | * @param null $default If the key isn't found, return this value. By default undefined keys return null. |
||
| 141 | * @return mixed If the $key is a full path, a bare value will be returned. If it is a partial path, a nested array will be retuned. |
||
| 142 | */ |
||
| 143 | public function get($key, $default = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Forget a key and all children |
||
| 185 | * This cannot forget variables set in config.php |
||
| 186 | * |
||
| 187 | * @param string $key Explicit key to forget |
||
| 188 | */ |
||
| 189 | public function forget($key) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Clear the settings cache. |
||
| 206 | * If path is set, only clear the path and it's parents. |
||
| 207 | * This will not clear children. |
||
| 208 | * |
||
| 209 | * @param string $key The path to clear. |
||
| 210 | */ |
||
| 211 | public function flush($key = null) |
||
| 226 | |||
| 227 | // ---- Local Utility functions ---- |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set a key value pair into the Settings store. |
||
| 231 | * |
||
| 232 | * @param string $key A . separated path to this setting |
||
| 233 | * @param array|null $value A value or an array. If value is an array it will be converted to a . separate path(s) concatinated onto the given key |
||
| 234 | * @throws \Exception |
||
| 235 | */ |
||
| 236 | public function set($key, $value = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Check if the key is read only. |
||
| 271 | * When the user is not a global admin. |
||
| 272 | * Currently, $key is unused |
||
| 273 | * |
||
| 274 | * @param string $key The path to check |
||
| 275 | * @return boolean false or the source: config | auth |
||
| 276 | */ |
||
| 277 | public function isReadOnly($key) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Push a value onto an array configuration value. |
||
| 285 | * |
||
| 286 | * @param string $key |
||
| 287 | * @param mixed $value |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | public function push($key, $value) |
||
| 308 | } |
||
| 309 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: