Total Complexity | 43 |
Total Lines | 264 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | final class Util |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Comprueba y devuelve un directorio temporal válido |
||
39 | * |
||
40 | * @return bool|string |
||
41 | */ |
||
42 | public static function getTempDir() |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Realiza el proceso de logout. |
||
71 | * |
||
72 | * FIXME |
||
73 | */ |
||
74 | public static function logout() |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Obtener el tamaño máximo de subida de PHP. |
||
81 | */ |
||
82 | public static function getMaxUpload(): int |
||
83 | { |
||
84 | return min(self::convertShortUnit(ini_get('upload_max_filesize')), |
||
85 | self::convertShortUnit(ini_get('post_max_size')), |
||
86 | self::convertShortUnit(ini_get('memory_limit'))); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param $value |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | public static function convertShortUnit($value): int |
||
95 | { |
||
96 | if (preg_match('/(\d+)(\w+)/', $value, $match)) { |
||
97 | switch (strtoupper($match[2])) { |
||
98 | case 'K': |
||
99 | return (int)$match[1] * 1024; |
||
100 | case 'M': |
||
101 | return (int)$match[1] * pow(1024, 2); |
||
102 | case 'G': |
||
103 | return (int)$match[1] * pow(1024, 3); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return (int)$value; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Checks a variable to see if it should be considered a boolean true or false. |
||
112 | * Also takes into account some text-based representations of true of false, |
||
113 | * such as 'false','N','yes','on','off', etc. |
||
114 | * |
||
115 | * @author Samuel Levy <[email protected]> |
||
116 | * |
||
117 | * @param mixed $in The variable to check |
||
118 | * @param bool $strict If set to false, consider everything that is not false to |
||
119 | * be true. |
||
120 | * |
||
121 | * @return bool The boolean equivalent or null (if strict, and no exact equivalent) |
||
122 | */ |
||
123 | public static function boolval($in, $strict = false) |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Cast an object to another class, keeping the properties, but changing the methods |
||
147 | * |
||
148 | * @param string $dstClass Destination class name |
||
149 | * @param string|object $serialized |
||
150 | * @param string $srcClass Old class name for removing from private methods |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public static function unserialize($dstClass, $serialized, $srcClass = null) |
||
155 | { |
||
156 | if (!is_object($serialized)) { |
||
157 | $match = preg_match_all('/O:\d+:"(?P<class>[^"]++)"/', $serialized, $matches); |
||
158 | |||
159 | $process = false; |
||
160 | |||
161 | if ($match) { |
||
162 | foreach ($matches['class'] as $class) { |
||
163 | if (!class_exists($class) |
||
164 | || $class !== $dstClass |
||
165 | ) { |
||
166 | $process = true; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if ($process === false) { |
||
171 | return unserialize($serialized); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | // Serialized data needs to be processed to change the class name |
||
176 | if ($process === true) { |
||
177 | // If source class is set, it will try to clean up the class name from private methods |
||
178 | if ($srcClass !== null) { |
||
179 | $serialized = preg_replace_callback( |
||
180 | '/:\d+:"\x00' . preg_quote($srcClass, '/') . '\x00(\w+)"/', |
||
181 | function ($matches) { |
||
182 | return ':' . strlen($matches[1]) . ':"' . $matches[1] . '"'; |
||
183 | }, |
||
184 | $serialized); |
||
185 | } |
||
186 | |||
187 | return self::castToClass($serialized, $dstClass); |
||
188 | } |
||
189 | |||
190 | if (preg_match('/a:\d+:{/', $serialized)) { |
||
191 | return unserialize($serialized); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | return $serialized; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Cast an object to another class |
||
200 | * |
||
201 | * @param $cast |
||
202 | * @param $class |
||
203 | * |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public static function castToClass($cast, $class) |
||
207 | { |
||
208 | // TODO: should avoid '__PHP_Incomplete_Class'? |
||
209 | |||
210 | $cast = is_object($cast) ? serialize($cast) : $cast; |
||
211 | |||
212 | return unserialize(preg_replace('/O:\d+:"[^"]++"/', 'O:' . strlen($class) . ':"' . $class . '"', $cast)); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Bloquear la aplicación |
||
217 | * |
||
218 | * @param int $userId |
||
219 | * @param string $subject |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public static function lockApp($userId, $subject) |
||
224 | { |
||
225 | $data = ['time' => time(), 'userId' => (int)$userId, 'subject' => $subject]; |
||
226 | |||
227 | return file_put_contents(LOCK_FILE, json_encode($data)); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Desbloquear la aplicación |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public static function unlockApp() |
||
236 | { |
||
237 | return @unlink(LOCK_FILE); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Comprueba si la aplicación está bloqueada |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | public static function getAppLock() |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Devolver el tiempo aproximado en segundos de una operación |
||
258 | * |
||
259 | * @param $startTime |
||
260 | * @param $numItems |
||
261 | * @param $totalItems |
||
262 | * |
||
263 | * @return array Con el tiempo estimado y los elementos por segundo |
||
264 | */ |
||
265 | public static function getETA($startTime, $numItems, $totalItems) |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Adaptador para convertir una cadena de IDs a un array |
||
279 | * |
||
280 | * @param string $itemsId |
||
281 | * @param string $delimiter |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public static function itemsIdAdapter(string $itemsId, $delimiter = ','): array |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @return int |
||
294 | */ |
||
295 | public static function getMaxDownloadChunk(): int |
||
298 | } |
||
299 | } |
||
300 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.