Total Complexity | 42 |
Total Lines | 310 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like StringTool 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 StringTool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class StringTool |
||
6 | { |
||
7 | public static function startsWith($haystack, $needle, $case = true) |
||
8 | { |
||
9 | mb_internal_encoding('UTF-8'); |
||
10 | |||
11 | if ($case) { |
||
12 | return mb_strpos($haystack, $needle, 0) === 0; |
||
13 | } |
||
14 | return mb_stripos($haystack, $needle, 0) === 0; |
||
15 | } |
||
16 | |||
17 | public static function endsWith($haystack,$needle, $case = true) |
||
18 | { |
||
19 | mb_internal_encoding('UTF-8'); |
||
20 | |||
21 | $expectedPosition = mb_strlen($haystack) - mb_strlen($needle); |
||
22 | if ($case) { |
||
23 | return mb_strrpos($haystack, $needle, 0) === $expectedPosition; |
||
24 | } |
||
25 | return mb_strripos($haystack, $needle, 0) === $expectedPosition; |
||
26 | } |
||
27 | |||
28 | public static function contains($haystack, $needle) |
||
29 | { |
||
30 | mb_internal_encoding('UTF-8'); |
||
31 | |||
32 | return mb_strpos($haystack, $needle) !== false; |
||
33 | } |
||
34 | |||
35 | public static function capitalizeFirstLetter($string) |
||
36 | { |
||
37 | mb_internal_encoding('UTF-8'); |
||
38 | |||
39 | return mb_convert_case($string, MB_CASE_TITLE); |
||
40 | } |
||
41 | |||
42 | public static function removePrefix($string, $prefix) |
||
43 | { |
||
44 | return mb_substr($string, mb_strlen($prefix)); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Remove any non-ASCII characters and convert known non-ASCII characters |
||
49 | * to their ASCII equivalents, if possible. |
||
50 | * @param string $string |
||
51 | * @return string |
||
52 | * @link http://gist.github.com/119517 |
||
53 | */ |
||
54 | public static function convertAscii($string) |
||
55 | { |
||
56 | // Replace Single Curly Quotes |
||
57 | $search[] = chr(226).chr(128).chr(152); |
||
|
|||
58 | $replace[] = "'"; |
||
59 | $search[] = chr(226).chr(128).chr(153); |
||
60 | $replace[] = "'"; |
||
61 | |||
62 | // Replace Smart Double Curly Quotes |
||
63 | $search[] = chr(226).chr(128).chr(156); |
||
64 | $replace[] = '"'; |
||
65 | $search[] = chr(226).chr(128).chr(157); |
||
66 | $replace[] = '"'; |
||
67 | |||
68 | // Replace En Dash |
||
69 | $search[] = chr(226).chr(128).chr(147); |
||
70 | $replace[] = '-'; |
||
71 | |||
72 | // Replace Em Dash |
||
73 | $search[] = chr(226).chr(128).chr(148); |
||
74 | $replace[] = '-'; |
||
75 | |||
76 | // Replace Bullet |
||
77 | $search[] = chr(226).chr(128).chr(162); |
||
78 | $replace[] = '*'; |
||
79 | |||
80 | // Replace Middle Dot |
||
81 | $search[] = chr(194).chr(183); |
||
82 | $replace[] = '*'; |
||
83 | |||
84 | // Replace Ellipsis with three consecutive dots |
||
85 | $search[] = chr(226).chr(128).chr(166); |
||
86 | $replace[] = '...'; |
||
87 | |||
88 | // Apply Replacements |
||
89 | $string = str_replace($search, $replace, $string); |
||
90 | |||
91 | // Remove any non-ASCII Characters (keeps cyrillic) |
||
92 | $string = preg_replace("/[^\x80-\xFF, \x01-\x7F]/","", $string); |
||
93 | |||
94 | return $string; |
||
95 | } |
||
96 | |||
97 | |||
98 | public static function unaccent($string) |
||
99 | { |
||
100 | |||
101 | } |
||
102 | |||
103 | public static function cleanSmsContent($message, $transliterateCyrillic = true) |
||
109 | } |
||
110 | |||
111 | public static function getArrayWithRemovedPrefixRecursive(array $input, $prefix) |
||
112 | { |
||
113 | mb_internal_encoding('UTF-8'); |
||
114 | |||
115 | $return = array(); |
||
116 | foreach ($input as $key => $value) { |
||
117 | if (strpos($key, $prefix) === 0) { |
||
118 | $key = substr($key, mb_strlen($prefix)); |
||
119 | } |
||
120 | |||
121 | if (is_array($value)) { |
||
122 | $value = self::getArrayWithRemovedPrefixRecursive($value, $prefix); |
||
123 | } |
||
124 | |||
125 | $return[$key] = $value; |
||
126 | } |
||
127 | return $return; |
||
128 | } |
||
129 | |||
130 | // possible TODO fix type Combionation to Combination |
||
131 | public static function createCombionationsFromStringWords($phrase = ''): array |
||
132 | { |
||
133 | $phrase = preg_replace('/\s+/', ' ', strtolower($phrase)); |
||
134 | $words = explode(' ', $phrase); |
||
135 | |||
136 | return self::createCombionationsFromWords($words); |
||
137 | } |
||
138 | |||
139 | public static function createCombionationsFromWords($words = []): array |
||
140 | { |
||
141 | array_walk($words, 'trim'); |
||
142 | |||
143 | foreach ($words as $x => $value) { |
||
144 | if (trim($value) === '') { |
||
145 | unset($words[$x]); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return self::permuteCombinations($words); |
||
150 | } |
||
151 | |||
152 | private static function permuteCombinations($items, $perms = []): array |
||
169 | } |
||
170 | |||
171 | public static function toLower($string) |
||
172 | { |
||
173 | mb_internal_encoding('UTF-8'); |
||
174 | |||
175 | return mb_strtolower($string); |
||
176 | } |
||
177 | |||
178 | public static function toUpper($string) |
||
179 | { |
||
180 | mb_internal_encoding('UTF-8'); |
||
181 | |||
182 | return mb_strtoupper($string); |
||
183 | } |
||
184 | |||
185 | public static function toUTF8($string) |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param int $length |
||
193 | * @return string |
||
194 | */ |
||
195 | public static function random($length = 8) |
||
196 | { |
||
197 | $numbers = '0123456789'; |
||
198 | $numbersLength = strlen($numbers); |
||
199 | |||
200 | $characters = 'abcdefghijklmnopqrstuvwxyz'; |
||
201 | $charactersLength = strlen($characters); |
||
202 | |||
203 | $string = ''; |
||
204 | for ($i = 0; $i < $length; $i++) { |
||
205 | $symbol = $numbers[rand(0, $numbersLength - 1)]; |
||
206 | if (rand(0, 1)) { |
||
207 | $symbol = $characters[rand(0, $charactersLength - 1)]; |
||
208 | if (rand(0, 1)) { |
||
209 | $symbol = self::toUpper($symbol); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | $string .= $symbol; |
||
214 | } |
||
215 | |||
216 | return $string; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param $length |
||
221 | * @return string |
||
222 | */ |
||
223 | public static function password($length = 8) |
||
224 | { |
||
225 | if ($length < 8) { |
||
226 | throw new \InvalidArgumentException('Length must be more than 7'); |
||
227 | } |
||
228 | |||
229 | $string = self::random($length); |
||
230 | |||
231 | if (!preg_match('/[a-z]/', $string)) { |
||
232 | return self::random($length); |
||
233 | } |
||
234 | |||
235 | if (!preg_match('/[A-Z]/', $string)) { |
||
236 | return self::random($length); |
||
237 | } |
||
238 | |||
239 | if (!preg_match('/\d/', $string)) { |
||
240 | return self::random($length); |
||
241 | } |
||
242 | |||
243 | return $string; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Transliterates polish chars to latin char equivalents |
||
248 | * @param $string |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public static function polishToLatin($string) |
||
252 | { |
||
253 | $polishChars = ['Ą','Ć','Ę','Ł','Ó','Ś','Ź','Ż','Ń','ą','ć','ę','ł','ó','ś','ź','ż','ń']; |
||
254 | $latinChars = ['A','C','E','L','O','S','Z','Z','N','a','c','e','l','o','s','z','z','n']; |
||
255 | |||
256 | return str_replace($polishChars, $latinChars, $string); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Tries to parse correct string from various price string formats |
||
261 | * |
||
262 | * '2000 EUR' -> '2000.00' |
||
263 | * '2000.00' -> '2000.00' |
||
264 | * '2,000 EUR' -> '2000.00' |
||
265 | * '2.000' -> '2000.00' |
||
266 | * '2.320,34 EUR' -> '2320.34' |
||
267 | * '2,000,000.31'-> '2000000.31' |
||
268 | * '2.000.000' -> '2000000.00' |
||
269 | * '2.00' -> '2.00' |
||
270 | * '2,000€' -> '2000.00' |
||
271 | * |
||
272 | * @param $price |
||
273 | * @return string |
||
274 | */ |
||
275 | public static function cleanupPriceString($price) |
||
315 | } |
||
316 | } |
||
317 |