Total Complexity | 46 |
Total Lines | 277 |
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) |
||
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) |
||
109 | } |
||
110 | |||
111 | public static function has($storage, $setting) |
||
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) |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Add data to an existing key of a storage array. |
||
154 | * |
||
155 | * @param array $storage |
||
156 | * @param mixed $setting Can be an array, a string, |
||
157 | * or a special formatted string |
||
158 | * (eg 'app/path/project'). |
||
159 | * @param mixed $data |
||
160 | * @return array |
||
161 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
162 | */ |
||
163 | public static function add($storage, $setting, $data) |
||
164 | { |
||
165 | if (!is_array($storage) || empty($setting)) { |
||
166 | throw new ArrayStorageException('Invalid parameters specified.'); |
||
167 | } |
||
168 | $setting = self::parseSetting($setting); |
||
169 | $newData = [$data]; |
||
170 | if (self::has($storage, $setting)) { |
||
171 | $existingData = self::get($storage, $setting); |
||
172 | if (!is_array($existingData)) { |
||
173 | throw new ArrayStorageException('Invalid existing data type.'); |
||
174 | } |
||
175 | $newData = array_merge($existingData, $newData); |
||
176 | } |
||
177 | return self::set($storage, $setting, $newData); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Append data to a storage array. |
||
182 | * |
||
183 | * @param array $storage |
||
184 | * @param mixed $data |
||
185 | * @return array |
||
186 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
187 | */ |
||
188 | public static function append($storage, $data = []) |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Removes a setting from a storage array. |
||
208 | * |
||
209 | * @param array $storage |
||
210 | * @param mixed $setting Can be an array, a string, |
||
211 | * or a special formatted string |
||
212 | * (eg 'app/path/project'). |
||
213 | * |
||
214 | * @return array The updated storage array. |
||
215 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
216 | */ |
||
217 | public static function remove($storage, $setting) |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Remove index from multi-dimensional array. |
||
243 | * |
||
244 | * https://stackoverflow.com/questions/26661828/ |
||
245 | * |
||
246 | * @param array $array |
||
247 | * The array to remove the index from. |
||
248 | * @param array $indices |
||
249 | * Indexed array containing the indices chain up to the index that should be |
||
250 | * removed. |
||
251 | * @return array |
||
252 | * The array with the index removed. |
||
253 | * @throws \WebServCo\Framework\Exceptions\ArrayStorageException |
||
254 | * If the index does not exist within the array. |
||
255 | */ |
||
256 | protected static function removeByIndex($array, $indices) |
||
283 | } |
||
284 | } |
||
285 |