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 | * Set a key value pair into the Settings store. |
||
| 60 | * |
||
| 61 | * @param string $key A . separated path to this setting |
||
| 62 | * @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 |
||
| 63 | * @throws \Exception |
||
| 64 | */ |
||
| 65 | public function set($key, $value = null) |
||
| 66 | { |
||
| 67 | if (is_array($value)) { |
||
| 68 | // repeat until value contains no arrays |
||
| 69 | foreach ($value as $k => $v) { |
||
| 70 | if (!empty($key)) { |
||
| 71 | if (is_string($k) && !str_contains($k, '.') && DbConfig::exactKey($key)->exists() && DbConfig::key($key)->count() == 1) { |
||
| 72 | // check that we aren't trying to set an array onto an existing value only setting |
||
| 73 | throw new \Exception("Attempting to set array value to existing non-array value at the key '".$key."'"); |
||
| 74 | } |
||
| 75 | else { |
||
| 76 | // we are not at the leaf yet, add this chunk to the key and recurse |
||
| 77 | $this->set($key.'.'.$k, $v); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | else { |
||
| 81 | // a leaf, recurse one last time |
||
| 82 | $this->set($k, $v); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | else { |
||
| 87 | // make sure we can save this |
||
| 88 | if ($this->isReadOnly($key)) { |
||
| 89 | throw new \Exception("The setting '".$key."' is read only"); |
||
| 90 | } |
||
| 91 | |||
| 92 | // flush the cache and save the value in db and cache |
||
| 93 | $this->flush($key); |
||
| 94 | DbConfig::updateOrCreate(['config_name' => $key], ['config_value' => $value]); |
||
| 95 | Cache::tags(self::$cache_tag)->put($key, $value, $this->cache_time); |
||
| 96 | } |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get a value from the Settings store. |
||
| 102 | * |
||
| 103 | * @param string $key A full or partial . separated key. |
||
| 104 | * @param null $default If the key isn't found, return this value. By default undefined keys return null. |
||
| 105 | * @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. |
||
| 106 | */ |
||
| 107 | public function get($key, $default = null) |
||
| 108 | { |
||
| 109 | // return value from cache or fetch it and return it |
||
| 110 | return Cache::tags(self::$cache_tag)->remember($key, $this->cache_time, function() use ($key, $default) { |
||
| 111 | |||
| 112 | // fetch the values from storage |
||
| 113 | if (Config::has('config.'.$key)) { |
||
| 114 | $config_data = Config::get('config.'.$key, $default); |
||
| 115 | } |
||
| 116 | $db_data = DbConfig::key($key)->get(['config_name', 'config_value']); |
||
| 117 | |||
| 118 | if (count($db_data) == 1 && $db_data->first()->config_name == $key) { |
||
| 119 | // return a value if we are getting one item and it is the requested item (not recursing) |
||
| 120 | return $db_data->first()->config_value; |
||
| 121 | } |
||
| 122 | elseif (count($db_data) >= 1) { |
||
| 123 | // convert the collection to an array |
||
| 124 | $result = self::collectionToArray($db_data, $key); |
||
| 125 | |||
| 126 | // if we have config_data, merge them |
||
| 127 | if (isset($config_data)) { |
||
| 128 | return array_replace_recursive($config_data, $result); |
||
| 129 | } |
||
| 130 | else { |
||
| 131 | return $result; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // fall back to config.php/defaults.php |
||
| 136 | if (isset($config_data) && (!is_array($config_data) || count($db_data) == 0)) { |
||
| 137 | // return the value from config.php if it is a value |
||
| 138 | return $config_data; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | // we couldn't find the key, return the default |
||
| 143 | return $default; |
||
| 144 | }); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Convert an Eloquent Collection into a nested array |
||
| 149 | * |
||
| 150 | * @param $data \Illuminate\Database\Eloquent\Collection The Collection. |
||
| 151 | * @param string $prefix Path to prepend. Do not include trailing . |
||
| 152 | * @return array The resulting nested array. |
||
| 153 | */ |
||
| 154 | private static function collectionToArray($data, $prefix = "") |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Check if the key is defined in the Settings store. |
||
| 176 | * |
||
| 177 | * @param string $key Only full paths will return true. |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function has($key) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Check if the key is read only. |
||
| 187 | * When the user is not a global admin. |
||
| 188 | * Currently, $key is unused |
||
| 189 | * |
||
| 190 | * @param string $key The path to check |
||
| 191 | * @return boolean false or the source: config | auth |
||
| 192 | */ |
||
| 193 | public function isReadOnly($key) |
||
| 194 | { |
||
| 195 | $user = \Auth::user(); |
||
| 196 | return (is_null($user) || !$user->isAdmin()); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Forget a key and all children |
||
| 201 | * This cannot forget variables set in config.php |
||
| 202 | * |
||
| 203 | * @param string $key Explicit key to forget |
||
| 204 | */ |
||
| 205 | public function forget($key) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get all settings defined in the Settings store. |
||
| 222 | * |
||
| 223 | * @return array A nested array of all settings. |
||
| 224 | */ |
||
| 225 | public function all() |
||
| 232 | |||
| 233 | // ---- Local Utility functions ---- |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Clear the settings cache. |
||
| 237 | * If path is set, only clear the path and it's parents. |
||
| 238 | * This will not clear children. |
||
| 239 | * |
||
| 240 | * @param string $key The path to clear. |
||
| 241 | */ |
||
| 242 | public function flush($key = null) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Prepend a value onto an array configuration value. |
||
| 260 | * |
||
| 261 | * @param string $key |
||
| 262 | * @param mixed $value |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | public function prepend($key, $value) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Push a value onto an array configuration value. |
||
| 287 | * |
||
| 288 | * @param string $key |
||
| 289 | * @param mixed $value |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | public function push($key, $value) |
||
| 310 | } |
||
| 311 |
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: