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