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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
5 | class Util |
||
6 | { |
||
7 | const LAYOUT_STYLE = 1; |
||
8 | const LAYOUT_CLASS = 2; |
||
9 | |||
10 | const INTERNAL_BLOCK_OPEN = '%%%INTBLOCKO235978%%%'; |
||
11 | const INTERNAL_BLOCK_CLOSE = '%%%INTBLOCKC235978%%%'; |
||
12 | /** |
||
13 | * Таблица символов |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | public static $_charsTable = array( |
||
18 | '"' => array('html' => array('«', '»', '“', '‘', '„', '“', '"', '«', '»'), |
||
19 | 'utf8' => array(0x201E, 0x201C, 0x201F, 0x201D, 0x00AB, 0x00BB)), |
||
20 | ' ' => array('html' => array(' ', ' ', ' '), |
||
21 | 'utf8' => array(0x00A0, 0x2002, 0x2003, 0x2008, 0x2009)), |
||
22 | '-' => array('html' => array( /*'—',*/ |
||
23 | '–', '−', '—', '—', '–'), |
||
24 | 'utf8' => array(0x002D, /*0x2014,*/ |
||
25 | 0x2010, 0x2012, 0x2013)), |
||
26 | '—' => array('html' => array('—'), |
||
27 | 'utf8' => array(0x2014)), |
||
28 | '==' => array('html' => array('≡'), |
||
29 | 'utf8' => array(0x2261)), |
||
30 | '...' => array('html' => array('…', '…'), |
||
31 | 'utf8' => array(0x2026)), |
||
32 | '!=' => array('html' => array('≠', '≠'), |
||
33 | 'utf8' => array(0x2260)), |
||
34 | '<=' => array('html' => array('≤', '≤'), |
||
35 | 'utf8' => array(0x2264)), |
||
36 | '>=' => array('html' => array('≥', '≥'), |
||
37 | 'utf8' => array(0x2265)), |
||
38 | '1/2' => array('html' => array('½', '½'), |
||
39 | 'utf8' => array(0x00BD)), |
||
40 | '1/4' => array('html' => array('¼', '¼'), |
||
41 | 'utf8' => array(0x00BC)), |
||
42 | '3/4' => array('html' => array('¾', '¾'), |
||
43 | 'utf8' => array(0x00BE)), |
||
44 | '+-' => array('html' => array('±', '±'), |
||
45 | 'utf8' => array(0x00B1)), |
||
46 | '&' => array('html' => array('&', '&')), |
||
47 | '(tm)' => array('html' => array('™', '™'), |
||
48 | 'utf8' => array(0x2122)), |
||
49 | //'(r)' => array('html' => array('<sup>®</sup>', '®', '®'), |
||
50 | '(r)' => array('html' => array('®', '®'), |
||
51 | 'utf8' => array(0x00AE)), |
||
52 | '(c)' => array('html' => array('©', '©'), |
||
53 | 'utf8' => array(0x00A9)), |
||
54 | '§' => array('html' => array('§', '§'), |
||
55 | 'utf8' => array(0x00A7)), |
||
56 | '`' => array('html' => array('́')), |
||
57 | '\'' => array('html' => array('’', '’')), |
||
58 | 'x' => array('html' => array('×', '×'), |
||
59 | 'utf8' => array('×') /* какой же у него может быть код? */), |
||
60 | |||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * Добавление к тегам атрибута 'id', благодаря которому |
||
65 | * при повторном типографирование текста будут удалены теги, |
||
66 | * расставленные данным типографом |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected static $_typographSpecificTagId = false; |
||
71 | |||
72 | /** |
||
73 | * Костыли для работы с символами UTF-8 |
||
74 | * |
||
75 | * @author somebody? |
||
76 | * @param int $c код символа в кодировке UTF-8 (например, 0x00AB) |
||
77 | * @return string|false |
||
78 | */ |
||
79 | public static function _getUnicodeChar($c) |
||
99 | |||
100 | /** |
||
101 | * Удаление кодов HTML из текста |
||
102 | * |
||
103 | * <code> |
||
104 | * // Remove UTF-8 chars: |
||
105 | * $str = \EMT\Util::clear_special_chars('your text', 'utf8'); |
||
106 | * // ... or HTML codes only: |
||
107 | * $str = \EMT\Util::clear_special_chars('your text', 'html'); |
||
108 | * // ... or combo: |
||
109 | * $str = \EMT\Util::clear_special_chars('your text'); |
||
110 | * </code> |
||
111 | * |
||
112 | * @param string $text |
||
113 | * @param mixed $mode |
||
114 | * @return false|string |
||
115 | */ |
||
116 | public static function clear_special_chars($text, $mode = null) |
||
145 | |||
146 | /** |
||
147 | * Удаление тегов HTML из текста |
||
148 | * Тег <br /> будет преобразов в перенос строки \n, сочетание тегов </p><p> - |
||
149 | * в двойной перенос |
||
150 | * |
||
151 | * @param string $text |
||
152 | * @param array $allowableTag массив из тегов, которые будут проигнорированы |
||
153 | * @return string |
||
154 | */ |
||
155 | public static function remove_html_tags($text, $allowableTag = null) |
||
178 | |||
179 | /** |
||
180 | * Сохраняем содержимое тегов HTML |
||
181 | * |
||
182 | * Тег 'a' кодируется со специальным префиксом для дальнейшей |
||
183 | * возможности выносить за него кавычки. |
||
184 | * |
||
185 | * @param string $text |
||
186 | * @param boolean $way |
||
187 | * @return string |
||
188 | */ |
||
189 | public static function safe_tag_chars($text, $way) |
||
210 | |||
211 | /** |
||
212 | * Декодриует спец блоки |
||
213 | * |
||
214 | * @param string $text |
||
215 | * @return string |
||
216 | */ |
||
217 | public static function decode_internal_blocks($text) |
||
227 | |||
228 | /** |
||
229 | * Кодирует спец блок |
||
230 | * |
||
231 | * @param string $text |
||
232 | * @return string |
||
233 | */ |
||
234 | public static function iblock($text) |
||
238 | |||
239 | /** |
||
240 | * Создание тега с защищенным содержимым |
||
241 | * |
||
242 | * @param string $content текст, который будет обрамлен тегом |
||
243 | * @param string $tag тэг |
||
244 | * @param array $attribute список атрибутов, где ключ - имя атрибута, а значение - само значение данного атрибута |
||
245 | * @return string |
||
246 | */ |
||
247 | public static function build_safe_tag($content, $tag = 'span', $attribute = array(), $layout = \EMT\Util::LAYOUT_STYLE) |
||
291 | |||
292 | /** |
||
293 | * Метод, осуществляющий кодирование (сохранение) информации |
||
294 | * с целью невозможности типографировать ее |
||
295 | * |
||
296 | * @param string $text |
||
297 | * @return string |
||
298 | */ |
||
299 | public static function encrypt_tag($text) |
||
303 | |||
304 | /** |
||
305 | * Метод, осуществляющий декодирование информации |
||
306 | * |
||
307 | * @param string $text |
||
308 | * @return string |
||
309 | */ |
||
310 | public static function decrypt_tag($text) |
||
314 | |||
315 | /** |
||
316 | * @param string[] $needle |
||
317 | */ |
||
318 | public static function strpos_ex(&$haystack, $needle, $offset = null) |
||
343 | |||
344 | public static function _process_selector_pattern(&$pattern) |
||
351 | |||
352 | public static function _test_pattern($pattern, $text) |
||
358 | |||
359 | public static function strtolower($string) |
||
378 | |||
379 | // взято с http://www.w3.org/TR/html4/sgml/entities.html |
||
380 | protected static $html4_char_ents = array( |
||
381 | 'nbsp' => 160, |
||
382 | 'iexcl' => 161, |
||
383 | 'cent' => 162, |
||
384 | 'pound' => 163, |
||
385 | 'curren' => 164, |
||
386 | 'yen' => 165, |
||
387 | 'brvbar' => 166, |
||
388 | 'sect' => 167, |
||
389 | 'uml' => 168, |
||
390 | 'copy' => 169, |
||
391 | 'ordf' => 170, |
||
392 | 'laquo' => 171, |
||
393 | 'not' => 172, |
||
394 | 'shy' => 173, |
||
395 | 'reg' => 174, |
||
396 | 'macr' => 175, |
||
397 | 'deg' => 176, |
||
398 | 'plusmn' => 177, |
||
399 | 'sup2' => 178, |
||
400 | 'sup3' => 179, |
||
401 | 'acute' => 180, |
||
402 | 'micro' => 181, |
||
403 | 'para' => 182, |
||
404 | 'middot' => 183, |
||
405 | 'cedil' => 184, |
||
406 | 'sup1' => 185, |
||
407 | 'ordm' => 186, |
||
408 | 'raquo' => 187, |
||
409 | 'frac14' => 188, |
||
410 | 'frac12' => 189, |
||
411 | 'frac34' => 190, |
||
412 | 'iquest' => 191, |
||
413 | 'Agrave' => 192, |
||
414 | 'Aacute' => 193, |
||
415 | 'Acirc' => 194, |
||
416 | 'Atilde' => 195, |
||
417 | 'Auml' => 196, |
||
418 | 'Aring' => 197, |
||
419 | 'AElig' => 198, |
||
420 | 'Ccedil' => 199, |
||
421 | 'Egrave' => 200, |
||
422 | 'Eacute' => 201, |
||
423 | 'Ecirc' => 202, |
||
424 | 'Euml' => 203, |
||
425 | 'Igrave' => 204, |
||
426 | 'Iacute' => 205, |
||
427 | 'Icirc' => 206, |
||
428 | 'Iuml' => 207, |
||
429 | 'ETH' => 208, |
||
430 | 'Ntilde' => 209, |
||
431 | 'Ograve' => 210, |
||
432 | 'Oacute' => 211, |
||
433 | 'Ocirc' => 212, |
||
434 | 'Otilde' => 213, |
||
435 | 'Ouml' => 214, |
||
436 | 'times' => 215, |
||
437 | 'Oslash' => 216, |
||
438 | 'Ugrave' => 217, |
||
439 | 'Uacute' => 218, |
||
440 | 'Ucirc' => 219, |
||
441 | 'Uuml' => 220, |
||
442 | 'Yacute' => 221, |
||
443 | 'THORN' => 222, |
||
444 | 'szlig' => 223, |
||
445 | 'agrave' => 224, |
||
446 | 'aacute' => 225, |
||
447 | 'acirc' => 226, |
||
448 | 'atilde' => 227, |
||
449 | 'auml' => 228, |
||
450 | 'aring' => 229, |
||
451 | 'aelig' => 230, |
||
452 | 'ccedil' => 231, |
||
453 | 'egrave' => 232, |
||
454 | 'eacute' => 233, |
||
455 | 'ecirc' => 234, |
||
456 | 'euml' => 235, |
||
457 | 'igrave' => 236, |
||
458 | 'iacute' => 237, |
||
459 | 'icirc' => 238, |
||
460 | 'iuml' => 239, |
||
461 | 'eth' => 240, |
||
462 | 'ntilde' => 241, |
||
463 | 'ograve' => 242, |
||
464 | 'oacute' => 243, |
||
465 | 'ocirc' => 244, |
||
466 | 'otilde' => 245, |
||
467 | 'ouml' => 246, |
||
468 | 'divide' => 247, |
||
469 | 'oslash' => 248, |
||
470 | 'ugrave' => 249, |
||
471 | 'uacute' => 250, |
||
472 | 'ucirc' => 251, |
||
473 | 'uuml' => 252, |
||
474 | 'yacute' => 253, |
||
475 | 'thorn' => 254, |
||
476 | 'yuml' => 255, |
||
477 | 'fnof' => 402, |
||
478 | 'Alpha' => 913, |
||
479 | 'Beta' => 914, |
||
480 | 'Gamma' => 915, |
||
481 | 'Delta' => 916, |
||
482 | 'Epsilon' => 917, |
||
483 | 'Zeta' => 918, |
||
484 | 'Eta' => 919, |
||
485 | 'Theta' => 920, |
||
486 | 'Iota' => 921, |
||
487 | 'Kappa' => 922, |
||
488 | 'Lambda' => 923, |
||
489 | 'Mu' => 924, |
||
490 | 'Nu' => 925, |
||
491 | 'Xi' => 926, |
||
492 | 'Omicron' => 927, |
||
493 | 'Pi' => 928, |
||
494 | 'Rho' => 929, |
||
495 | 'Sigma' => 931, |
||
496 | 'Tau' => 932, |
||
497 | 'Upsilon' => 933, |
||
498 | 'Phi' => 934, |
||
499 | 'Chi' => 935, |
||
500 | 'Psi' => 936, |
||
501 | 'Omega' => 937, |
||
502 | 'alpha' => 945, |
||
503 | 'beta' => 946, |
||
504 | 'gamma' => 947, |
||
505 | 'delta' => 948, |
||
506 | 'epsilon' => 949, |
||
507 | 'zeta' => 950, |
||
508 | 'eta' => 951, |
||
509 | 'theta' => 952, |
||
510 | 'iota' => 953, |
||
511 | 'kappa' => 954, |
||
512 | 'lambda' => 955, |
||
513 | 'mu' => 956, |
||
514 | 'nu' => 957, |
||
515 | 'xi' => 958, |
||
516 | 'omicron' => 959, |
||
517 | 'pi' => 960, |
||
518 | 'rho' => 961, |
||
519 | 'sigmaf' => 962, |
||
520 | 'sigma' => 963, |
||
521 | 'tau' => 964, |
||
522 | 'upsilon' => 965, |
||
523 | 'phi' => 966, |
||
524 | 'chi' => 967, |
||
525 | 'psi' => 968, |
||
526 | 'omega' => 969, |
||
527 | 'thetasym' => 977, |
||
528 | 'upsih' => 978, |
||
529 | 'piv' => 982, |
||
530 | 'bull' => 8226, |
||
531 | 'hellip' => 8230, |
||
532 | 'prime' => 8242, |
||
533 | 'Prime' => 8243, |
||
534 | 'oline' => 8254, |
||
535 | 'frasl' => 8260, |
||
536 | 'weierp' => 8472, |
||
537 | 'image' => 8465, |
||
538 | 'real' => 8476, |
||
539 | 'trade' => 8482, |
||
540 | 'alefsym' => 8501, |
||
541 | 'larr' => 8592, |
||
542 | 'uarr' => 8593, |
||
543 | 'rarr' => 8594, |
||
544 | 'darr' => 8595, |
||
545 | 'harr' => 8596, |
||
546 | 'crarr' => 8629, |
||
547 | 'lArr' => 8656, |
||
548 | 'uArr' => 8657, |
||
549 | 'rArr' => 8658, |
||
550 | 'dArr' => 8659, |
||
551 | 'hArr' => 8660, |
||
552 | 'forall' => 8704, |
||
553 | 'part' => 8706, |
||
554 | 'exist' => 8707, |
||
555 | 'empty' => 8709, |
||
556 | 'nabla' => 8711, |
||
557 | 'isin' => 8712, |
||
558 | 'notin' => 8713, |
||
559 | 'ni' => 8715, |
||
560 | 'prod' => 8719, |
||
561 | 'sum' => 8721, |
||
562 | 'minus' => 8722, |
||
563 | 'lowast' => 8727, |
||
564 | 'radic' => 8730, |
||
565 | 'prop' => 8733, |
||
566 | 'infin' => 8734, |
||
567 | 'ang' => 8736, |
||
568 | 'and' => 8743, |
||
569 | 'or' => 8744, |
||
570 | 'cap' => 8745, |
||
571 | 'cup' => 8746, |
||
572 | 'int' => 8747, |
||
573 | 'there4' => 8756, |
||
574 | 'sim' => 8764, |
||
575 | 'cong' => 8773, |
||
576 | 'asymp' => 8776, |
||
577 | 'ne' => 8800, |
||
578 | 'equiv' => 8801, |
||
579 | 'le' => 8804, |
||
580 | 'ge' => 8805, |
||
581 | 'sub' => 8834, |
||
582 | 'sup' => 8835, |
||
583 | 'nsub' => 8836, |
||
584 | 'sube' => 8838, |
||
585 | 'supe' => 8839, |
||
586 | 'oplus' => 8853, |
||
587 | 'otimes' => 8855, |
||
588 | 'perp' => 8869, |
||
589 | 'sdot' => 8901, |
||
590 | 'lceil' => 8968, |
||
591 | 'rceil' => 8969, |
||
592 | 'lfloor' => 8970, |
||
593 | 'rfloor' => 8971, |
||
594 | 'lang' => 9001, |
||
595 | 'rang' => 9002, |
||
596 | 'loz' => 9674, |
||
597 | 'spades' => 9824, |
||
598 | 'clubs' => 9827, |
||
599 | 'hearts' => 9829, |
||
600 | 'diams' => 9830, |
||
601 | 'quot' => 34, |
||
602 | 'amp' => 38, |
||
603 | 'lt' => 60, |
||
604 | 'gt' => 62, |
||
605 | 'OElig' => 338, |
||
606 | 'oelig' => 339, |
||
607 | 'Scaron' => 352, |
||
608 | 'scaron' => 353, |
||
609 | 'Yuml' => 376, |
||
610 | 'circ' => 710, |
||
611 | 'tilde' => 732, |
||
612 | 'ensp' => 8194, |
||
613 | 'emsp' => 8195, |
||
614 | 'thinsp' => 8201, |
||
615 | 'zwnj' => 8204, |
||
616 | 'zwj' => 8205, |
||
617 | 'lrm' => 8206, |
||
618 | 'rlm' => 8207, |
||
619 | 'ndash' => 8211, |
||
620 | 'mdash' => 8212, |
||
621 | 'lsquo' => 8216, |
||
622 | 'rsquo' => 8217, |
||
623 | 'sbquo' => 8218, |
||
624 | 'ldquo' => 8220, |
||
625 | 'rdquo' => 8221, |
||
626 | 'bdquo' => 8222, |
||
627 | 'dagger' => 8224, |
||
628 | 'Dagger' => 8225, |
||
629 | 'permil' => 8240, |
||
630 | 'lsaquo' => 8249, |
||
631 | 'rsaquo' => 8250, |
||
632 | 'euro' => 8364, |
||
633 | ); |
||
634 | |||
635 | /** |
||
636 | * Вернуть уникод символ по html entinty |
||
637 | * |
||
638 | * @param string $entity |
||
639 | * @return string |
||
640 | */ |
||
641 | public static function html_char_entity_to_unicode($entity) |
||
647 | |||
648 | /** |
||
649 | * Сконвериторвать все html entity в соответсвующие юникод символы |
||
650 | * |
||
651 | * @param string $text |
||
652 | */ |
||
653 | public static function convert_html_entities_to_unicode(&$text) |
||
669 | |||
670 | /** |
||
671 | * @param string $needle |
||
672 | */ |
||
673 | public static function rstrpos($haystack, $needle, $offset = 0) |
||
691 | |||
692 | public static function ifop($cond, $true, $false) |
||
696 | |||
697 | } |
||
698 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.