| Total Complexity | 52 |
| Total Lines | 462 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Functions 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 Functions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Functions |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Returns PATH modified for current operating system. |
||
| 25 | * |
||
| 26 | * @param string $path |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | public static function sysFilename($path) |
||
| 31 | { |
||
| 32 | return str_replace(['\\', '/'], constant('DIRECTORY_SEPARATOR'), $path); |
||
| 33 | } |
||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Add params to url |
||
| 40 | * |
||
| 41 | * @param string $url originall url |
||
| 42 | * @param array $addParams value to add |
||
| 43 | * @param boolean $override replace already existing values ? |
||
| 44 | * |
||
| 45 | * @return string url with parameters added |
||
| 46 | */ |
||
| 47 | public static function addUrlParams($url, $addParams, $override = false) |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Turn all URLs in clickable links. |
||
| 81 | * |
||
| 82 | * @author Arnold Daniels <[email protected]> |
||
| 83 | * |
||
| 84 | * @param string $value |
||
| 85 | * @param array $protocols http/https, ftp, mail, twitter |
||
| 86 | * @param array $attributes |
||
| 87 | * @param string $mode normal or all |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public static function linkify($value, $protocols = array('http', 'mail'), |
||
| 92 | array $attributes = array()) |
||
| 93 | { |
||
| 94 | // Link attributes |
||
| 95 | $attr = ''; |
||
| 96 | foreach ($attributes as $key => $val) { |
||
| 97 | $attr = ' '.$key.'="'.htmlentities($val).'"'; |
||
| 98 | } |
||
| 99 | |||
| 100 | $links = array(); |
||
| 101 | |||
| 102 | // Extract existing links and tags |
||
| 103 | $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', |
||
| 104 | function ($match) use (&$links) { |
||
| 105 | return '<'.array_push($links, $match[1]).'>'; |
||
| 106 | }, $value); |
||
| 107 | |||
| 108 | // Extract text links for each protocol |
||
| 109 | foreach ((array) $protocols as $protocol) { |
||
| 110 | switch ($protocol) { |
||
| 111 | case 'http': |
||
| 112 | case 'https': $value = preg_replace_callback('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i', |
||
| 113 | function ($match) use ($protocol, &$links, $attr) { |
||
| 114 | if ($match[1]) $protocol = $match[1]; |
||
| 115 | $link = $match[2] ?: $match[3]; |
||
| 116 | return '<'.array_push($links, |
||
| 117 | "<a $attr href=\"$protocol://$link\">$link</a>").'>'; |
||
| 118 | }, $value); |
||
| 119 | break; |
||
| 120 | case 'mail': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', |
||
| 121 | function ($match) use (&$links, $attr) { |
||
| 122 | return '<'.array_push($links, |
||
| 123 | "<a $attr href=\"mailto:{$match[1]}\">{$match[1]}</a>").'>'; |
||
| 124 | }, $value); |
||
| 125 | break; |
||
| 126 | default: $value = preg_replace_callback('~'.preg_quote($protocol, |
||
| 127 | '~').'://([^\s<]+?)(?<![\.,:])~i', |
||
| 128 | function ($match) use ($protocol, &$links, $attr) { |
||
| 129 | return '<'.array_push($links, |
||
| 130 | "<a $attr href=\"$protocol://{$match[1]}\">{$match[1]}</a>").'>'; |
||
| 131 | }, $value); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // Insert all link |
||
| 137 | return preg_replace_callback('/<(\d+)>/', |
||
| 138 | function ($match) use (&$links) { |
||
| 139 | return $links[$match[1] - 1]; |
||
| 140 | }, $value); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Move data field $columName from $sourceArray to $destinationArray. |
||
| 145 | * |
||
| 146 | * @param array $sourceArray source |
||
| 147 | * @param array $destinationArray destination |
||
| 148 | * @param string $columName item to move |
||
| 149 | */ |
||
| 150 | public static function divDataArray(&$sourceArray, &$destinationArray, |
||
| 151 | $columName) |
||
| 152 | { |
||
| 153 | $result = false; |
||
| 154 | if (array_key_exists($columName, $sourceArray)) { |
||
| 155 | $destinationArray[$columName] = $sourceArray[$columName]; |
||
| 156 | unset($sourceArray[$columName]); |
||
| 157 | |||
| 158 | $result = true; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $result; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Test for associative array. |
||
| 166 | * |
||
| 167 | * @param array $arr |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public static function isAssoc(array $arr) |
||
| 172 | { |
||
| 173 | return array_keys($arr) !== range(0, count($arr) - 1); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Odstraní z textu diakritiku. |
||
| 178 | * |
||
| 179 | * @param string $text |
||
| 180 | */ |
||
| 181 | public static function rip($text) |
||
| 182 | { |
||
| 183 | $convertTable = [ |
||
| 184 | 'ä' => 'a', |
||
| 185 | 'Ä' => 'A', |
||
| 186 | 'á' => 'a', |
||
| 187 | 'Á' => 'A', |
||
| 188 | 'à' => 'a', |
||
| 189 | 'À' => 'A', |
||
| 190 | 'ã' => 'a', |
||
| 191 | 'Ã' => 'A', |
||
| 192 | 'â' => 'a', |
||
| 193 | 'Â' => 'A', |
||
| 194 | 'č' => 'c', |
||
| 195 | 'Č' => 'C', |
||
| 196 | 'ć' => 'c', |
||
| 197 | 'Ć' => 'C', |
||
| 198 | 'ď' => 'd', |
||
| 199 | 'Ď' => 'D', |
||
| 200 | 'ě' => 'e', |
||
| 201 | 'Ě' => 'E', |
||
| 202 | 'é' => 'e', |
||
| 203 | 'É' => 'E', |
||
| 204 | 'ë' => 'e', |
||
| 205 | 'Ë' => 'E', |
||
| 206 | 'è' => 'e', |
||
| 207 | 'È' => 'E', |
||
| 208 | 'ê' => 'e', |
||
| 209 | 'Ê' => 'E', |
||
| 210 | 'í' => 'i', |
||
| 211 | 'Í' => 'I', |
||
| 212 | 'ï' => 'i', |
||
| 213 | 'Ï' => 'I', |
||
| 214 | 'ì' => 'i', |
||
| 215 | 'Ì' => 'I', |
||
| 216 | 'î' => 'i', |
||
| 217 | 'Î' => 'I', |
||
| 218 | 'ľ' => 'l', |
||
| 219 | 'Ľ' => 'L', |
||
| 220 | 'ĺ' => 'l', |
||
| 221 | 'Ĺ' => 'L', |
||
| 222 | 'ń' => 'n', |
||
| 223 | 'Ń' => 'N', |
||
| 224 | 'ň' => 'n', |
||
| 225 | 'Ň' => 'N', |
||
| 226 | 'ñ' => 'n', |
||
| 227 | 'Ñ' => 'N', |
||
| 228 | 'ó' => 'o', |
||
| 229 | 'Ó' => 'O', |
||
| 230 | 'ö' => 'o', |
||
| 231 | 'Ö' => 'O', |
||
| 232 | 'ô' => 'o', |
||
| 233 | 'Ô' => 'O', |
||
| 234 | 'ò' => 'o', |
||
| 235 | 'Ò' => 'O', |
||
| 236 | 'õ' => 'o', |
||
| 237 | 'Õ' => 'O', |
||
| 238 | 'ő' => 'o', |
||
| 239 | 'Ő' => 'O', |
||
| 240 | 'ř' => 'r', |
||
| 241 | 'Ř' => 'R', |
||
| 242 | 'ŕ' => 'r', |
||
| 243 | 'Ŕ' => 'R', |
||
| 244 | 'š' => 's', |
||
| 245 | 'Š' => 'S', |
||
| 246 | 'ś' => 's', |
||
| 247 | 'Ś' => 'S', |
||
| 248 | 'ť' => 't', |
||
| 249 | 'Ť' => 'T', |
||
| 250 | 'ú' => 'u', |
||
| 251 | 'Ú' => 'U', |
||
| 252 | 'ů' => 'u', |
||
| 253 | 'Ů' => 'U', |
||
| 254 | 'ü' => 'u', |
||
| 255 | 'Ü' => 'U', |
||
| 256 | 'ù' => 'u', |
||
| 257 | 'Ù' => 'U', |
||
| 258 | 'ũ' => 'u', |
||
| 259 | 'Ũ' => 'U', |
||
| 260 | 'û' => 'u', |
||
| 261 | 'Û' => 'U', |
||
| 262 | 'ý' => 'y', |
||
| 263 | 'Ý' => 'Y', |
||
| 264 | 'ž' => 'z', |
||
| 265 | 'Ž' => 'Z', |
||
| 266 | 'ź' => 'z', |
||
| 267 | 'Ź' => 'Z', |
||
| 268 | ]; |
||
| 269 | |||
| 270 | return iconv('UTF-8', 'ASCII//TRANSLIT', strtr($text, $convertTable)); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Encrypt. |
||
| 275 | * Šifrování. |
||
| 276 | * |
||
| 277 | * @param string $textToEncrypt plaintext |
||
| 278 | * @param string $encryptKey klíč |
||
| 279 | * |
||
| 280 | * @return string encrypted text |
||
| 281 | */ |
||
| 282 | public static function easeEncrypt($textToEncrypt, $encryptKey) |
||
| 283 | { |
||
| 284 | $encryption_key = base64_decode($encryptKey); |
||
| 285 | $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); |
||
| 286 | $encrypted = openssl_encrypt($textToEncrypt, 'aes-256-cbc', |
||
| 287 | $encryption_key, 0, $iv); |
||
| 288 | return base64_encode($encrypted.'::'.$iv); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Decrypt |
||
| 293 | * Dešifrování. |
||
| 294 | * |
||
| 295 | * @param string $textToDecrypt šifrovaný text |
||
| 296 | * @param string $encryptKey šifrovací klíč |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public static function easeDecrypt($textToDecrypt, $encryptKey) |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Generování náhodného čísla. |
||
| 311 | * |
||
| 312 | * @param int $minimal |
||
| 313 | * @param int $maximal |
||
| 314 | * |
||
| 315 | * @return float |
||
| 316 | */ |
||
| 317 | public static function randomNumber($minimal = null, $maximal = null) |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Vrací náhodný řetězec dané délky. |
||
| 335 | * |
||
| 336 | * @param int $length |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public static function randomString($length = 6) |
||
| 341 | { |
||
| 342 | return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), |
||
| 343 | 0, $length); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Rekurzivně překóduje pole. |
||
| 348 | * |
||
| 349 | * @param string $in_charset |
||
| 350 | * @param string $out_charset |
||
| 351 | * @param array $arr originální pole |
||
| 352 | * |
||
| 353 | * @return array překódované pole |
||
| 354 | */ |
||
| 355 | public static function recursiveIconv($in_charset, $out_charset, $arr) |
||
| 356 | { |
||
| 357 | if (!is_array($arr)) { |
||
| 358 | return \iconv($in_charset, $out_charset, $arr); |
||
| 359 | } |
||
| 360 | $ret = $arr; |
||
| 361 | |||
| 362 | array_walk_recursive($ret, '\Ease\Functions::arrayIconv', [$in_charset, $out_charset, $arr] ); |
||
| 363 | |||
| 364 | return $ret; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Pomocná funkce pro překódování vícerozměrného pole. |
||
| 369 | * |
||
| 370 | * @see recursiveIconv |
||
| 371 | * |
||
| 372 | * @param mixed $val |
||
| 373 | * @param string $key |
||
| 374 | * @param mixed $encodings |
||
| 375 | */ |
||
| 376 | public static function arrayIconv(&$val, /** @scrutinizer ignore-unused */ $key, $encodings) |
||
| 377 | { |
||
| 378 | $val = iconv($encodings[0], $encodings[1], $val); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Zobrazí velikost souboru v srozumitelném tvaru. |
||
| 383 | * |
||
| 384 | * @param int $filesize bytů |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public static function humanFilesize($filesize) |
||
| 389 | { |
||
| 390 | if (is_numeric($filesize)) { |
||
| 391 | $decr = 1024; |
||
| 392 | $step = 0; |
||
| 393 | $prefix = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB']; |
||
| 394 | |||
| 395 | while (($filesize / $decr) > 0.9) { |
||
| 396 | $filesize = $filesize / $decr; |
||
| 397 | ++$step; |
||
| 398 | } |
||
| 399 | |||
| 400 | return round($filesize, 2).' '.$prefix[$step]; |
||
| 401 | } else { |
||
| 402 | return 'NaN'; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Reindex Array by given key. |
||
| 408 | * |
||
| 409 | * @param array $data array to reindex |
||
| 410 | * @param string $indexBy one of columns in array |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public static function reindexArrayBy($data, $indexBy = null) |
||
| 415 | { |
||
| 416 | $reindexedData = []; |
||
| 417 | |||
| 418 | foreach ($data as $dataID => $data) { |
||
| 419 | if (array_key_exists($indexBy, $data)) { |
||
| 420 | $reindexedData[$data[$indexBy]] = $data; |
||
| 421 | } else { |
||
| 422 | throw new \Exception(sprintf('Data row does not contain column %s for reindexing', |
||
| 423 | $indexBy)); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | return $reindexedData; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Filter Only letters from string. |
||
| 432 | * Pouze malé a velké písmena. |
||
| 433 | * |
||
| 434 | * @return string text bez zvláštních znaků |
||
| 435 | */ |
||
| 436 | public static function lettersOnly($text) |
||
| 437 | { |
||
| 438 | return preg_replace('/[^(a-zA-Z0-9)]*/', '', $text); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Confirm that string is serialized |
||
| 443 | * |
||
| 444 | * @param string $data |
||
| 445 | * |
||
| 446 | * @return boolean |
||
| 447 | */ |
||
| 448 | public static function isSerialized($data) |
||
| 449 | { |
||
| 450 | // if it isn't a string, it isn't serialized |
||
| 451 | if (!is_string($data)) return false; |
||
| 452 | $data = trim($data); |
||
| 453 | if ('N;' == $data) return true; |
||
| 454 | if (!preg_match('/^([adObis]):/', $data, $badions)) return false; |
||
| 455 | switch ($badions[1]) { |
||
| 456 | case 'a' : |
||
| 457 | case 'O' : |
||
| 458 | case 's' : |
||
| 459 | if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) |
||
| 460 | return true; |
||
| 461 | break; |
||
| 462 | case 'b' : |
||
| 463 | case 'i' : |
||
| 464 | case 'd' : |
||
| 465 | if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) |
||
| 466 | return true; |
||
| 467 | break; |
||
| 468 | } |
||
| 469 | return false; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get Classname without namespace prefix |
||
| 474 | * |
||
| 475 | * @param object $object |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | static public function baseClassName($object) |
||
| 483 | } |
||
| 484 | |||
| 485 | } |
||
| 486 |