Changers   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 43
lcom 0
cbo 0
dl 0
loc 190
rs 8.3157
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addConfigRow() 0 15 3
C castColumns() 0 45 15
B deleteColumns() 0 19 5
D insertSubArray() 0 28 9
D setColumn() 0 26 10
A transpose() 0 7 1

How to fix   Complexity   

Complex Class

Complex classes like Changers 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 Changers, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace SimpleArrayLibrary\Categories;
4
5
use InvalidArgumentException;
6
use UnexpectedValueException;
7
8
trait Changers
9
{
10
    /**
11
     * @param array $config
12
     * @param array $keys
13
     * @param mixed $value
14
     *
15
     * @return array
16
     */
17
    public static function addConfigRow(array $config, array $keys, $value)
18
    {
19
        // validation
20
        if (self::isAssociative($keys)) {
21
            throw new UnexpectedValueException('Array of config keys must be numeric');
22
        }
23
24
        $row = $value;
25
        for ($i = count($keys) - 1; $i >= 0; $i--) {
26
            $row = [$keys[$i] => $row];
27
        }
28
        $config = self::insertSubArray($config, $row);
29
30
        return $config;
31
    }
32
33
    /**
34
     * @param array $matrix
35
     * @param array $castMap
36
     * @param bool  $allKeysMustBePresent
37
     *
38
     * @return array
39
     */
40
    public static function castColumns(array $matrix, array $castMap, $allKeysMustBePresent = true)
41
    {
42
        if (!is_bool($allKeysMustBePresent)) {
43
            throw new InvalidArgumentException('Third parameter must be a boolean');
44
        }
45
        if (empty($matrix)) {
46
            return $matrix;
47
        }
48
        if (self::countMinDepth($matrix) < 2) {
49
            throw new UnexpectedValueException('Can not cast columns on one dimensional array');
50
        }
51
52
        foreach ($matrix as $key => $row) {
53
            foreach ($castMap as $column => $type) {
54
                if (isset($row[$column]) || array_key_exists($column, $row)) {
55
                    switch ($type) {
56
                        case self::TYPE_INT:
57
                            $matrix[$key][$column] = (int)$row[$column];
58
                            break;
59
                        case self::TYPE_STRING:
60
                            $matrix[$key][$column] = (string)$row[$column];
61
                            break;
62
                        case self::TYPE_FLOAT:
63
                            $matrix[$key][$column] = (float)$row[$column];
64
                            break;
65
                        case self::TYPE_BOOL:
66
                            $matrix[$key][$column] = (bool)$row[$column];
67
                            break;
68
                        case self::TYPE_ARRAY:
69
                            $matrix[$key][$column] = (array)$row[$column];
70
                            break;
71
                        case self::TYPE_OBJECT:
72
                            $matrix[$key][$column] = (object)$row[$column];
73
                            break;
74
                        default:
75
                            throw new UnexpectedValueException('Invalid type: ' . $type);
76
                    }
77
                } elseif ($allKeysMustBePresent) {
78
                    throw new UnexpectedValueException('Column: ' . $column . ' missing in row: ' . $key);
79
                }
80
            }
81
        }
82
83
        return $matrix;
84
    }
85
86
    /**
87
     * @param array $matrix
88
     * @param mixed $columns
89
     *
90
     * @return array
91
     */
92
    public static function deleteColumns(array $matrix, array $columns)
93
    {
94
        // validate input
95
        if (self::countMinDepth($matrix) < 2) {
96
            throw new UnexpectedValueException('Can not delete columns on one dimensional array');
97
        }
98
        if (self::countMinDepth($columns) != 1) {
99
            throw new InvalidArgumentException('Invalid column');
100
        }
101
102
        // remove columns in every row
103
        foreach ($matrix as $key => $row) {
104
            foreach ($columns as $column) {
105
                unset($matrix[$key][$column]);
106
            }
107
        }
108
109
        return $matrix;
110
    }
111
112
    /**
113
     * @param mixed $array
114
     * @param mixed $subArray
115
     * @param bool  $overwrite
116
     * @param bool  $ignoreIfExists
117
     *
118
     * @return array
119
     */
120
    public static function insertSubArray($array, $subArray, $overwrite = false, $ignoreIfExists = false)
121
    {
122
        // validate
123
        if (!is_bool($overwrite)) {
124
            throw new InvalidArgumentException('Overwrite indicator must be a boolean');
125
        }
126
        if (!is_bool($ignoreIfExists)) {
127
            throw new InvalidArgumentException('Ignore if exists indicator must be a boolean');
128
        }
129
130
        if (!is_array($subArray) || !is_array($array)) {
131
            // $subArray[$key] is leaf of the array
132
            if ($overwrite) {
133
                $array = $subArray;
134
            } elseif (!$ignoreIfExists) {
135
                throw new UnexpectedValueException('Sub-array already exists');
136
            }
137
        } else {
138
            $key = key($subArray);
139
            if (isset($array[$key]) || array_key_exists($key, $array)) {
140
                $array[$key] = self::insertSubArray($array[$key], $subArray[$key], $overwrite, $ignoreIfExists);
141
            } else {
142
                $array[$key] = $subArray[$key];
143
            }
144
        }
145
146
        return $array;
147
    }
148
149
    /**
150
     * @param array $matrix
151
     * @param mixed $column
152
     * @param mixed $value
153
     * @param bool  $insertIfMissing
154
     * @param bool  $overwrite
155
     *
156
     * @return array
157
     */
158
    public static function setColumn(array $matrix, $column, $value, $insertIfMissing = true, $overwrite = true)
159
    {
160
        // validate input
161
        if (self::countMinDepth($matrix) < 2) {
162
            throw new UnexpectedValueException('Can not set columns on one dimensional array');
163
        }
164
        if (!is_scalar($column)) {
165
            throw new InvalidArgumentException('Invalid column');
166
        }
167
        if (!is_bool($insertIfMissing)) {
168
            throw new InvalidArgumentException('Insert if missing indicator must be a boolean');
169
        }
170
        if (!is_bool($overwrite)) {
171
            throw new InvalidArgumentException('Overwrite indicator must be a boolean');
172
        }
173
174
        foreach ($matrix as $key => $row) {
175
            if (isset($row[$column]) || array_key_exists($column, $row)) {
176
                $matrix[$key][$column] = ($overwrite ? $value : $matrix[$key][$column]);
177
            } elseif ($insertIfMissing) {
178
                $matrix[$key][$column] = $value;
179
            }
180
        }
181
182
        return $matrix;
183
    }
184
185
    /**
186
     * @param array $matrix
187
     *
188
     * @return array
189
     */
190
    public static function transpose(array $matrix)
191
    {
192
        // example #4 explains how this works
193
        // http://php.net/manual/en/function.array-map.php
194
        array_unshift($matrix, null);
195
        return call_user_func_array('array_map', $matrix);
196
    }
197
}