Passed
Push — master ( 7dc692...212065 )
by Sylvain
02:20
created

Format::removeAccents()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 338
Code Lines 314

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 314
nc 2
nop 1
dl 0
loc 338
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix;
6
7
use Money\Currencies\ISOCurrencies;
8
use Money\Formatter\DecimalMoneyFormatter;
9
use Money\Money;
10
11
abstract class Format
12
{
13
    /**
14
     * Truncate a string and append '…' at the end
15
     *
16
     * @param string $ellipsis the string to indicate truncation happened
17
     *
18
     * @return string truncated string
19
     */
20 5
    public static function truncate(string $string, int $maxLength, string $ellipsis = '…'): string
21
    {
22 5
        if (mb_strlen($string) > $maxLength) {
23 3
            $string = mb_substr($string, 0, $maxLength - mb_strlen($ellipsis));
24 3
            $string .= $ellipsis;
25
        }
26
27 5
        return $string;
28
    }
29
30
    /**
31
     * Shortcut to format money
32
     */
33
    public static function money(Money $money): string
34
    {
35
        $currencies = new ISOCurrencies();
36
        $moneyFormatter = new DecimalMoneyFormatter($currencies);
37
38
        return $moneyFormatter->format($money);
39
    }
40
41
    /**
42
     * Converts all accent characters to ASCII characters.
43
     *
44
     * If there are no accent characters, then the string given is just returned.
45
     *
46
     * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/formatting.php
47
     *
48
     * @param mixed $string
49
     */
50 3
    public static function removeAccents($string): string
51
    {
52 3
        if (!preg_match('/[\x80-\xff]/', $string)) {
53
            return $string;
54
        }
55
56
        $chars = [
57
            // Decompositions for Latin-1 Supplement.
58 3
            'ª' => 'a',
59
            'º' => 'o',
60
            'À' => 'A',
61
            'Á' => 'A',
62
            'Â' => 'A',
63
            'Ã' => 'A',
64
            'Ä' => 'A',
65
            'Å' => 'A',
66
            'Æ' => 'AE',
67
            'Ç' => 'C',
68
            'È' => 'E',
69
            'É' => 'E',
70
            'Ê' => 'E',
71
            'Ë' => 'E',
72
            'Ì' => 'I',
73
            'Í' => 'I',
74
            'Î' => 'I',
75
            'Ï' => 'I',
76
            'Ð' => 'D',
77
            'Ñ' => 'N',
78
            'Ò' => 'O',
79
            'Ó' => 'O',
80
            'Ô' => 'O',
81
            'Õ' => 'O',
82
            'Ö' => 'O',
83
            'Ù' => 'U',
84
            'Ú' => 'U',
85
            'Û' => 'U',
86
            'Ü' => 'U',
87
            'Ý' => 'Y',
88
            'Þ' => 'TH',
89
            'ß' => 's',
90
            'à' => 'a',
91
            'á' => 'a',
92
            'â' => 'a',
93
            'ã' => 'a',
94
            'ä' => 'a',
95
            'å' => 'a',
96
            'æ' => 'ae',
97
            'ç' => 'c',
98
            'è' => 'e',
99
            'é' => 'e',
100
            'ê' => 'e',
101
            'ë' => 'e',
102
            'ì' => 'i',
103
            'í' => 'i',
104
            'î' => 'i',
105
            'ï' => 'i',
106
            'ð' => 'd',
107
            'ñ' => 'n',
108
            'ò' => 'o',
109
            'ó' => 'o',
110
            'ô' => 'o',
111
            'õ' => 'o',
112
            'ö' => 'o',
113
            'ø' => 'o',
114
            'ù' => 'u',
115
            'ú' => 'u',
116
            'û' => 'u',
117
            'ü' => 'u',
118
            'ý' => 'y',
119
            'þ' => 'th',
120
            'ÿ' => 'y',
121
            'Ø' => 'O',
122
            // Decompositions for Latin Extended-A.
123
            'Ā' => 'A',
124
            'ā' => 'a',
125
            'Ă' => 'A',
126
            'ă' => 'a',
127
            'Ą' => 'A',
128
            'ą' => 'a',
129
            'Ć' => 'C',
130
            'ć' => 'c',
131
            'Ĉ' => 'C',
132
            'ĉ' => 'c',
133
            'Ċ' => 'C',
134
            'ċ' => 'c',
135
            'Č' => 'C',
136
            'č' => 'c',
137
            'Ď' => 'D',
138
            'ď' => 'd',
139
            'Đ' => 'D',
140
            'đ' => 'd',
141
            'Ē' => 'E',
142
            'ē' => 'e',
143
            'Ĕ' => 'E',
144
            'ĕ' => 'e',
145
            'Ė' => 'E',
146
            'ė' => 'e',
147
            'Ę' => 'E',
148
            'ę' => 'e',
149
            'Ě' => 'E',
150
            'ě' => 'e',
151
            'Ĝ' => 'G',
152
            'ĝ' => 'g',
153
            'Ğ' => 'G',
154
            'ğ' => 'g',
155
            'Ġ' => 'G',
156
            'ġ' => 'g',
157
            'Ģ' => 'G',
158
            'ģ' => 'g',
159
            'Ĥ' => 'H',
160
            'ĥ' => 'h',
161
            'Ħ' => 'H',
162
            'ħ' => 'h',
163
            'Ĩ' => 'I',
164
            'ĩ' => 'i',
165
            'Ī' => 'I',
166
            'ī' => 'i',
167
            'Ĭ' => 'I',
168
            'ĭ' => 'i',
169
            'Į' => 'I',
170
            'į' => 'i',
171
            'İ' => 'I',
172
            'ı' => 'i',
173
            'IJ' => 'IJ',
174
            'ij' => 'ij',
175
            'Ĵ' => 'J',
176
            'ĵ' => 'j',
177
            'Ķ' => 'K',
178
            'ķ' => 'k',
179
            'ĸ' => 'k',
180
            'Ĺ' => 'L',
181
            'ĺ' => 'l',
182
            'Ļ' => 'L',
183
            'ļ' => 'l',
184
            'Ľ' => 'L',
185
            'ľ' => 'l',
186
            'Ŀ' => 'L',
187
            'ŀ' => 'l',
188
            'Ł' => 'L',
189
            'ł' => 'l',
190
            'Ń' => 'N',
191
            'ń' => 'n',
192
            'Ņ' => 'N',
193
            'ņ' => 'n',
194
            'Ň' => 'N',
195
            'ň' => 'n',
196
            'ʼn' => 'n',
197
            'Ŋ' => 'N',
198
            'ŋ' => 'n',
199
            'Ō' => 'O',
200
            'ō' => 'o',
201
            'Ŏ' => 'O',
202
            'ŏ' => 'o',
203
            'Ő' => 'O',
204
            'ő' => 'o',
205
            'Œ' => 'OE',
206
            'œ' => 'oe',
207
            'Ŕ' => 'R',
208
            'ŕ' => 'r',
209
            'Ŗ' => 'R',
210
            'ŗ' => 'r',
211
            'Ř' => 'R',
212
            'ř' => 'r',
213
            'Ś' => 'S',
214
            'ś' => 's',
215
            'Ŝ' => 'S',
216
            'ŝ' => 's',
217
            'Ş' => 'S',
218
            'ş' => 's',
219
            'Š' => 'S',
220
            'š' => 's',
221
            'Ţ' => 'T',
222
            'ţ' => 't',
223
            'Ť' => 'T',
224
            'ť' => 't',
225
            'Ŧ' => 'T',
226
            'ŧ' => 't',
227
            'Ũ' => 'U',
228
            'ũ' => 'u',
229
            'Ū' => 'U',
230
            'ū' => 'u',
231
            'Ŭ' => 'U',
232
            'ŭ' => 'u',
233
            'Ů' => 'U',
234
            'ů' => 'u',
235
            'Ű' => 'U',
236
            'ű' => 'u',
237
            'Ų' => 'U',
238
            'ų' => 'u',
239
            'Ŵ' => 'W',
240
            'ŵ' => 'w',
241
            'Ŷ' => 'Y',
242
            'ŷ' => 'y',
243
            'Ÿ' => 'Y',
244
            'Ź' => 'Z',
245
            'ź' => 'z',
246
            'Ż' => 'Z',
247
            'ż' => 'z',
248
            'Ž' => 'Z',
249
            'ž' => 'z',
250
            'ſ' => 's',
251
            // Decompositions for Latin Extended-B.
252
            'Ș' => 'S',
253
            'ș' => 's',
254
            'Ț' => 'T',
255
            'ț' => 't',
256
            // Euro sign.
257
            '€' => 'E',
258
            // GBP (Pound) sign.
259
            '£' => '',
260
            // Vowels with diacritic (Vietnamese).
261
            // Unmarked.
262
            'Ơ' => 'O',
263
            'ơ' => 'o',
264
            'Ư' => 'U',
265
            'ư' => 'u',
266
            // Grave accent.
267
            'Ầ' => 'A',
268
            'ầ' => 'a',
269
            'Ằ' => 'A',
270
            'ằ' => 'a',
271
            'Ề' => 'E',
272
            'ề' => 'e',
273
            'Ồ' => 'O',
274
            'ồ' => 'o',
275
            'Ờ' => 'O',
276
            'ờ' => 'o',
277
            'Ừ' => 'U',
278
            'ừ' => 'u',
279
            'Ỳ' => 'Y',
280
            'ỳ' => 'y',
281
            // Hook.
282
            'Ả' => 'A',
283
            'ả' => 'a',
284
            'Ẩ' => 'A',
285
            'ẩ' => 'a',
286
            'Ẳ' => 'A',
287
            'ẳ' => 'a',
288
            'Ẻ' => 'E',
289
            'ẻ' => 'e',
290
            'Ể' => 'E',
291
            'ể' => 'e',
292
            'Ỉ' => 'I',
293
            'ỉ' => 'i',
294
            'Ỏ' => 'O',
295
            'ỏ' => 'o',
296
            'Ổ' => 'O',
297
            'ổ' => 'o',
298
            'Ở' => 'O',
299
            'ở' => 'o',
300
            'Ủ' => 'U',
301
            'ủ' => 'u',
302
            'Ử' => 'U',
303
            'ử' => 'u',
304
            'Ỷ' => 'Y',
305
            'ỷ' => 'y',
306
            // Tilde.
307
            'Ẫ' => 'A',
308
            'ẫ' => 'a',
309
            'Ẵ' => 'A',
310
            'ẵ' => 'a',
311
            'Ẽ' => 'E',
312
            'ẽ' => 'e',
313
            'Ễ' => 'E',
314
            'ễ' => 'e',
315
            'Ỗ' => 'O',
316
            'ỗ' => 'o',
317
            'Ỡ' => 'O',
318
            'ỡ' => 'o',
319
            'Ữ' => 'U',
320
            'ữ' => 'u',
321
            'Ỹ' => 'Y',
322
            'ỹ' => 'y',
323
            // Acute accent.
324
            'Ấ' => 'A',
325
            'ấ' => 'a',
326
            'Ắ' => 'A',
327
            'ắ' => 'a',
328
            'Ế' => 'E',
329
            'ế' => 'e',
330
            'Ố' => 'O',
331
            'ố' => 'o',
332
            'Ớ' => 'O',
333
            'ớ' => 'o',
334
            'Ứ' => 'U',
335
            'ứ' => 'u',
336
            // Dot below.
337
            'Ạ' => 'A',
338
            'ạ' => 'a',
339
            'Ậ' => 'A',
340
            'ậ' => 'a',
341
            'Ặ' => 'A',
342
            'ặ' => 'a',
343
            'Ẹ' => 'E',
344
            'ẹ' => 'e',
345
            'Ệ' => 'E',
346
            'ệ' => 'e',
347
            'Ị' => 'I',
348
            'ị' => 'i',
349
            'Ọ' => 'O',
350
            'ọ' => 'o',
351
            'Ộ' => 'O',
352
            'ộ' => 'o',
353
            'Ợ' => 'O',
354
            'ợ' => 'o',
355
            'Ụ' => 'U',
356
            'ụ' => 'u',
357
            'Ự' => 'U',
358
            'ự' => 'u',
359
            'Ỵ' => 'Y',
360
            'ỵ' => 'y',
361
            // Vowels with diacritic (Chinese, Hanyu Pinyin).
362
            'ɑ' => 'a',
363
            // Macron.
364
            'Ǖ' => 'U',
365
            'ǖ' => 'u',
366
            // Acute accent.
367
            'Ǘ' => 'U',
368
            'ǘ' => 'u',
369
            // Caron.
370
            'Ǎ' => 'A',
371
            'ǎ' => 'a',
372
            'Ǐ' => 'I',
373
            'ǐ' => 'i',
374
            'Ǒ' => 'O',
375
            'ǒ' => 'o',
376
            'Ǔ' => 'U',
377
            'ǔ' => 'u',
378
            'Ǚ' => 'U',
379
            'ǚ' => 'u',
380
            // Grave accent.
381
            'Ǜ' => 'U',
382
            'ǜ' => 'u',
383
        ];
384
385 3
        $string = strtr($string, $chars);
386
387 3
        return $string;
388
    }
389
}
390