| Total Complexity | 52 |
| Total Lines | 315 |
| 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 | * Generar una clave aleatoria |
||
| 39 | * |
||
| 40 | * @param int $length Longitud de la clave |
||
| 41 | * @param bool $useNumbers Usar números |
||
| 42 | * @param bool $useSpecial Usar carácteres especiales |
||
| 43 | * @param bool $checKStrength |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public static function randomPassword($length = 16, $useNumbers = true, $useSpecial = true, $checKStrength = true) |
||
| 48 | { |
||
| 49 | $charsLower = 'abcdefghijklmnopqrstuwxyz'; |
||
| 50 | $charsUpper = 'ABCDEFGHIJKLMNOPQRSTUWXYZ'; |
||
| 51 | |||
| 52 | $alphabet = $charsLower . $charsUpper; |
||
| 53 | |||
| 54 | if ($useSpecial === true) { |
||
| 55 | $charsSpecial = '@$%&/()!_:.;{}^'; |
||
| 56 | $alphabet .= $charsSpecial; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($useNumbers === true) { |
||
| 60 | $charsNumbers = '0123456789'; |
||
| 61 | $alphabet .= $charsNumbers; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | $passGen = function () use ($alphabet, $length) { |
||
| 68 | $pass = []; |
||
| 69 | $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache |
||
| 70 | |||
| 71 | for ($i = 0; $i < $length; $i++) { |
||
| 72 | $n = mt_rand(0, $alphaLength); |
||
| 73 | $pass[] = $alphabet[$n]; |
||
| 74 | } |
||
| 75 | |||
| 76 | return $pass; |
||
| 77 | }; |
||
| 78 | |||
| 79 | if ($checKStrength === true) { |
||
| 80 | do { |
||
| 81 | $pass = $passGen(); |
||
| 82 | $strength = ['lower' => 0, 'upper' => 0, 'special' => 0, 'number' => 0]; |
||
| 83 | |||
| 84 | foreach ($pass as $char) { |
||
| 85 | if (strpos($charsLower, $char) !== false) { |
||
| 86 | $strength['lower']++; |
||
| 87 | } elseif (strpos($charsUpper, $char) !== false) { |
||
| 88 | $strength['upper']++; |
||
| 89 | } elseif ($useSpecial === true && strpos($charsSpecial, $char) !== false) { |
||
|
|
|||
| 90 | $strength['special']++; |
||
| 91 | } elseif ($useNumbers === true && strpos($charsNumbers, $char) !== false) { |
||
| 92 | $strength['number']++; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($useSpecial === false) { |
||
| 97 | unset($strength['special']); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($useNumbers === false) { |
||
| 101 | unset($strength['number']); |
||
| 102 | } |
||
| 103 | } while (in_array(0, $strength, true)); |
||
| 104 | |||
| 105 | return implode($pass); |
||
| 106 | } |
||
| 107 | |||
| 108 | return implode($passGen()); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Generar una cadena aleatoria usuando criptografía. |
||
| 113 | * |
||
| 114 | * @param int $length opcional, con la longitud de la cadena |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException |
||
| 118 | */ |
||
| 119 | public static function generateRandomBytes($length = 30) |
||
| 120 | { |
||
| 121 | return Encoding::binToHex(Core::secureRandom($length)); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Comprueba y devuelve un directorio temporal válido |
||
| 126 | * |
||
| 127 | * @return bool|string |
||
| 128 | */ |
||
| 129 | public static function getTempDir() |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Realiza el proceso de logout. |
||
| 158 | * |
||
| 159 | * FIXME |
||
| 160 | */ |
||
| 161 | public static function logout() |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Obtener el tamaño máximo de subida de PHP. |
||
| 168 | */ |
||
| 169 | public static function getMaxUpload() |
||
| 170 | { |
||
| 171 | $max_upload = (int)ini_get('upload_max_filesize'); |
||
| 172 | $max_post = (int)ini_get('post_max_size'); |
||
| 173 | $memory_limit = (int)ini_get('memory_limit'); |
||
| 174 | |||
| 175 | return min($max_upload, $max_post, $memory_limit); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Checks a variable to see if it should be considered a boolean true or false. |
||
| 180 | * Also takes into account some text-based representations of true of false, |
||
| 181 | * such as 'false','N','yes','on','off', etc. |
||
| 182 | * |
||
| 183 | * @author Samuel Levy <[email protected]> |
||
| 184 | * |
||
| 185 | * @param mixed $in The variable to check |
||
| 186 | * @param bool $strict If set to false, consider everything that is not false to |
||
| 187 | * be true. |
||
| 188 | * |
||
| 189 | * @return bool The boolean equivalent or null (if strict, and no exact equivalent) |
||
| 190 | */ |
||
| 191 | public static function boolval($in, $strict = false) |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Cast an object to another class, keeping the properties, but changing the methods |
||
| 211 | * |
||
| 212 | * @param string $dstClass Destination class name |
||
| 213 | * @param string|object $serialized |
||
| 214 | * @param string $srcClass Old class name for removing from private methods |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | public static function unserialize($dstClass, $serialized, $srcClass = null) |
||
| 219 | { |
||
| 220 | if (!is_object($serialized)) { |
||
| 221 | $match = preg_match_all('/O:\d+:"(?P<class>[^"]++)"/', $serialized, $matches); |
||
| 222 | |||
| 223 | $process = false; |
||
| 224 | |||
| 225 | if ($match) { |
||
| 226 | foreach ($matches['class'] as $class) { |
||
| 227 | if (!class_exists($class) |
||
| 228 | || $class !== $dstClass |
||
| 229 | ) { |
||
| 230 | $process = true; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($process === false) { |
||
| 235 | return unserialize($serialized); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | // Serialized data needs to be processed to change the class name |
||
| 240 | if ($process === true) { |
||
| 241 | // If source class is set, it will try to clean up the class name from private methods |
||
| 242 | if ($srcClass !== null) { |
||
| 243 | $serialized = preg_replace_callback( |
||
| 244 | '/:\d+:"\x00' . preg_quote($srcClass, '/') . '\x00(\w+)"/', |
||
| 245 | function ($matches) { |
||
| 246 | return ':' . strlen($matches[1]) . ':"' . $matches[1] . '"'; |
||
| 247 | }, |
||
| 248 | $serialized); |
||
| 249 | } |
||
| 250 | |||
| 251 | return self::castToClass($serialized, $dstClass); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | return $serialized; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Cast an object to another class |
||
| 260 | * |
||
| 261 | * @param $cast |
||
| 262 | * @param $class |
||
| 263 | * |
||
| 264 | * @return mixed |
||
| 265 | */ |
||
| 266 | public static function castToClass($cast, $class) |
||
| 267 | { |
||
| 268 | // TODO: should avoid '__PHP_Incomplete_Class'? |
||
| 269 | |||
| 270 | $cast = is_object($cast) ? serialize($cast) : $cast; |
||
| 271 | |||
| 272 | return unserialize(preg_replace('/O:\d+:"[^"]++"/', 'O:' . strlen($class) . ':"' . $class . '"', $cast)); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Bloquear la aplicación |
||
| 277 | * |
||
| 278 | * @param int $userId |
||
| 279 | * @param string $subject |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public static function lockApp($userId, $subject) |
||
| 284 | { |
||
| 285 | $data = ['time' => time(), 'userId' => (int)$userId, 'subject' => $subject]; |
||
| 286 | |||
| 287 | return file_put_contents(LOCK_FILE, json_encode($data)); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Desbloquear la aplicación |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public static function unlockApp() |
||
| 296 | { |
||
| 297 | return @unlink(LOCK_FILE); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Comprueba si la aplicación está bloqueada |
||
| 302 | * |
||
| 303 | * @return int |
||
| 304 | */ |
||
| 305 | public static function getAppLock() |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Devolver el tiempo aproximado en segundos de una operación |
||
| 318 | * |
||
| 319 | * @param $startTime |
||
| 320 | * @param $numItems |
||
| 321 | * @param $totalItems |
||
| 322 | * |
||
| 323 | * @return array Con el tiempo estimado y los elementos por segundo |
||
| 324 | */ |
||
| 325 | public static function getETA($startTime, $numItems, $totalItems) |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Adaptador para convertir una cadena de IDs a un array |
||
| 339 | * |
||
| 340 | * @param string $itemsId |
||
| 341 | * @param string $delimiter |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public static function itemsIdAdapter(string $itemsId, $delimiter = ','): array |
||
| 350 | } |
||
| 351 | } |
||
| 352 |