Complex classes like BaseArrayHelper 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 BaseArrayHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class BaseArrayHelper |
||
23 | { |
||
24 | /** |
||
25 | * Converts an object or an array of objects into an array. |
||
26 | * @param object|array|string $object the object to be converted into an array |
||
27 | * @param array $properties a mapping from object class names to the properties that need to put into the resulting arrays. |
||
28 | * The properties specified for each class is an array of the following format: |
||
29 | * |
||
30 | * ```php |
||
31 | * [ |
||
32 | * 'app\models\Post' => [ |
||
33 | * 'id', |
||
34 | * 'title', |
||
35 | * // the key name in array result => property name |
||
36 | * 'createTime' => 'created_at', |
||
37 | * // the key name in array result => anonymous function |
||
38 | * 'length' => function ($post) { |
||
39 | * return strlen($post->content); |
||
40 | * }, |
||
41 | * ], |
||
42 | * ] |
||
43 | * ``` |
||
44 | * |
||
45 | * The result of `ArrayHelper::toArray($post, $properties)` could be like the following: |
||
46 | * |
||
47 | * ```php |
||
48 | * [ |
||
49 | * 'id' => 123, |
||
50 | * 'title' => 'test', |
||
51 | * 'createTime' => '2013-01-01 12:00AM', |
||
52 | * 'length' => 301, |
||
53 | * ] |
||
54 | * ``` |
||
55 | * |
||
56 | * @param boolean $recursive whether to recursively converts properties which are objects into arrays. |
||
57 | * @return array the array representation of the object |
||
58 | */ |
||
59 | 1 | public static function toArray($object, $properties = [], $recursive = true) |
|
101 | |||
102 | /** |
||
103 | * Merges two or more arrays into one recursively. |
||
104 | * If each array has an element with the same string key value, the latter |
||
105 | * will overwrite the former (different from array_merge_recursive). |
||
106 | * Recursive merging will be conducted if both arrays have an element of array |
||
107 | * type and are having the same key. |
||
108 | * For integer-keyed elements, the elements from the latter array will |
||
109 | * be appended to the former array. |
||
110 | * @param array $a array to be merged to |
||
111 | * @param array $b array to be merged from. You can specify additional |
||
112 | * arrays via third argument, fourth argument etc. |
||
113 | * @return array the merged array (the original arrays are not changed.) |
||
114 | */ |
||
115 | 1744 | public static function merge($a, $b) |
|
138 | |||
139 | /** |
||
140 | * Retrieves the value of an array element or object property with the given key or property name. |
||
141 | * If the key does not exist in the array or object, the default value will be returned instead. |
||
142 | * |
||
143 | * The key may be specified in a dot format to retrieve the value of a sub-array or the property |
||
144 | * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
||
145 | * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
||
146 | * or `$array->x` is neither an array nor an object, the default value will be returned. |
||
147 | * Note that if the array already has an element `x.y.z`, then its value will be returned |
||
148 | * instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
||
149 | * like `['x', 'y', 'z']`. |
||
150 | * |
||
151 | * Below are some usage examples, |
||
152 | * |
||
153 | * ```php |
||
154 | * // working with array |
||
155 | * $username = \yii\helpers\ArrayHelper::getValue($_POST, 'username'); |
||
156 | * // working with object |
||
157 | * $username = \yii\helpers\ArrayHelper::getValue($user, 'username'); |
||
158 | * // working with anonymous function |
||
159 | * $fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) { |
||
160 | * return $user->firstName . ' ' . $user->lastName; |
||
161 | * }); |
||
162 | * // using dot format to retrieve the property of embedded object |
||
163 | * $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street'); |
||
164 | * // using an array of keys to retrieve the value |
||
165 | * $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']); |
||
166 | * ``` |
||
167 | * |
||
168 | * @param array|object $array array or object to extract value from |
||
169 | * @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object, |
||
170 | * or an anonymous function returning the value. The anonymous function signature should be: |
||
171 | * `function($array, $defaultValue)`. |
||
172 | * The possibility to pass an array of keys is available since version 2.0.4. |
||
173 | * @param mixed $default the default value to be returned if the specified array key does not exist. Not used when |
||
174 | * getting value from an object. |
||
175 | * @return mixed the value of the element if found, default value otherwise |
||
176 | * @throws InvalidParamException if $array is neither an array nor an object. |
||
177 | */ |
||
178 | 44 | public static function getValue($array, $key, $default = null) |
|
211 | |||
212 | /** |
||
213 | * Removes an item from an array and returns the value. If the key does not exist in the array, the default value |
||
214 | * will be returned instead. |
||
215 | * |
||
216 | * Usage examples, |
||
217 | * |
||
218 | * ```php |
||
219 | * // $array = ['type' => 'A', 'options' => [1, 2]]; |
||
220 | * // working with array |
||
221 | * $type = \yii\helpers\ArrayHelper::remove($array, 'type'); |
||
222 | * // $array content |
||
223 | * // $array = ['options' => [1, 2]]; |
||
224 | * ``` |
||
225 | * |
||
226 | * @param array $array the array to extract value from |
||
227 | * @param string $key key name of the array element |
||
228 | * @param mixed $default the default value to be returned if the specified key does not exist |
||
229 | * @return mixed|null the value of the element if found, default value otherwise |
||
230 | */ |
||
231 | 52 | public static function remove(&$array, $key, $default = null) |
|
242 | |||
243 | /** |
||
244 | * Indexes an array according to a specified key. |
||
245 | * The input array should be multidimensional or an array of objects. |
||
246 | * |
||
247 | * The key can be a key name of the sub-array, a property name of object, or an anonymous |
||
248 | * function which returns the key value given an array element. |
||
249 | * |
||
250 | * If a key value is null, the corresponding array element will be discarded and not put in the result. |
||
251 | * |
||
252 | * For example, |
||
253 | * |
||
254 | * ```php |
||
255 | * $array = [ |
||
256 | * ['id' => '123', 'data' => 'abc'], |
||
257 | * ['id' => '345', 'data' => 'def'], |
||
258 | * ]; |
||
259 | * $result = ArrayHelper::index($array, 'id'); |
||
260 | * // the result is: |
||
261 | * // [ |
||
262 | * // '123' => ['id' => '123', 'data' => 'abc'], |
||
263 | * // '345' => ['id' => '345', 'data' => 'def'], |
||
264 | * // ] |
||
265 | * |
||
266 | * // using anonymous function |
||
267 | * $result = ArrayHelper::index($array, function ($element) { |
||
268 | * return $element['id']; |
||
269 | * }); |
||
270 | * ``` |
||
271 | * |
||
272 | * @param array $array the array that needs to be indexed |
||
273 | * @param string|\Closure $key the column name or anonymous function whose result will be used to index the array |
||
274 | * @return array the indexed array |
||
275 | */ |
||
276 | 1 | public static function index($array, $key) |
|
286 | |||
287 | /** |
||
288 | * Returns the values of a specified column in an array. |
||
289 | * The input array should be multidimensional or an array of objects. |
||
290 | * |
||
291 | * For example, |
||
292 | * |
||
293 | * ```php |
||
294 | * $array = [ |
||
295 | * ['id' => '123', 'data' => 'abc'], |
||
296 | * ['id' => '345', 'data' => 'def'], |
||
297 | * ]; |
||
298 | * $result = ArrayHelper::getColumn($array, 'id'); |
||
299 | * // the result is: ['123', '345'] |
||
300 | * |
||
301 | * // using anonymous function |
||
302 | * $result = ArrayHelper::getColumn($array, function ($element) { |
||
303 | * return $element['id']; |
||
304 | * }); |
||
305 | * ``` |
||
306 | * |
||
307 | * @param array $array |
||
308 | * @param string|\Closure $name |
||
309 | * @param boolean $keepKeys whether to maintain the array keys. If false, the resulting array |
||
310 | * will be re-indexed with integers. |
||
311 | * @return array the list of column values |
||
312 | */ |
||
313 | 6 | public static function getColumn($array, $name, $keepKeys = true) |
|
328 | |||
329 | /** |
||
330 | * Builds a map (key-value pairs) from a multidimensional array or an array of objects. |
||
331 | * The `$from` and `$to` parameters specify the key names or property names to set up the map. |
||
332 | * Optionally, one can further group the map according to a grouping field `$group`. |
||
333 | * |
||
334 | * For example, |
||
335 | * |
||
336 | * ```php |
||
337 | * $array = [ |
||
338 | * ['id' => '123', 'name' => 'aaa', 'class' => 'x'], |
||
339 | * ['id' => '124', 'name' => 'bbb', 'class' => 'x'], |
||
340 | * ['id' => '345', 'name' => 'ccc', 'class' => 'y'], |
||
341 | * ]; |
||
342 | * |
||
343 | * $result = ArrayHelper::map($array, 'id', 'name'); |
||
344 | * // the result is: |
||
345 | * // [ |
||
346 | * // '123' => 'aaa', |
||
347 | * // '124' => 'bbb', |
||
348 | * // '345' => 'ccc', |
||
349 | * // ] |
||
350 | * |
||
351 | * $result = ArrayHelper::map($array, 'id', 'name', 'class'); |
||
352 | * // the result is: |
||
353 | * // [ |
||
354 | * // 'x' => [ |
||
355 | * // '123' => 'aaa', |
||
356 | * // '124' => 'bbb', |
||
357 | * // ], |
||
358 | * // 'y' => [ |
||
359 | * // '345' => 'ccc', |
||
360 | * // ], |
||
361 | * // ] |
||
362 | * ``` |
||
363 | * |
||
364 | * @param array $array |
||
365 | * @param string|\Closure $from |
||
366 | * @param string|\Closure $to |
||
367 | * @param string|\Closure $group |
||
368 | * @return array |
||
369 | */ |
||
370 | 9 | public static function map($array, $from, $to, $group = null) |
|
385 | |||
386 | /** |
||
387 | * Checks if the given array contains the specified key. |
||
388 | * This method enhances the `array_key_exists()` function by supporting case-insensitive |
||
389 | * key comparison. |
||
390 | * @param string $key the key to check |
||
391 | * @param array $array the array with keys to check |
||
392 | * @param boolean $caseSensitive whether the key comparison should be case-sensitive |
||
393 | * @return boolean whether the array contains the specified key |
||
394 | */ |
||
395 | 1 | public static function keyExists($key, $array, $caseSensitive = true) |
|
409 | |||
410 | /** |
||
411 | * Sorts an array of objects or arrays (with the same structure) by one or several keys. |
||
412 | * @param array $array the array to be sorted. The array will be modified after calling this method. |
||
413 | * @param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array |
||
414 | * elements, a property name of the objects, or an anonymous function returning the values for comparison |
||
415 | * purpose. The anonymous function signature should be: `function($item)`. |
||
416 | * To sort by multiple keys, provide an array of keys here. |
||
417 | * @param integer|array $direction the sorting direction. It can be either `SORT_ASC` or `SORT_DESC`. |
||
418 | * When sorting by multiple keys with different sorting directions, use an array of sorting directions. |
||
419 | * @param integer|array $sortFlag the PHP sort flag. Valid values include |
||
420 | * `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`. |
||
421 | * Please refer to [PHP manual](http://php.net/manual/en/function.sort.php) |
||
422 | * for more details. When sorting by multiple keys with different sort flags, use an array of sort flags. |
||
423 | * @throws InvalidParamException if the $direction or $sortFlag parameters do not have |
||
424 | * correct number of elements as that of $key. |
||
425 | */ |
||
426 | 5 | public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR) |
|
453 | |||
454 | /** |
||
455 | * Encodes special characters in an array of strings into HTML entities. |
||
456 | * Only array values will be encoded by default. |
||
457 | * If a value is an array, this method will also encode it recursively. |
||
458 | * Only string values will be encoded. |
||
459 | * @param array $data data to be encoded |
||
460 | * @param boolean $valuesOnly whether to encode array values only. If false, |
||
461 | * both the array keys and array values will be encoded. |
||
462 | * @param string $charset the charset that the data is using. If not set, |
||
463 | * [[\yii\base\Application::charset]] will be used. |
||
464 | * @return array the encoded data |
||
465 | * @see http://www.php.net/manual/en/function.htmlspecialchars.php |
||
466 | */ |
||
467 | 1 | public static function htmlEncode($data, $valuesOnly = true, $charset = null) |
|
488 | |||
489 | /** |
||
490 | * Decodes HTML entities into the corresponding characters in an array of strings. |
||
491 | * Only array values will be decoded by default. |
||
492 | * If a value is an array, this method will also decode it recursively. |
||
493 | * Only string values will be decoded. |
||
494 | * @param array $data data to be decoded |
||
495 | * @param boolean $valuesOnly whether to decode array values only. If false, |
||
496 | * both the array keys and array values will be decoded. |
||
497 | * @return array the decoded data |
||
498 | * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php |
||
499 | */ |
||
500 | 1 | public static function htmlDecode($data, $valuesOnly = true) |
|
518 | |||
519 | /** |
||
520 | * Returns a value indicating whether the given array is an associative array. |
||
521 | * |
||
522 | * An array is associative if all its keys are strings. If `$allStrings` is false, |
||
523 | * then an array will be treated as associative if at least one of its keys is a string. |
||
524 | * |
||
525 | * Note that an empty array will NOT be considered associative. |
||
526 | * |
||
527 | * @param array $array the array being checked |
||
528 | * @param boolean $allStrings whether the array keys must be all strings in order for |
||
529 | * the array to be treated as associative. |
||
530 | * @return boolean whether the array is associative |
||
531 | */ |
||
532 | 116 | public static function isAssociative($array, $allStrings = true) |
|
554 | |||
555 | /** |
||
556 | * Returns a value indicating whether the given array is an indexed array. |
||
557 | * |
||
558 | * An array is indexed if all its keys are integers. If `$consecutive` is true, |
||
559 | * then the array keys must be a consecutive sequence starting from 0. |
||
560 | * |
||
561 | * Note that an empty array will be considered indexed. |
||
562 | * |
||
563 | * @param array $array the array being checked |
||
564 | * @param boolean $consecutive whether the array keys must be a consecutive sequence |
||
565 | * in order for the array to be treated as indexed. |
||
566 | * @return boolean whether the array is associative |
||
567 | */ |
||
568 | 1 | public static function isIndexed($array, $consecutive = false) |
|
589 | |||
590 | /** |
||
591 | * Check whether an array or [[\Traversable]] contains an element. |
||
592 | * |
||
593 | * This method does the same as the PHP function [in_array()](http://php.net/manual/en/function.in-array.php) |
||
594 | * but it does not only work for arrays but also objects that implement the [[\Traversable]] interface. |
||
595 | * @param mixed $needle The value to look for. |
||
596 | * @param array|\Traversable $haystack The set of values to search. |
||
597 | * @param boolean $strict Whether to enable strict (`===`) comparison. |
||
598 | * @return boolean `true` if `$needle` was found in `$haystack`, `false` otherwise. |
||
599 | * @throws \InvalidArgumentException if `$haystack` is neither traversable nor an array. |
||
600 | * @see http://php.net/manual/en/function.in-array.php |
||
601 | */ |
||
602 | 10 | public static function isIn($needle, $haystack, $strict = false) |
|
618 | |||
619 | /** |
||
620 | * Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]]. |
||
621 | * |
||
622 | * This method will return `true`, if all elements of `$needles` are contained in |
||
623 | * `$haystack`. If at least one element is missing, `false` will be returned. |
||
624 | * @param array|\Traversable $needles The values that must **all** be in `$haystack`. |
||
625 | * @param array|\Traversable $haystack The set of value to search. |
||
626 | * @param boolean $strict Whether to enable strict (`===`) comparison. |
||
627 | * @throws \InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array. |
||
628 | * @return boolean `true` if `$needles` is a subset of `$haystack`, `false` otherwise. |
||
629 | */ |
||
630 | 5 | public static function isSubset($needles, $haystack, $strict = false) |
|
643 | } |
||
644 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.