| Total Complexity | 46 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 1 | 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 |
||
| 9 | final class ArrayStorage |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Add data to an existing key of a storage array. |
||
| 13 | * |
||
| 14 | * @param array<mixed> $storage |
||
| 15 | * @param mixed $setting Can be an array, a string, |
||
| 16 | * or a special formatted string |
||
| 17 | * (eg 'i18n/lang'). |
||
| 18 | * @param mixed $data |
||
| 19 | * @return array<mixed> |
||
| 20 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
| 21 | */ |
||
| 22 | public static function add(array $storage, $setting, $data): array |
||
| 23 | { |
||
| 24 | if (!\is_array($storage) || empty($setting)) { |
||
|
|
|||
| 25 | throw new ArrayStorageException('Invalid parameters specified.'); |
||
| 26 | } |
||
| 27 | $setting = self::parseSetting($setting); |
||
| 28 | $newData = [$data]; |
||
| 29 | if (self::has($storage, $setting)) { |
||
| 30 | $existingData = self::get($storage, $setting); |
||
| 31 | if (!\is_array($existingData)) { |
||
| 32 | throw new ArrayStorageException('Invalid existing data type.'); |
||
| 33 | } |
||
| 34 | $newData = \array_merge($existingData, $newData); |
||
| 35 | } |
||
| 36 | return self::set($storage, $setting, $newData); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Append data to a storage array. |
||
| 41 | * |
||
| 42 | * @param array<mixed> $storage |
||
| 43 | * @param mixed $data |
||
| 44 | * @return array<mixed> |
||
| 45 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
| 46 | */ |
||
| 47 | public static function append(array $storage, $data = []): array |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Retrieve a value from a storage array. |
||
| 64 | * |
||
| 65 | * @param mixed $storage |
||
| 66 | * @param mixed $setting Can be an array, a string, |
||
| 67 | * or a special formatted string |
||
| 68 | * (eg 'i18n/lang'). |
||
| 69 | * @param mixed $defaultValue |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public static function get($storage, $setting = null, $defaultValue = null) |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Retrieve a value from a storage array. |
||
| 138 | * |
||
| 139 | * Returns $defaultValue if $setting is empty. |
||
| 140 | * |
||
| 141 | * @param mixed $storage |
||
| 142 | * @param mixed $setting Can be an array, a string, |
||
| 143 | * or a special formatted string |
||
| 144 | * (eg 'i18n/lang'). |
||
| 145 | * @param mixed $defaultValue |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public static function getElse($storage, $setting = null, $defaultValue = null) |
||
| 149 | { |
||
| 150 | $data = self::get($storage, $setting, $defaultValue); |
||
| 151 | return !empty($data) |
||
| 152 | ? $data |
||
| 153 | : $defaultValue; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param mixed $storage |
||
| 158 | * @param mixed $setting Can be an array, a string, |
||
| 159 | * or a special formatted string |
||
| 160 | * (eg 'i18n/lang'). |
||
| 161 | */ |
||
| 162 | public static function has($storage, $setting): bool |
||
| 163 | { |
||
| 164 | $value = 'WSFW_NOEXIST'; |
||
| 165 | $check = self::get($storage, $setting, $value); |
||
| 166 | return $check !== $value; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Removes a setting from a storage array. |
||
| 171 | * |
||
| 172 | * @param array<mixed> $storage |
||
| 173 | * @param mixed $setting Can be an array, a string, |
||
| 174 | * or a special formatted string |
||
| 175 | * (eg 'i18n/lang'). |
||
| 176 | * @return array<mixed> The updated storage array. |
||
| 177 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
| 178 | */ |
||
| 179 | public static function remove(array $storage, $setting): array |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets a value in a storage array. |
||
| 205 | * |
||
| 206 | * @param array<mixed> $storage |
||
| 207 | * @param mixed $setting Can be an array, a string, |
||
| 208 | * or a special formatted string |
||
| 209 | * (eg 'i18n/lang'). |
||
| 210 | * @param mixed $value The value to be stored. |
||
| 211 | * @return array<mixed> The storage array with new data. |
||
| 212 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
| 213 | */ |
||
| 214 | public static function set(array $storage, $setting, $value): array |
||
| 215 | { |
||
| 216 | if (!\is_array($storage) || empty($setting)) { |
||
| 217 | throw new ArrayStorageException('Invalid parameters specified.'); |
||
| 218 | } |
||
| 219 | $setting = self::parseSetting($setting); |
||
| 220 | if (\is_array($setting)) { |
||
| 221 | // phpcs:ignore SlevomatCodingStandard.PHP.DisallowReference.DisallowedAssigningByReference |
||
| 222 | $reference = &$storage; |
||
| 223 | foreach ($setting as $item) { |
||
| 224 | if (!\is_array($reference)) { |
||
| 225 | $reference = []; |
||
| 226 | } |
||
| 227 | // phpcs:ignore SlevomatCodingStandard.PHP.DisallowReference.DisallowedAssigningByReference |
||
| 228 | $reference = &$reference[$item]; |
||
| 229 | } |
||
| 230 | $reference = $value; |
||
| 231 | unset($reference); |
||
| 232 | return $storage; |
||
| 233 | } |
||
| 234 | $storage[$setting] = $value; |
||
| 235 | return $storage; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Remove index from multi-dimensional array. |
||
| 240 | * |
||
| 241 | * https://stackoverflow.com/questions/26661828/ |
||
| 242 | * |
||
| 243 | * @param array<mixed> $array |
||
| 244 | * The array to remove the index from. |
||
| 245 | * @param array<int,string> $indices |
||
| 246 | * Indexed array containing the indices chain up to the index that should be |
||
| 247 | * removed. |
||
| 248 | * @return array<mixed> |
||
| 249 | * The array with the index removed. |
||
| 250 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
| 251 | * If the index does not exist within the array. |
||
| 252 | */ |
||
| 253 | protected static function removeByIndex(array $array, array $indices): array |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Parse the setting key to make sure it's a simple string |
||
| 287 | * or an array. |
||
| 288 | * |
||
| 289 | * @param mixed $setting |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | private static function parseSetting($setting) |
||
| 298 | } |
||
| 299 | } |
||
| 300 |