Total Complexity | 53 |
Total Lines | 232 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ChooseRowsEtc 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 ChooseRowsEtc, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ChooseRowsEtc |
||
9 | { |
||
10 | /** |
||
11 | * Transpose 2-dimensional array. |
||
12 | * See https://stackoverflow.com/questions/797251/transposing-multidimensional-arrays-in-php |
||
13 | * especially the comment from user17994717. |
||
14 | * |
||
15 | * @param mixed[] $array |
||
16 | * |
||
17 | * @return mixed[] |
||
18 | */ |
||
19 | public static function transpose(array $array): array |
||
22 | } |
||
23 | |||
24 | /** @return mixed[] */ |
||
25 | private static function arrayValues(mixed $array): array |
||
26 | { |
||
27 | return is_array($array) ? array_values($array) : [$array]; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * CHOOSECOLS. |
||
32 | * |
||
33 | * @param mixed $input expecting two-dimensional array |
||
34 | * |
||
35 | * @return mixed[]|string |
||
36 | */ |
||
37 | public static function chooseCols(mixed $input, mixed ...$args): array|string |
||
38 | { |
||
39 | if (!is_array($input)) { |
||
40 | $input = [[$input]]; |
||
41 | } |
||
42 | $retval = self::chooseRows(self::transpose($input), ...$args); |
||
43 | |||
44 | return is_array($retval) ? self::transpose($retval) : $retval; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * CHOOSEROWS. |
||
49 | * |
||
50 | * @param mixed $input expecting two-dimensional array |
||
51 | * |
||
52 | * @return mixed[]|string |
||
53 | */ |
||
54 | public static function chooseRows(mixed $input, mixed ...$args): array|string |
||
81 | } |
||
82 | |||
83 | private static function dropRows(array $array, mixed $offset): array|string |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * DROP. |
||
110 | * |
||
111 | * @param mixed $input expect two-dimensional array |
||
112 | * |
||
113 | * @return mixed[]|string |
||
114 | */ |
||
115 | public static function drop(mixed $input, mixed $rows = null, mixed $columns = null): array|string |
||
135 | } |
||
136 | |||
137 | private static function takeRows(array $array, mixed $offset): array|string |
||
138 | { |
||
139 | if ($offset === null) { |
||
140 | return $array; |
||
141 | } |
||
142 | if (!is_numeric($offset)) { |
||
143 | return ExcelError::VALUE(); |
||
144 | } |
||
145 | $offset = (int) $offset; |
||
146 | if ($offset === 0) { |
||
147 | // should be #CALC! - see above |
||
148 | return ExcelError::VALUE(); |
||
149 | } |
||
150 | $count = count($array); |
||
151 | if (abs($offset) >= $count) { |
||
152 | return $array; |
||
153 | } |
||
154 | if ($offset > 0) { |
||
155 | return array_slice($array, 0, $offset); |
||
156 | } |
||
157 | |||
158 | return array_slice($array, $count + $offset); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * TAKE. |
||
163 | * |
||
164 | * @param mixed $input expecting two-dimensional array |
||
165 | * |
||
166 | * @return mixed[]|string |
||
167 | */ |
||
168 | public static function take(mixed $input, mixed $rows, mixed $columns = null): array|string |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * EXPAND. |
||
195 | * |
||
196 | * @param mixed $input expecting two-dimensional array |
||
197 | * |
||
198 | * @return mixed[]|string |
||
199 | */ |
||
200 | public static function expand(mixed $input, mixed $rows, mixed $columns = null, mixed $pad = '#N/A'): array|string |
||
242 |