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 |
||
| 34 | class Settings implements ConfigContract |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * The tag for the cache. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public static $cache_tag = "Settings"; |
||
| 42 | /** |
||
| 43 | * The amount of time in minutes to store values in cache |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $cache_time; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Settings constructor. |
||
| 51 | */ |
||
| 52 | 66 | public function __construct() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Set a key value pair into the Settings store. |
||
| 59 | * |
||
| 60 | * @param string $key A . separated path to this setting |
||
| 61 | * @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 |
||
| 62 | * @throws \Exception |
||
| 63 | */ |
||
| 64 | 33 | public function set($key, $value = null) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Get a value from the Settings store. |
||
| 97 | * |
||
| 98 | * @param string $key A full or partial . separated key. |
||
| 99 | * @param null $default If the key isn't found, return this value. By default undefined keys return null. |
||
| 100 | * @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. |
||
| 101 | */ |
||
| 102 | public function get($key, $default = null, $is_array = false) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Convert an Eloquent Collection into a nested array |
||
| 141 | * |
||
| 142 | * @param $data \Illuminate\Database\Eloquent\Collection The Collection. |
||
| 143 | * @param string $prefix Path to prepend. Do not include trailing . |
||
| 144 | * @return array The resulting nested array. |
||
| 145 | */ |
||
| 146 | 15 | private static function collectionToArray($data, $prefix = "") |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Check if the key is defined in the Settings store. |
||
| 168 | * |
||
| 169 | * @param string $key Only full paths will return true. |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 2 | public function has($key) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Check if the key is read only. |
||
| 179 | * When the user is not a global admin. |
||
| 180 | * Currently, $key is unused |
||
| 181 | * |
||
| 182 | * @param string $key The path to check |
||
| 183 | * @return boolean false or the source: config | auth |
||
| 184 | */ |
||
| 185 | 34 | public function isReadOnly($key) |
|
| 186 | { |
||
| 187 | 34 | $user = \Auth::user(); |
|
| 188 | 34 | return (is_null($user) || !$user->isAdmin()); |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Forget a key and all children |
||
| 193 | * This cannot forget variables set in config.php |
||
| 194 | * |
||
| 195 | * @param string $key Explicit key to forget |
||
| 196 | */ |
||
| 197 | 3 | public function forget($key) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get all settings defined in the Settings store. |
||
| 213 | * |
||
| 214 | * @return array A nested array of all settings. |
||
| 215 | */ |
||
| 216 | 1 | public function all() |
|
| 223 | |||
| 224 | // ---- Local Utility functions ---- |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Clear the settings cache. |
||
| 228 | * If path is set, only clear the path and it's parents. |
||
| 229 | * This will not clear children. |
||
| 230 | * |
||
| 231 | * @param string $key The path to clear. |
||
| 232 | */ |
||
| 233 | 35 | public function flush($key = null) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Prepend a value onto an array configuration value. |
||
| 250 | * |
||
| 251 | * @param string $key |
||
| 252 | * @param mixed $value |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | 1 | public function prepend($key, $value) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Push a value onto an array configuration value. |
||
| 276 | * |
||
| 277 | * @param string $key |
||
| 278 | * @param mixed $value |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | 1 | public function push($key, $value) |
|
| 298 | } |
||
| 299 |
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: