| Total Complexity | 41 |
| Total Lines | 208 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Helper |
||
| 6 | { |
||
| 7 | // @formatter:off |
||
| 8 | /** |
||
| 9 | * Cyrillic mapping. |
||
| 10 | * |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected static $cyrMap = array( |
||
| 14 | 'е', 'ё', 'ж', 'х', 'ц', 'ч', 'ш', 'щ', 'ю', 'я', |
||
| 15 | 'Е', 'Ё', 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ю', 'Я', |
||
| 16 | 'а', 'б', 'в', 'г', 'д', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'ъ', 'ы', 'ь', 'э', |
||
| 17 | 'А', 'Б', 'В', 'Г', 'Д', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Ъ', 'Ы', 'Ь', 'Э' |
||
| 18 | ); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Latin mapping. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected static $latMap = array( |
||
| 26 | 'ye', 'ye', 'zh', 'kh', 'ts', 'ch', 'sh', 'shch', 'yu', 'ya', |
||
| 27 | 'Ye', 'Ye', 'Zh', 'Kh', 'Ts', 'Ch', 'Sh', 'Shch', 'Yu', 'Ya', |
||
| 28 | 'a', 'b', 'v', 'g', 'd', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ʺ', 'y', '–', 'e', |
||
| 29 | 'A', 'B', 'V', 'G', 'D', 'Z', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'ʺ', 'Y', '–', 'E' |
||
| 30 | ); |
||
| 31 | // @formatter:on |
||
| 32 | |||
| 33 | public static function toCamelCase($string) |
||
| 34 | { |
||
| 35 | return preg_replace('~\s+~', '', lcfirst(ucwords(strtr($string, '_', ' ')))); |
||
| 36 | } |
||
| 37 | |||
| 38 | public static function fromCamelCase($string, $separator = '_') |
||
| 39 | { |
||
| 40 | return strtolower(preg_replace('/(?!^)[[:upper:]]+/', $separator.'$0', $string)); |
||
| 41 | } |
||
| 42 | |||
| 43 | public static function isEmpty($variable) |
||
| 44 | { |
||
| 45 | $result = true; |
||
| 46 | |||
| 47 | if (is_array($variable) && count($variable) > 0) { |
||
| 48 | foreach ($variable as $value) { |
||
| 49 | $result = $result && self::isEmpty($value); |
||
| 50 | } |
||
| 51 | } else { |
||
| 52 | $result = empty($variable); |
||
| 53 | } |
||
| 54 | |||
| 55 | return $result; |
||
| 56 | } |
||
| 57 | |||
| 58 | public static function variable(&$value) |
||
| 59 | { |
||
| 60 | if ($value instanceof \DateTime) { |
||
| 61 | $value = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'"; |
||
| 62 | } elseif (!is_numeric($value)) { |
||
| 63 | $value = "'".addslashes(trim($value))."'"; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (trim($value) === '' || trim($value) === "''") { |
||
| 67 | $value = null; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | public static function getUniqueId($length = 20) |
||
| 72 | { |
||
| 73 | return bin2hex(openssl_random_pseudo_bytes($length)); |
||
| 74 | } |
||
| 75 | |||
| 76 | public static function validateDate($date) |
||
| 77 | { |
||
| 78 | $date = str_replace('-', '', $date); |
||
| 79 | $day = intval(substr($date, 0, 2)); |
||
| 80 | $month = intval(substr($date, 2, 2)); |
||
| 81 | |||
| 82 | if ($month < 0 || $month > 12) { |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | // @formatter:off |
||
| 86 | $months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
||
| 87 | // @formatter:on |
||
| 88 | if (intval(substr($date, 4, 2)) % 4 === 0) { |
||
| 89 | $months[1] = 29; |
||
| 90 | } |
||
| 91 | |||
| 92 | return $day > 0 && $day <= $months[$month - 1]; |
||
| 93 | } |
||
| 94 | |||
| 95 | public static function newPKValidate($personCode) |
||
| 96 | { |
||
| 97 | $personCode = str_replace('-', '', $personCode); |
||
| 98 | // @formatter:off |
||
| 99 | $sum = |
||
| 100 | (substr($personCode, 0, 1) * 1) + |
||
| 101 | (substr($personCode, 1, 1) * 6) + |
||
| 102 | (substr($personCode, 2, 1) * 3) + |
||
| 103 | (substr($personCode, 3, 1) * 7) + |
||
| 104 | (substr($personCode, 4, 1) * 9) + |
||
| 105 | (substr($personCode, 5, 1) * 10) + |
||
| 106 | (substr($personCode, 6, 1) * 5) + |
||
| 107 | (substr($personCode, 7, 1) * 8) + |
||
| 108 | (substr($personCode, 8, 1) * 4) + |
||
| 109 | (substr($personCode, 9, 1) * 2); |
||
| 110 | // @formatter:on |
||
| 111 | |||
| 112 | $remainder = $sum % 11; |
||
| 113 | |||
| 114 | if (1 - $remainder < -1) { |
||
| 115 | return substr($personCode, 10, 1) == (1 - $remainder + 11); |
||
| 116 | } else { |
||
| 117 | return substr($personCode, 10, 1) == (1 - $remainder); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | public static function validatePersonCode($personCode = null) |
||
| 122 | { |
||
| 123 | if ($personCode !== null) { |
||
| 124 | $personCode = str_replace('-', '', $personCode); |
||
| 125 | |||
| 126 | if (strlen($personCode) !== 11) { |
||
| 127 | return 'error_length'; |
||
| 128 | } |
||
| 129 | if (preg_match("/^[0-9]+$/", $personCode) === null) { |
||
| 130 | return 'error_symbols'; |
||
| 131 | } |
||
| 132 | if (intval(substr($personCode, 0, 2)) < 32) { |
||
| 133 | if (!self::validateDate($personCode)) { |
||
| 134 | return 'error_invalid'; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | if (intval(substr($personCode, 0, 2)) > 32 || (intval(substr($personCode, 0, 2)) === 32 && !self::newPKValidate($personCode))) { |
||
| 138 | return 'error_invalid'; |
||
| 139 | } |
||
| 140 | |||
| 141 | return true; |
||
| 142 | } |
||
| 143 | |||
| 144 | return 'error_empty'; |
||
| 145 | } |
||
| 146 | |||
| 147 | public static function swap(&$foo, &$bar) |
||
| 148 | { |
||
| 149 | $tmp = $foo; |
||
| 150 | $foo = $bar; |
||
| 151 | $bar = $tmp; |
||
| 152 | } |
||
| 153 | |||
| 154 | public static function removeDuplicates(&$array) |
||
| 157 | } |
||
| 158 | |||
| 159 | public static function cleanText($text) |
||
| 160 | { |
||
| 161 | return html_entity_decode(self::oneSpace(str_replace(' ?', '', mb_convert_encoding(strip_tags($text), "UTF-8", "UTF-8")))); |
||
| 162 | } |
||
| 163 | |||
| 164 | public static function oneSpace($text) |
||
| 165 | { |
||
| 166 | return preg_replace('/\s+/S', ' ', $text); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Transliterates cyrillic text to latin. |
||
| 171 | * |
||
| 172 | * @param string $text cyrillic text |
||
| 173 | * |
||
| 174 | * @return string latin text |
||
| 175 | */ |
||
| 176 | public static function translit2($text) |
||
| 177 | { |
||
| 178 | return str_replace(self::$cyrMap, self::$latMap, $text); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Transliterates latin text to cyrillic. |
||
| 183 | * |
||
| 184 | * @param string $text latin text |
||
| 185 | * |
||
| 186 | * @return string cyrillic text |
||
| 187 | */ |
||
| 188 | public static function translit4($text) |
||
| 191 | } |
||
| 192 | |||
| 193 | public static function multiple(array $keys) |
||
| 202 | } |
||
| 203 | |||
| 204 | public static function multiset(array $keys) |
||
| 213 | } |
||
| 214 | } |