| Total Complexity | 57 |
| Total Lines | 265 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 10 | class Helper |
||
| 11 | { |
||
| 12 | // @formatter:off |
||
| 13 | /** |
||
| 14 | * Cyrillic mapping. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected static $cyrMap = array( |
||
| 19 | 'е', 'ё', 'ж', 'х', 'ц', 'ч', 'ш', 'щ', 'ю', 'я', |
||
| 20 | 'Е', 'Ё', 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ю', 'Я', |
||
| 21 | 'а', 'б', 'в', 'г', 'д', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'ъ', 'ы', 'ь', 'э', |
||
| 22 | 'А', 'Б', 'В', 'Г', 'Д', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Ъ', 'Ы', 'Ь', 'Э' |
||
| 23 | ); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Latin mapping. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected static $latMap = array( |
||
| 31 | 'ye', 'ye', 'zh', 'kh', 'ts', 'ch', 'sh', 'shch', 'yu', 'ya', |
||
| 32 | 'Ye', 'Ye', 'Zh', 'Kh', 'Ts', 'Ch', 'Sh', 'Shch', 'Yu', 'Ya', |
||
| 33 | 'a', 'b', 'v', 'g', 'd', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ʺ', 'y', '–', 'e', |
||
| 34 | 'A', 'B', 'V', 'G', 'D', 'Z', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'ʺ', 'Y', '–', 'E' |
||
| 35 | ); |
||
| 36 | // @formatter:on |
||
| 37 | |||
| 38 | const RAND_BASIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 39 | const RAND_EXTENDED = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_-=+;:,.?'; |
||
| 40 | |||
| 41 | public static function toCamelCase($string, $upFirst = true) |
||
| 42 | { |
||
| 43 | if ($upFirst) { |
||
| 44 | return preg_replace('~\s+~', '', lcfirst(ucwords(str_replace('_', ' ', $string)))); |
||
| 45 | } |
||
| 46 | |||
| 47 | return preg_replace('~\s+~', '', ucwords(str_replace('_', ' ', $string))); |
||
| 48 | } |
||
| 49 | |||
| 50 | public static function fromCamelCase($string, $separator = '_') |
||
| 51 | { |
||
| 52 | return strtolower(preg_replace('/(?!^)[[:upper:]]+/', $separator.'$0', $string)); |
||
| 53 | } |
||
| 54 | |||
| 55 | public static function isEmpty($variable) |
||
| 56 | { |
||
| 57 | $result = true; |
||
| 58 | |||
| 59 | if (\is_array($variable) && \count($variable) > 0) { |
||
| 60 | foreach ($variable as $value) { |
||
| 61 | $result = $result && self::isEmpty($value); |
||
| 62 | } |
||
| 63 | } else { |
||
| 64 | $result = empty($variable); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $result; |
||
| 68 | } |
||
| 69 | |||
| 70 | public static function genRandomString($length = 20, $chars = self::RAND_BASIC) |
||
| 71 | { |
||
| 72 | return substr(str_shuffle(str_repeat($chars, (int)ceil((int)($length / \strlen($chars))))), 1, $length); |
||
| 73 | } |
||
| 74 | |||
| 75 | public static function getUniqueId($length = 20) |
||
| 76 | { |
||
| 77 | try { |
||
| 78 | return bin2hex(random_bytes($length)); |
||
| 79 | } catch (\Exception $exception) { |
||
| 80 | return self::genRandomString($length); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | public static function validatePersonCode($personCode) |
||
| 85 | { |
||
| 86 | $personCode = str_replace('-', '', $personCode); |
||
| 87 | $result = true; |
||
| 88 | |||
| 89 | if (\strlen($personCode) !== 11 || preg_match('/^\d+$/', $personCode) === null) { |
||
| 90 | $result = false; |
||
| 91 | } elseif (((int)substr($personCode, 0, 2) === 32 && !self::newPKValidate($personCode)) || !self::validateDate($personCode)) { |
||
| 92 | $result = false; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $result; |
||
| 96 | } |
||
| 97 | |||
| 98 | public static function validateDate($date) |
||
| 99 | { |
||
| 100 | $date = str_replace('-', '', $date); |
||
| 101 | $day = (int)substr($date, 0, 2); |
||
| 102 | $month = (int)substr($date, 2, 2); |
||
| 103 | |||
| 104 | if ($month < 0 || $month > 12) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | // @formatter:off |
||
| 108 | $months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
||
| 109 | // @formatter:on |
||
| 110 | if ((int)substr($date, 4, 2) % 4 === 0) { |
||
| 111 | $months[1] = 29; |
||
| 112 | } |
||
| 113 | |||
| 114 | return $day > 0 && $day <= $months[$month - 1]; |
||
| 115 | } |
||
| 116 | |||
| 117 | public static function validateDate2($date, $format = 'd-m-Y H:i:s') |
||
| 118 | { |
||
| 119 | $object = \DateTime::createFromFormat($format, $date); |
||
| 120 | |||
| 121 | return $object && $object->format($format) === $date; |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function excelDate($timestamp, $format = 'd-m-Y H:i:s') |
||
| 125 | { |
||
| 126 | $base = 25569; |
||
| 127 | if ($timestamp >= $base) { |
||
| 128 | $unix = ($timestamp - $base) * 86400; |
||
| 129 | $date = gmdate($format, $unix); |
||
| 130 | if (self::validateDate2($date, $format)) { |
||
| 131 | return $date; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return $timestamp; |
||
| 136 | } |
||
| 137 | |||
| 138 | public static function newPKValidate($personCode) |
||
| 139 | { |
||
| 140 | $personCode = str_replace('-', '', $personCode); |
||
| 141 | |||
| 142 | // @formatter:off |
||
| 143 | $calculations = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; |
||
| 144 | // @formatter:on |
||
| 145 | |||
| 146 | $sum = 0; |
||
| 147 | foreach ($calculations as $key => $calculation) { |
||
| 148 | $sum += ($personCode[$key] * $calculation); |
||
| 149 | } |
||
| 150 | |||
| 151 | $remainder = $sum % 11; |
||
| 152 | |||
| 153 | if (1 - $remainder < -1) { |
||
| 154 | return $personCode[10] === (1 - $remainder + 11); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $personCode[10] === (1 - $remainder); |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function swap(&$foo, &$bar) |
||
| 161 | { |
||
| 162 | $tmp = $foo; |
||
| 163 | $foo = $bar; |
||
| 164 | $bar = $tmp; |
||
| 165 | } |
||
| 166 | |||
| 167 | public static function removeDuplicates(&$array) |
||
| 170 | } |
||
| 171 | |||
| 172 | public static function cleanText($text) |
||
| 173 | { |
||
| 174 | return html_entity_decode(self::oneSpace(str_replace(' ?', '', mb_convert_encoding(strip_tags($text), 'UTF-8', 'UTF-8')))); |
||
| 175 | } |
||
| 176 | |||
| 177 | public static function oneSpace($text) |
||
| 178 | { |
||
| 179 | return preg_replace('/\s+/S', ' ', $text); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Transliterates cyrillic text to latin. |
||
| 184 | * |
||
| 185 | * @param string $text cyrillic text |
||
| 186 | * |
||
| 187 | * @return string latin text |
||
| 188 | */ |
||
| 189 | public static function translit2($text) |
||
| 190 | { |
||
| 191 | return str_replace(self::$cyrMap, self::$latMap, $text); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Transliterates latin text to cyrillic. |
||
| 196 | * |
||
| 197 | * @param string $text latin text |
||
| 198 | * |
||
| 199 | * @return string cyrillic text |
||
| 200 | */ |
||
| 201 | public static function translit4($text) |
||
| 202 | { |
||
| 203 | return str_replace(self::$latMap, self::$cyrMap, $text); |
||
| 204 | } |
||
| 205 | |||
| 206 | public static function multiple(array $keys) |
||
| 207 | { |
||
| 208 | foreach ($keys as $key) { |
||
| 209 | if (!\is_array($key)) { |
||
| 210 | return false; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | public static function multiset(array $keys) |
||
| 218 | { |
||
| 219 | foreach ($keys as $key) { |
||
| 220 | if ($key === null) { |
||
| 221 | return false; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | return true; |
||
| 226 | } |
||
| 227 | |||
| 228 | public static function useHttps(Request $request) |
||
| 229 | { |
||
| 230 | $https = false; |
||
| 231 | if ($request->server->has('HTTPS') && 'on' === $request->server->get('HTTPS')) { |
||
| 232 | $https = true; |
||
| 233 | } elseif ($request->server->has('SERVER_PORT') && 443 === (int)$request->server->get('SERVER_PORT')) { |
||
| 234 | $https = true; |
||
| 235 | } elseif ($request->server->has('HTTP_X_FORWARDED_SSL') && 'on' === $request->server->get('HTTP_X_FORWARDED_SSL')) { |
||
| 236 | $https = true; |
||
| 237 | } elseif ($request->server->has('HTTP_X_FORWARDED_PROTO') && 'https' === $request->server->get('HTTP_X_FORWARDED_PROTO')) { |
||
| 238 | $https = true; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $https; |
||
| 242 | } |
||
| 243 | |||
| 244 | public static function getSchema(Request $request) |
||
| 245 | { |
||
| 246 | if (self::useHttps($request)) { |
||
| 247 | return 'https://'; |
||
| 248 | } |
||
| 249 | |||
| 250 | return 'http://'; |
||
| 251 | } |
||
| 252 | |||
| 253 | public static function initialize(array $arguments, ContainerAwareCommand $command) |
||
| 275 | } |
||
| 276 | } |
||
| 277 |