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 bool $recursive whether to recursively converts properties which are objects into arrays. |
||
57 | * @return array the array representation of the object |
||
58 | */ |
||
59 | 11 | 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 | * You can use [[UnsetArrayValue]] object to unset value from previous array or |
||
111 | * [[ReplaceArrayValue]] to force replace former value instead of recursive merging. |
||
112 | * @param array $a array to be merged to |
||
113 | * @param array $b array to be merged from. You can specify additional |
||
114 | * arrays via third argument, fourth argument etc. |
||
115 | * @return array the merged array (the original arrays are not changed.) |
||
116 | */ |
||
117 | 2299 | public static function merge($a, $b) |
|
144 | |||
145 | /** |
||
146 | * Retrieves the value of an array element or object property with the given key or property name. |
||
147 | * If the key does not exist in the array or object, the default value will be returned instead. |
||
148 | * |
||
149 | * The key may be specified in a dot format to retrieve the value of a sub-array or the property |
||
150 | * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
||
151 | * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
||
152 | * or `$array->x` is neither an array nor an object, the default value will be returned. |
||
153 | * Note that if the array already has an element `x.y.z`, then its value will be returned |
||
154 | * instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
||
155 | * like `['x', 'y', 'z']`. |
||
156 | * |
||
157 | * Below are some usage examples, |
||
158 | * |
||
159 | * ```php |
||
160 | * // working with array |
||
161 | * $username = \yii\helpers\ArrayHelper::getValue($_POST, 'username'); |
||
162 | * // working with object |
||
163 | * $username = \yii\helpers\ArrayHelper::getValue($user, 'username'); |
||
164 | * // working with anonymous function |
||
165 | * $fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) { |
||
166 | * return $user->firstName . ' ' . $user->lastName; |
||
167 | * }); |
||
168 | * // using dot format to retrieve the property of embedded object |
||
169 | * $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street'); |
||
170 | * // using an array of keys to retrieve the value |
||
171 | * $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']); |
||
172 | * ``` |
||
173 | * |
||
174 | * @param array|object $array array or object to extract value from |
||
175 | * @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object, |
||
176 | * or an anonymous function returning the value. The anonymous function signature should be: |
||
177 | * `function($array, $defaultValue)`. |
||
178 | * The possibility to pass an array of keys is available since version 2.0.4. |
||
179 | * @param mixed $default the default value to be returned if the specified array key does not exist. Not used when |
||
180 | * getting value from an object. |
||
181 | * @return mixed the value of the element if found, default value otherwise. |
||
182 | * @throws InvalidParamException if $array is neither an array nor an object. |
||
183 | */ |
||
184 | 88 | public static function getValue($array, $key, $default = null) |
|
221 | |||
222 | /** |
||
223 | * Removes an item from an array and returns the value. If the key does not exist in the array, the default value |
||
224 | * will be returned instead. |
||
225 | * |
||
226 | * Usage examples, |
||
227 | * |
||
228 | * ```php |
||
229 | * // $array = ['type' => 'A', 'options' => [1, 2]]; |
||
230 | * // working with array |
||
231 | * $type = \yii\helpers\ArrayHelper::remove($array, 'type'); |
||
232 | * // $array content |
||
233 | * // $array = ['options' => [1, 2]]; |
||
234 | * ``` |
||
235 | * |
||
236 | * @param array $array the array to extract value from |
||
237 | * @param string $key key name of the array element |
||
238 | * @param mixed $default the default value to be returned if the specified key does not exist |
||
239 | * @return mixed|null the value of the element if found, default value otherwise |
||
240 | */ |
||
241 | 116 | public static function remove(&$array, $key, $default = null) |
|
252 | |||
253 | /** |
||
254 | * Removes items with matching values from the array and returns the removed items. |
||
255 | * |
||
256 | * Example, |
||
257 | * |
||
258 | * ```php |
||
259 | * $array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson']; |
||
260 | * $removed = \yii\helpers\ArrayHelper::removeValue($array, 'Jackson'); |
||
261 | * // result: |
||
262 | * // $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger']; |
||
263 | * // $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson']; |
||
264 | * ``` |
||
265 | * |
||
266 | * @param array $array the array where to look the value from |
||
267 | * @param string $value the value to remove from the array |
||
268 | * @return array the items that were removed from the array |
||
269 | * @since 2.0.11 |
||
270 | */ |
||
271 | 2 | public static function removeValue(&$array, $value) |
|
284 | |||
285 | /** |
||
286 | * Indexes and/or groups the array according to a specified key. |
||
287 | * The input should be either multidimensional array or an array of objects. |
||
288 | * |
||
289 | * The $key can be either a key name of the sub-array, a property name of object, or an anonymous |
||
290 | * function that must return the value that will be used as a key. |
||
291 | * |
||
292 | * $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based |
||
293 | * on keys specified. |
||
294 | * |
||
295 | * If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition |
||
296 | * to `$groups` not specified then the element is discarded. |
||
297 | * |
||
298 | * For example: |
||
299 | * |
||
300 | * ```php |
||
301 | * $array = [ |
||
302 | * ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], |
||
303 | * ['id' => '345', 'data' => 'def', 'device' => 'tablet'], |
||
304 | * ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], |
||
305 | * ]; |
||
306 | * $result = ArrayHelper::index($array, 'id'); |
||
307 | * ``` |
||
308 | * |
||
309 | * The result will be an associative array, where the key is the value of `id` attribute |
||
310 | * |
||
311 | * ```php |
||
312 | * [ |
||
313 | * '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], |
||
314 | * '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] |
||
315 | * // The second element of an original array is overwritten by the last element because of the same id |
||
316 | * ] |
||
317 | * ``` |
||
318 | * |
||
319 | * An anonymous function can be used in the grouping array as well. |
||
320 | * |
||
321 | * ```php |
||
322 | * $result = ArrayHelper::index($array, function ($element) { |
||
323 | * return $element['id']; |
||
324 | * }); |
||
325 | * ``` |
||
326 | * |
||
327 | * Passing `id` as a third argument will group `$array` by `id`: |
||
328 | * |
||
329 | * ```php |
||
330 | * $result = ArrayHelper::index($array, null, 'id'); |
||
331 | * ``` |
||
332 | * |
||
333 | * The result will be a multidimensional array grouped by `id` on the first level, by `device` on the second level |
||
334 | * and indexed by `data` on the third level: |
||
335 | * |
||
336 | * ```php |
||
337 | * [ |
||
338 | * '123' => [ |
||
339 | * ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] |
||
340 | * ], |
||
341 | * '345' => [ // all elements with this index are present in the result array |
||
342 | * ['id' => '345', 'data' => 'def', 'device' => 'tablet'], |
||
343 | * ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], |
||
344 | * ] |
||
345 | * ] |
||
346 | * ``` |
||
347 | * |
||
348 | * The anonymous function can be used in the array of grouping keys as well: |
||
349 | * |
||
350 | * ```php |
||
351 | * $result = ArrayHelper::index($array, 'data', [function ($element) { |
||
352 | * return $element['id']; |
||
353 | * }, 'device']); |
||
354 | * ``` |
||
355 | * |
||
356 | * The result will be a multidimensional array grouped by `id` on the first level, by the `device` on the second one |
||
357 | * and indexed by the `data` on the third level: |
||
358 | * |
||
359 | * ```php |
||
360 | * [ |
||
361 | * '123' => [ |
||
362 | * 'laptop' => [ |
||
363 | * 'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] |
||
364 | * ] |
||
365 | * ], |
||
366 | * '345' => [ |
||
367 | * 'tablet' => [ |
||
368 | * 'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet'] |
||
369 | * ], |
||
370 | * 'smartphone' => [ |
||
371 | * 'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] |
||
372 | * ] |
||
373 | * ] |
||
374 | * ] |
||
375 | * ``` |
||
376 | * |
||
377 | * @param array $array the array that needs to be indexed or grouped |
||
378 | * @param string|\Closure|null $key the column name or anonymous function which result will be used to index the array |
||
379 | * @param string|string[]|\Closure[]|null $groups the array of keys, that will be used to group the input array |
||
380 | * by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not |
||
381 | * defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added |
||
382 | * to the result array without any key. This parameter is available since version 2.0.8. |
||
383 | * @return array the indexed and/or grouped array |
||
384 | */ |
||
385 | 3 | public static function index($array, $key, $groups = []) |
|
419 | |||
420 | /** |
||
421 | * Returns the values of a specified column in an array. |
||
422 | * The input array should be multidimensional or an array of objects. |
||
423 | * |
||
424 | * For example, |
||
425 | * |
||
426 | * ```php |
||
427 | * $array = [ |
||
428 | * ['id' => '123', 'data' => 'abc'], |
||
429 | * ['id' => '345', 'data' => 'def'], |
||
430 | * ]; |
||
431 | * $result = ArrayHelper::getColumn($array, 'id'); |
||
432 | * // the result is: ['123', '345'] |
||
433 | * |
||
434 | * // using anonymous function |
||
435 | * $result = ArrayHelper::getColumn($array, function ($element) { |
||
436 | * return $element['id']; |
||
437 | * }); |
||
438 | * ``` |
||
439 | * |
||
440 | * @param array $array |
||
441 | * @param string|\Closure $name |
||
442 | * @param bool $keepKeys whether to maintain the array keys. If false, the resulting array |
||
443 | * will be re-indexed with integers. |
||
444 | * @return array the list of column values |
||
445 | */ |
||
446 | 7 | public static function getColumn($array, $name, $keepKeys = true) |
|
461 | |||
462 | /** |
||
463 | * Builds a map (key-value pairs) from a multidimensional array or an array of objects. |
||
464 | * The `$from` and `$to` parameters specify the key names or property names to set up the map. |
||
465 | * Optionally, one can further group the map according to a grouping field `$group`. |
||
466 | * |
||
467 | * For example, |
||
468 | * |
||
469 | * ```php |
||
470 | * $array = [ |
||
471 | * ['id' => '123', 'name' => 'aaa', 'class' => 'x'], |
||
472 | * ['id' => '124', 'name' => 'bbb', 'class' => 'x'], |
||
473 | * ['id' => '345', 'name' => 'ccc', 'class' => 'y'], |
||
474 | * ]; |
||
475 | * |
||
476 | * $result = ArrayHelper::map($array, 'id', 'name'); |
||
477 | * // the result is: |
||
478 | * // [ |
||
479 | * // '123' => 'aaa', |
||
480 | * // '124' => 'bbb', |
||
481 | * // '345' => 'ccc', |
||
482 | * // ] |
||
483 | * |
||
484 | * $result = ArrayHelper::map($array, 'id', 'name', 'class'); |
||
485 | * // the result is: |
||
486 | * // [ |
||
487 | * // 'x' => [ |
||
488 | * // '123' => 'aaa', |
||
489 | * // '124' => 'bbb', |
||
490 | * // ], |
||
491 | * // 'y' => [ |
||
492 | * // '345' => 'ccc', |
||
493 | * // ], |
||
494 | * // ] |
||
495 | * ``` |
||
496 | * |
||
497 | * @param array $array |
||
498 | * @param string|\Closure $from |
||
499 | * @param string|\Closure $to |
||
500 | * @param string|\Closure $group |
||
501 | * @return array |
||
502 | */ |
||
503 | 23 | public static function map($array, $from, $to, $group = null) |
|
518 | |||
519 | /** |
||
520 | * Checks if the given array contains the specified key. |
||
521 | * This method enhances the `array_key_exists()` function by supporting case-insensitive |
||
522 | * key comparison. |
||
523 | * @param string $key the key to check |
||
524 | * @param array $array the array with keys to check |
||
525 | * @param bool $caseSensitive whether the key comparison should be case-sensitive |
||
526 | * @return bool whether the array contains the specified key |
||
527 | */ |
||
528 | 12 | public static function keyExists($key, $array, $caseSensitive = true) |
|
544 | |||
545 | /** |
||
546 | * Sorts an array of objects or arrays (with the same structure) by one or several keys. |
||
547 | * @param array $array the array to be sorted. The array will be modified after calling this method. |
||
548 | * @param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array |
||
549 | * elements, a property name of the objects, or an anonymous function returning the values for comparison |
||
550 | * purpose. The anonymous function signature should be: `function($item)`. |
||
551 | * To sort by multiple keys, provide an array of keys here. |
||
552 | * @param int|array $direction the sorting direction. It can be either `SORT_ASC` or `SORT_DESC`. |
||
553 | * When sorting by multiple keys with different sorting directions, use an array of sorting directions. |
||
554 | * @param int|array $sortFlag the PHP sort flag. Valid values include |
||
555 | * `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`. |
||
556 | * Please refer to [PHP manual](http://php.net/manual/en/function.sort.php) |
||
557 | * for more details. When sorting by multiple keys with different sort flags, use an array of sort flags. |
||
558 | * @throws InvalidParamException if the $direction or $sortFlag parameters do not have |
||
559 | * correct number of elements as that of $key. |
||
560 | */ |
||
561 | 6 | public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR) |
|
595 | |||
596 | /** |
||
597 | * Encodes special characters in an array of strings into HTML entities. |
||
598 | * Only array values will be encoded by default. |
||
599 | * If a value is an array, this method will also encode it recursively. |
||
600 | * Only string values will be encoded. |
||
601 | * @param array $data data to be encoded |
||
602 | * @param bool $valuesOnly whether to encode array values only. If false, |
||
603 | * both the array keys and array values will be encoded. |
||
604 | * @param string $charset the charset that the data is using. If not set, |
||
605 | * [[\yii\base\Application::charset]] will be used. |
||
606 | * @return array the encoded data |
||
607 | * @see http://www.php.net/manual/en/function.htmlspecialchars.php |
||
608 | */ |
||
609 | 1 | public static function htmlEncode($data, $valuesOnly = true, $charset = null) |
|
630 | |||
631 | /** |
||
632 | * Decodes HTML entities into the corresponding characters in an array of strings. |
||
633 | * Only array values will be decoded by default. |
||
634 | * If a value is an array, this method will also decode it recursively. |
||
635 | * Only string values will be decoded. |
||
636 | * @param array $data data to be decoded |
||
637 | * @param bool $valuesOnly whether to decode array values only. If false, |
||
638 | * both the array keys and array values will be decoded. |
||
639 | * @return array the decoded data |
||
640 | * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php |
||
641 | */ |
||
642 | 1 | public static function htmlDecode($data, $valuesOnly = true) |
|
660 | |||
661 | /** |
||
662 | * Returns a value indicating whether the given array is an associative array. |
||
663 | * |
||
664 | * An array is associative if all its keys are strings. If `$allStrings` is false, |
||
665 | * then an array will be treated as associative if at least one of its keys is a string. |
||
666 | * |
||
667 | * Note that an empty array will NOT be considered associative. |
||
668 | * |
||
669 | * @param array $array the array being checked |
||
670 | * @param bool $allStrings whether the array keys must be all strings in order for |
||
671 | * the array to be treated as associative. |
||
672 | * @return bool whether the array is associative |
||
673 | */ |
||
674 | 168 | public static function isAssociative($array, $allStrings = true) |
|
696 | |||
697 | /** |
||
698 | * Returns a value indicating whether the given array is an indexed array. |
||
699 | * |
||
700 | * An array is indexed if all its keys are integers. If `$consecutive` is true, |
||
701 | * then the array keys must be a consecutive sequence starting from 0. |
||
702 | * |
||
703 | * Note that an empty array will be considered indexed. |
||
704 | * |
||
705 | * @param array $array the array being checked |
||
706 | * @param bool $consecutive whether the array keys must be a consecutive sequence |
||
707 | * in order for the array to be treated as indexed. |
||
708 | * @return bool whether the array is associative |
||
709 | */ |
||
710 | 1 | public static function isIndexed($array, $consecutive = false) |
|
731 | |||
732 | /** |
||
733 | * Check whether an array or [[\Traversable]] contains an element. |
||
734 | * |
||
735 | * This method does the same as the PHP function [in_array()](http://php.net/manual/en/function.in-array.php) |
||
736 | * but additionally works for objects that implement the [[\Traversable]] interface. |
||
737 | * @param mixed $needle The value to look for. |
||
738 | * @param array|\Traversable $haystack The set of values to search. |
||
739 | * @param bool $strict Whether to enable strict (`===`) comparison. |
||
740 | * @return bool `true` if `$needle` was found in `$haystack`, `false` otherwise. |
||
741 | * @throws InvalidParamException if `$haystack` is neither traversable nor an array. |
||
742 | * @see http://php.net/manual/en/function.in-array.php |
||
743 | * @since 2.0.7 |
||
744 | */ |
||
745 | 16 | public static function isIn($needle, $haystack, $strict = false) |
|
761 | |||
762 | /** |
||
763 | * Checks whether a variable is an array or [[\Traversable]]. |
||
764 | * |
||
765 | * This method does the same as the PHP function [is_array()](http://php.net/manual/en/function.is-array.php) |
||
766 | * but additionally works on objects that implement the [[\Traversable]] interface. |
||
767 | * @param mixed $var The variable being evaluated. |
||
768 | * @return bool whether $var is array-like |
||
769 | * @see http://php.net/manual/en/function.is_array.php |
||
770 | * @since 2.0.8 |
||
771 | */ |
||
772 | 406 | public static function isTraversable($var) |
|
776 | |||
777 | /** |
||
778 | * Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]]. |
||
779 | * |
||
780 | * This method will return `true`, if all elements of `$needles` are contained in |
||
781 | * `$haystack`. If at least one element is missing, `false` will be returned. |
||
782 | * @param array|\Traversable $needles The values that must **all** be in `$haystack`. |
||
783 | * @param array|\Traversable $haystack The set of value to search. |
||
784 | * @param bool $strict Whether to enable strict (`===`) comparison. |
||
785 | * @throws InvalidParamException if `$haystack` or `$needles` is neither traversable nor an array. |
||
786 | * @return bool `true` if `$needles` is a subset of `$haystack`, `false` otherwise. |
||
787 | * @since 2.0.7 |
||
788 | */ |
||
789 | 6 | public static function isSubset($needles, $haystack, $strict = false) |
|
802 | |||
803 | /** |
||
804 | * Filters array according to rules specified. |
||
805 | * |
||
806 | * For example: |
||
807 | * |
||
808 | * ```php |
||
809 | * $array = [ |
||
810 | * 'A' => [1, 2], |
||
811 | * 'B' => [ |
||
812 | * 'C' => 1, |
||
813 | * 'D' => 2, |
||
814 | * ], |
||
815 | * 'E' => 1, |
||
816 | * ]; |
||
817 | * |
||
818 | * $result = \yii\helpers\ArrayHelper::filter($array, ['A']); |
||
819 | * // $result will be: |
||
820 | * // [ |
||
821 | * // 'A' => [1, 2], |
||
822 | * // ] |
||
823 | * |
||
824 | * $result = \yii\helpers\ArrayHelper::filter($array, ['A', 'B.C']); |
||
825 | * // $result will be: |
||
826 | * // [ |
||
827 | * // 'A' => [1, 2], |
||
828 | * // 'B' => ['C' => 1], |
||
829 | * // ] |
||
830 | * |
||
831 | * $result = \yii\helpers\ArrayHelper::filter($array, ['B', '!B.C']); |
||
832 | * // $result will be: |
||
833 | * // [ |
||
834 | * // 'B' => ['D' => 2], |
||
835 | * // ] |
||
836 | * ``` |
||
837 | * |
||
838 | * @param array $array Source array |
||
839 | * @param array $filters Rules that define array keys which should be left or removed from results. |
||
840 | * Each rule is: |
||
841 | * - `var` - `$array['var']` will be left in result. |
||
842 | * - `var.key` = only `$array['var']['key'] will be left in result. |
||
843 | * - `!var.key` = `$array['var']['key'] will be removed from result. |
||
844 | * @return array Filtered array |
||
845 | * @since 2.0.9 |
||
846 | */ |
||
847 | 24 | public static function filter($array, $filters) |
|
890 | } |
||
891 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.