Total Complexity | 45 |
Total Lines | 203 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Utils 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 Utils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | final class Utils |
||
37 | { |
||
38 | /** |
||
39 | * @codeCoverageIgnore |
||
40 | */ |
||
41 | private function __construct() |
||
42 | { |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Turns a byte value into a human-readable representation. |
||
47 | * |
||
48 | * @param int $value Amount of bytes |
||
49 | * |
||
50 | * @return array Human readable value and unit |
||
51 | */ |
||
52 | public static function getHumanReadableBytes($value) |
||
53 | { |
||
54 | static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); |
||
55 | |||
56 | $i = \floor(\log($value, 1024)); |
||
57 | $i = \min($i, 4); // Only go up to TB |
||
58 | |||
59 | return array( |
||
60 | 'value' => (float) ($value / \pow(1024, $i)), |
||
61 | 'unit' => $unit[$i], |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | public static function isSequential(array $array) |
||
66 | { |
||
67 | return \array_keys($array) === \range(0, \count($array) - 1); |
||
68 | } |
||
69 | |||
70 | public static function composerGetExtras($key = 'kint') |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @codeCoverageIgnore |
||
114 | */ |
||
115 | public static function composerSkipFlags() |
||
116 | { |
||
117 | $extras = self::composerGetExtras(); |
||
118 | |||
119 | if (!empty($extras['disable-facade']) && !\defined('KINT_SKIP_FACADE')) { |
||
120 | \define('KINT_SKIP_FACADE', true); |
||
121 | } |
||
122 | |||
123 | if (!empty($extras['disable-helpers']) && !\defined('KINT_SKIP_HELPERS')) { |
||
124 | \define('KINT_SKIP_HELPERS', true); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | public static function isTrace(array $trace) |
||
129 | { |
||
130 | if (!self::isSequential($trace)) { |
||
131 | return false; |
||
132 | } |
||
133 | |||
134 | static $bt_structure = array( |
||
135 | 'function' => 'string', |
||
136 | 'line' => 'integer', |
||
137 | 'file' => 'string', |
||
138 | 'class' => 'string', |
||
139 | 'object' => 'object', |
||
140 | 'type' => 'string', |
||
141 | 'args' => 'array', |
||
142 | ); |
||
143 | |||
144 | $file_found = false; |
||
145 | |||
146 | foreach ($trace as $frame) { |
||
147 | if (!\is_array($frame) || !isset($frame['function'])) { |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | foreach ($frame as $key => $val) { |
||
152 | if (!isset($bt_structure[$key])) { |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | if (\gettype($val) !== $bt_structure[$key]) { |
||
157 | return false; |
||
158 | } |
||
159 | |||
160 | if ('file' === $key) { |
||
161 | $file_found = true; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | return $file_found; |
||
167 | } |
||
168 | |||
169 | public static function traceFrameIsListed(array $frame, array $matches) |
||
170 | { |
||
171 | if (isset($frame['class'])) { |
||
172 | $called = array(\strtolower($frame['class']), \strtolower($frame['function'])); |
||
173 | } else { |
||
174 | $called = \strtolower($frame['function']); |
||
175 | } |
||
176 | |||
177 | return \in_array($called, $matches, true); |
||
178 | } |
||
179 | |||
180 | public static function normalizeAliases(array &$aliases) |
||
181 | { |
||
182 | static $name_regex = '[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*'; |
||
183 | |||
184 | foreach ($aliases as $index => &$alias) { |
||
185 | if (\is_array($alias) && 2 === \count($alias)) { |
||
186 | $alias = \array_values(\array_filter($alias, 'is_string')); |
||
187 | |||
188 | if (2 === \count($alias) && |
||
189 | \preg_match('/^'.$name_regex.'$/', $alias[1]) && |
||
190 | \preg_match('/^\\\\?('.$name_regex.'\\\\)*'.$name_regex.'$/', $alias[0]) |
||
191 | ) { |
||
192 | $alias = array( |
||
193 | \strtolower(\ltrim($alias[0], '\\')), |
||
194 | \strtolower($alias[1]), |
||
195 | ); |
||
196 | } else { |
||
197 | unset($aliases[$index]); |
||
198 | continue; |
||
199 | } |
||
200 | } elseif (\is_string($alias)) { |
||
201 | if (\preg_match('/^\\\\?('.$name_regex.'\\\\)*'.$name_regex.'$/', $alias)) { |
||
202 | $alias = \explode('\\', \strtolower($alias)); |
||
203 | $alias = \end($alias); |
||
204 | } else { |
||
205 | unset($aliases[$index]); |
||
206 | continue; |
||
207 | } |
||
208 | } else { |
||
209 | unset($aliases[$index]); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | $aliases = \array_values($aliases); |
||
214 | } |
||
215 | |||
216 | public static function truncateString($input, $length = PHP_INT_MAX, $end = '...', $encoding = false) |
||
230 | } |
||
231 | |||
232 | public static function getTypeString(ReflectionType $type) |
||
233 | { |
||
234 | if ($type instanceof ReflectionNamedType) { |
||
239 | } |
||
240 | } |
||
241 |