Total Complexity | 41 |
Total Lines | 215 |
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(str_replace('_', ' ', $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 getUniqueId($length = 20) |
||
59 | { |
||
60 | try { |
||
61 | $output = bin2hex(random_bytes($length)); |
||
62 | } catch (\Exception $exception) { |
||
63 | $output = substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int)ceil((int)($length / \strlen($x))))), 1, $length); |
||
64 | } |
||
65 | |||
66 | return $output; |
||
67 | } |
||
68 | |||
69 | public static function validatePersonCode($personCode = null) |
||
70 | { |
||
71 | if ($personCode !== null) { |
||
72 | $personCode = str_replace('-', '', $personCode); |
||
73 | |||
74 | if (\strlen($personCode) !== 11) { |
||
75 | return 'error_length'; |
||
76 | } |
||
77 | |||
78 | if (preg_match('/^\d+$/', $personCode) === null) { |
||
79 | return 'error_symbols'; |
||
80 | } |
||
81 | |||
82 | if (((int)substr($personCode, 0, 2) === 32 && !self::newPKValidate($personCode)) || !self::validateDate($personCode)) { |
||
83 | return 'error_invalid'; |
||
84 | } |
||
85 | |||
86 | return true; |
||
87 | } |
||
88 | |||
89 | return 'error_empty'; |
||
90 | } |
||
91 | |||
92 | public static function validateDate($date) |
||
93 | { |
||
94 | $date = str_replace('-', '', $date); |
||
95 | $day = (int)substr($date, 0, 2); |
||
96 | $month = (int)substr($date, 2, 2); |
||
97 | |||
98 | if ($month < 0 || $month > 12) { |
||
99 | return false; |
||
100 | } |
||
101 | // @formatter:off |
||
102 | $months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
||
103 | // @formatter:on |
||
104 | if ((int)substr($date, 4, 2) % 4 === 0) { |
||
105 | $months[1] = 29; |
||
106 | } |
||
107 | |||
108 | return $day > 0 && $day <= $months[$month - 1]; |
||
109 | } |
||
110 | |||
111 | public static function validateDate2($date, $format = 'd-m-Y H:i:s') |
||
112 | { |
||
113 | $object = \DateTime::createFromFormat($format, $date); |
||
114 | |||
115 | return $object && $object->format($format) === $date; |
||
116 | } |
||
117 | |||
118 | public static function excelDate($timestamp, $format = 'd-m-Y H:i:s') |
||
119 | { |
||
120 | $base = 25569; |
||
121 | if ($timestamp >= $base) { |
||
122 | $unix = ($timestamp - $base) * 86400; |
||
123 | $date = gmdate($format, $unix); |
||
124 | if (self::validateDate2($date, $format)) { |
||
125 | return $date; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | return $timestamp; |
||
130 | } |
||
131 | |||
132 | public static function newPKValidate($personCode) |
||
133 | { |
||
134 | $personCode = str_replace('-', '', $personCode); |
||
135 | |||
136 | // @formatter:off |
||
137 | $calculations = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; |
||
138 | // @formatter:on |
||
139 | |||
140 | $sum = 0; |
||
141 | foreach ($calculations as $key => $calculation) { |
||
142 | $sum += ($personCode[$key] * $calculation); |
||
143 | } |
||
144 | |||
145 | $remainder = $sum % 11; |
||
146 | |||
147 | if (1 - $remainder < -1) { |
||
148 | return $personCode[10] === (1 - $remainder + 11); |
||
149 | } |
||
150 | |||
151 | return $personCode[10] === (1 - $remainder); |
||
152 | } |
||
153 | |||
154 | public static function swap(&$foo, &$bar) |
||
155 | { |
||
156 | $tmp = $foo; |
||
157 | $foo = $bar; |
||
158 | $bar = $tmp; |
||
159 | } |
||
160 | |||
161 | public static function removeDuplicates(&$array) |
||
162 | { |
||
163 | $array = array_map('unserialize', array_unique(array_map('serialize', $array))); |
||
164 | } |
||
165 | |||
166 | public static function cleanText($text) |
||
167 | { |
||
168 | return html_entity_decode(self::oneSpace(str_replace(' ?', '', mb_convert_encoding(strip_tags($text), 'UTF-8', 'UTF-8')))); |
||
169 | } |
||
170 | |||
171 | public static function oneSpace($text) |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Transliterates cyrillic text to latin. |
||
178 | * |
||
179 | * @param string $text cyrillic text |
||
180 | * |
||
181 | * @return string latin text |
||
182 | */ |
||
183 | public static function translit2($text) |
||
184 | { |
||
185 | return str_replace(self::$cyrMap, self::$latMap, $text); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Transliterates latin text to cyrillic. |
||
190 | * |
||
191 | * @param string $text latin text |
||
192 | * |
||
193 | * @return string cyrillic text |
||
194 | */ |
||
195 | public static function translit4($text) |
||
198 | } |
||
199 | |||
200 | public static function multiple(array $keys) |
||
201 | { |
||
209 | } |
||
210 | |||
211 | public static function multiset(array $keys) |
||
222 |