Completed
Push — master ( 9cf391...4a311c )
by lan tian
8s
created

Util::operateArray()   C

Complexity

Conditions 14
Paths 16

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 5.1379
cc 14
eloc 16
nc 16
nop 8

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Toplan\PhpSms;
4
5
class Util
6
{
7
    /**
8
     * 对数组进行赋值/取值操作
9
     *
10
     * @param array         $arr
11
     * @param mixed         $key
12
     * @param mixed         $value
13
     * @param mixed         $getDefault
14
     * @param \Closure|null $setAction
15
     * @param bool          $override
16
     * @param \Closure|null $whenOverride
17
     * @param bool          $isSet
18
     *
19
     * @return mixed
20
     */
21
    public static function operateArray(array &$arr, $key, $value = null, $getDefault = null, \Closure $setAction = null, $override = false, $whenOverride = null, $isSet = false)
22
    {
23
        if (!$isSet && ($key === null || is_string($key) || is_int($key)) && $value === null) {
24
            return $key === null ? $arr :
25
                (isset($arr[$key]) ? $arr[$key] : $getDefault);
26
        }
27
        if ($override) {
28
            if (is_callable($whenOverride)) {
29
                call_user_func_array($whenOverride, [$arr]);
30
            }
31
            $arr = [];
32
        }
33
        if (is_array($key) || is_object($key)) {
34
            foreach ($key as $k => $v) {
35
                self::operateArray($arr, $k, $v, $getDefault, $setAction, false, null, true);
36
            }
37
        } elseif (is_callable($setAction)) {
38
            call_user_func_array($setAction, [$key, $value]);
39
        } else {
40
            $arr[$key] = $value;
41
        }
42
43
        return $arr;
44
    }
45
46
    /**
47
     * Pull the value from the specified array by key.
48
     *
49
     * @param array      $options
50
     * @param int|string $key
51
     *
52
     * @return mixed
53
     */
54
    public static function pullFromArrayByKey(array &$options, $key)
55
    {
56
        if (!isset($options[$key])) {
57
            return;
58
        }
59
        $value = $options[$key];
60
        unset($options[$key]);
61
62
        return $value;
63
    }
64
}
65