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