| Total Complexity | 41 |
| Total Lines | 267 |
| 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 |
||
| 35 | final class Util |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Comprueba y devuelve un directorio temporal válido |
||
| 40 | * |
||
| 41 | * @return bool|string |
||
| 42 | */ |
||
| 43 | public static function getTempDir() |
||
| 44 | { |
||
| 45 | $sysTmp = sys_get_temp_dir(); |
||
| 46 | $file = 'syspass.test'; |
||
| 47 | |||
| 48 | $checkDir = function ($dir) use ($file) { |
||
| 49 | if (file_exists($dir . DIRECTORY_SEPARATOR . $file)) { |
||
| 50 | return $dir; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (is_dir($dir) || @mkdir($dir)) { |
||
| 54 | if (touch($dir . DIRECTORY_SEPARATOR . $file)) { |
||
| 55 | return $dir; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | return false; |
||
| 60 | }; |
||
| 61 | |||
| 62 | if ($checkDir(TMP_PATH)) { |
||
| 63 | return TMP_PATH; |
||
| 64 | } |
||
| 65 | |||
| 66 | return $checkDir($sysTmp); |
||
| 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) |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Bloquear la aplicación |
||
| 217 | * |
||
| 218 | * @param int $userId |
||
| 219 | * @param string $subject |
||
| 220 | * |
||
| 221 | * @throws \SP\Storage\File\FileException |
||
| 222 | */ |
||
| 223 | public static function lockApp($userId, $subject) |
||
| 224 | { |
||
| 225 | $data = ['time' => time(), 'userId' => (int)$userId, 'subject' => $subject]; |
||
| 226 | |||
| 227 | $file = new FileHandler(LOCK_FILE); |
||
| 228 | $file->save(json_encode($data)); |
||
| 229 | |||
| 230 | logger('Application locked out'); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Desbloquear la aplicación |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public static function unlockApp() |
||
| 239 | { |
||
| 240 | logger('Application unlocked'); |
||
| 241 | |||
| 242 | return @unlink(LOCK_FILE); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Comprueba si la aplicación está bloqueada |
||
| 247 | * |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | public static function getAppLock() |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Devolver el tiempo aproximado en segundos de una operación |
||
| 262 | * |
||
| 263 | * @param $startTime |
||
| 264 | * @param $numItems |
||
| 265 | * @param $totalItems |
||
| 266 | * |
||
| 267 | * @return array Con el tiempo estimado y los elementos por segundo |
||
| 268 | */ |
||
| 269 | public static function getETA($startTime, $numItems, $totalItems) |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Adaptador para convertir una cadena de IDs a un array |
||
| 283 | * |
||
| 284 | * @param string $itemsId |
||
| 285 | * @param string $delimiter |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public static function itemsIdAdapter(string $itemsId, $delimiter = ','): array |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return int |
||
| 298 | */ |
||
| 299 | public static function getMaxDownloadChunk(): int |
||
| 304 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.