| Total Complexity | 41 | 
| Total Lines | 249 | 
| Duplicated Lines | 0 % | 
| Changes | 11 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like ArrayStorage 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.
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 ArrayStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 6 | final class ArrayStorage  | 
            ||
| 7 | { | 
            ||
| 8 | /**  | 
            ||
| 9 | * Parse the setting key to make sure it's a simple string  | 
            ||
| 10 | * or an array.  | 
            ||
| 11 | */  | 
            ||
| 12 | private static function parseSetting($setting)  | 
            ||
| 19 | }  | 
            ||
| 20 | |||
| 21 | /**  | 
            ||
| 22 | * Retrieve a value from a storage array.  | 
            ||
| 23 | *  | 
            ||
| 24 | * @param mixed $storage  | 
            ||
| 25 | * @param mixed $setting Can be an array, a string,  | 
            ||
| 26 | * or a special formatted string  | 
            ||
| 27 | * (eg 'app/path/project').  | 
            ||
| 28 | * @param mixed $defaultValue  | 
            ||
| 29 | * @return mixed  | 
            ||
| 30 | */  | 
            ||
| 31 | public static function get($storage, $setting = null, $defaultValue = false)  | 
            ||
| 32 |     { | 
            ||
| 33 | $setting = self::parseSetting($setting);  | 
            ||
| 34 | |||
| 35 |         if (!isset($setting) || is_bool($setting) || empty($storage)) { | 
            ||
| 36 | // use "!isset" for `$setting` and not "empty" (can be 0)  | 
            ||
| 37 | // `is_bool` check: handle wrong $setting type  | 
            ||
| 38 | // prevents: "array_key_exists(): The first argument should be either a string or an integer"  | 
            ||
| 39 | return $defaultValue;  | 
            ||
| 40 | }  | 
            ||
| 41 | |||
| 42 |         if (!is_array($storage)) { | 
            ||
| 43 | return $defaultValue;  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * If $setting is an array, process it recursively.  | 
            ||
| 48 | */  | 
            ||
| 49 |         if (is_array($setting)) { | 
            ||
| 50 | /**  | 
            ||
| 51 | * Check if we have the first $setting element in the  | 
            ||
| 52 | * configuration data array.  | 
            ||
| 53 | */  | 
            ||
| 54 |             if (array_key_exists(0, $setting) && array_key_exists($setting[0], $storage)) { | 
            ||
| 55 | /**  | 
            ||
| 56 | * Remove first element from $setting.  | 
            ||
| 57 | */  | 
            ||
| 58 | $key = array_shift($setting);  | 
            ||
| 59 | /**  | 
            ||
| 60 | * At the end of the recursion $setting will be  | 
            ||
| 61 | * an empty array. In this case we simply return the  | 
            ||
| 62 | * current configuration data.  | 
            ||
| 63 | */  | 
            ||
| 64 |                 if (empty($setting)) { | 
            ||
| 65 | return false !== $storage[$key] ? $storage[$key] : $defaultValue;  | 
            ||
| 66 | }  | 
            ||
| 67 | /**  | 
            ||
| 68 | * Go down one element in the configuration data  | 
            ||
| 69 | * and call the method again, with the remainig setting.  | 
            ||
| 70 | */  | 
            ||
| 71 | return self::get($storage[$key], $setting, $defaultValue);  | 
            ||
| 72 | }  | 
            ||
| 73 | /**  | 
            ||
| 74 | * The requested setting doesn't exist in our  | 
            ||
| 75 | * configuration data array.  | 
            ||
| 76 | */  | 
            ||
| 77 | return $defaultValue;  | 
            ||
| 78 | }  | 
            ||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * If we arrive here, $setting must be a simple string.  | 
            ||
| 82 | */  | 
            ||
| 83 |         if (array_key_exists($setting, $storage)) { | 
            ||
| 84 | return $storage[$setting];  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * If we got this far, there is no data to return.  | 
            ||
| 89 | */  | 
            ||
| 90 | return $defaultValue;  | 
            ||
| 91 | }  | 
            ||
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * Retrieve a value from a storage array.  | 
            ||
| 95 | *  | 
            ||
| 96 | * Returns $defaultValue if $setting is empty.  | 
            ||
| 97 | *  | 
            ||
| 98 | * @param mixed $storage  | 
            ||
| 99 | * @param mixed $setting Can be an array, a string,  | 
            ||
| 100 | * or a special formatted string  | 
            ||
| 101 | * (eg 'app/path/project').  | 
            ||
| 102 | * @param mixed $defaultValue  | 
            ||
| 103 | * @return mixed  | 
            ||
| 104 | */  | 
            ||
| 105 | public static function getElse($storage, $setting = null, $defaultValue = false)  | 
            ||
| 106 |     { | 
            ||
| 107 | $data = self::get($storage, $setting, $defaultValue);  | 
            ||
| 108 | return !empty($data) ? $data : $defaultValue;  | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 | public static function has($storage, $setting)  | 
            ||
| 112 |     { | 
            ||
| 113 | $value = 'WSFW_NOEXIST';  | 
            ||
| 114 | $check = self::get($storage, $setting, $value);  | 
            ||
| 115 | return $check !== $value;  | 
            ||
| 116 | }  | 
            ||
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * Sets a value in a storage array.  | 
            ||
| 120 | *  | 
            ||
| 121 | * @param array $storage  | 
            ||
| 122 | * @param mixed $setting Can be an array, a string,  | 
            ||
| 123 | * or a special formatted string  | 
            ||
| 124 | * (eg 'app/path/project').  | 
            ||
| 125 | * @param mixed $value The value to be stored.  | 
            ||
| 126 | *  | 
            ||
| 127 | * @return array The storage array with new data.  | 
            ||
| 128 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException  | 
            ||
| 129 | */  | 
            ||
| 130 | public static function set($storage, $setting, $value)  | 
            ||
| 131 |     { | 
            ||
| 132 |         if (!is_array($storage) || empty($setting)) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 133 |             throw new ArrayStorageException('Invalid parameters specified.'); | 
            ||
| 134 | }  | 
            ||
| 135 | $setting = self::parseSetting($setting);  | 
            ||
| 136 |         if (is_array($setting)) { | 
            ||
| 137 | $reference = &$storage;  | 
            ||
| 138 |             foreach ($setting as $item) { | 
            ||
| 139 |                 if (!is_array($reference)) { | 
            ||
| 140 | $reference = [];  | 
            ||
| 141 | }  | 
            ||
| 142 | $reference = &$reference[$item];  | 
            ||
| 143 | }  | 
            ||
| 144 | $reference = $value;  | 
            ||
| 145 | unset($reference);  | 
            ||
| 146 | return $storage;  | 
            ||
| 147 | }  | 
            ||
| 148 | $storage[$setting] = $value;  | 
            ||
| 149 | return $storage;  | 
            ||
| 150 | }  | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Append data to a storage array.  | 
            ||
| 154 | *  | 
            ||
| 155 | * @param array $storage  | 
            ||
| 156 | * @param mixed $data  | 
            ||
| 157 | * @return array  | 
            ||
| 158 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException  | 
            ||
| 159 | */  | 
            ||
| 160 | public static function append($storage, $data = [])  | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * Removes a setting from a storage array.  | 
            ||
| 180 | *  | 
            ||
| 181 | * @param array $storage  | 
            ||
| 182 | * @param mixed $setting Can be an array, a string,  | 
            ||
| 183 | * or a special formatted string  | 
            ||
| 184 | * (eg 'app/path/project').  | 
            ||
| 185 | *  | 
            ||
| 186 | * @return array The updated storage array.  | 
            ||
| 187 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException  | 
            ||
| 188 | */  | 
            ||
| 189 | public static function remove($storage, $setting)  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * Remove index from multi-dimensional array.  | 
            ||
| 215 | *  | 
            ||
| 216 | * https://stackoverflow.com/questions/26661828/  | 
            ||
| 217 | *  | 
            ||
| 218 | * @param array $array  | 
            ||
| 219 | * The array to remove the index from.  | 
            ||
| 220 | * @param array $indices  | 
            ||
| 221 | * Indexed array containing the indices chain up to the index that should be  | 
            ||
| 222 | * removed.  | 
            ||
| 223 | * @return array  | 
            ||
| 224 | * The array with the index removed.  | 
            ||
| 225 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException  | 
            ||
| 226 | * If the index does not exist within the array.  | 
            ||
| 227 | */  | 
            ||
| 228 | protected static function removeByIndex($array, $indices)  | 
            ||
| 255 | }  | 
            ||
| 256 | }  | 
            ||
| 257 |