Total Complexity | 112 |
Total Lines | 659 |
Duplicated Lines | 0 % |
Coverage | 98.34% |
Changes | 0 |
Complex classes like TextData 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 TextData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class TextData |
||
10 | { |
||
11 | private static $invalidChars; |
||
12 | |||
13 | 15 | private static function unicodeToOrd($character) |
|
16 | } |
||
17 | |||
18 | /** |
||
19 | * CHARACTER. |
||
20 | * |
||
21 | * @param string $character Value |
||
22 | * |
||
23 | * @return string |
||
24 | */ |
||
25 | 11 | public static function CHARACTER($character) |
|
26 | { |
||
27 | 11 | $character = Functions::flattenSingleValue($character); |
|
28 | |||
29 | 11 | if ((!is_numeric($character)) || ($character < 0)) { |
|
30 | 2 | return Functions::VALUE(); |
|
31 | } |
||
32 | |||
33 | 9 | if (function_exists('iconv')) { |
|
34 | 9 | return iconv('UCS-4LE', 'UTF-8', pack('V', $character)); |
|
35 | } |
||
36 | |||
37 | return mb_convert_encoding('&#' . (int) $character . ';', 'UTF-8', 'HTML-ENTITIES'); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * TRIMNONPRINTABLE. |
||
42 | * |
||
43 | * @param mixed $stringValue Value to check |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 5 | public static function TRIMNONPRINTABLE($stringValue = '') |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * TRIMSPACES. |
||
68 | * |
||
69 | * @param mixed $stringValue Value to check |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | 7 | public static function TRIMSPACES($stringValue = '') |
|
85 | } |
||
86 | |||
87 | 6 | private static function convertBooleanValue($value) |
|
88 | { |
||
89 | 6 | if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) { |
|
90 | 1 | return (int) $value; |
|
91 | } |
||
92 | |||
93 | 5 | return ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * ASCIICODE. |
||
98 | * |
||
99 | * @param string $characters Value |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | 17 | public static function ASCIICODE($characters) |
|
104 | { |
||
105 | 17 | if (($characters === null) || ($characters === '')) { |
|
106 | 2 | return Functions::VALUE(); |
|
|
|||
107 | } |
||
108 | 15 | $characters = Functions::flattenSingleValue($characters); |
|
109 | 15 | if (is_bool($characters)) { |
|
110 | 1 | $characters = self::convertBooleanValue($characters); |
|
111 | } |
||
112 | |||
113 | 15 | $character = $characters; |
|
114 | 15 | if (mb_strlen($characters, 'UTF-8') > 1) { |
|
115 | 9 | $character = mb_substr($characters, 0, 1, 'UTF-8'); |
|
116 | } |
||
117 | |||
118 | 15 | return self::unicodeToOrd($character); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * CONCATENATE. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public static function CONCATENATE(...$args) |
|
127 | { |
||
128 | 4 | $returnValue = ''; |
|
129 | |||
130 | // Loop through arguments |
||
131 | 4 | $aArgs = Functions::flattenArray($args); |
|
132 | 4 | foreach ($aArgs as $arg) { |
|
133 | 4 | if (is_bool($arg)) { |
|
134 | 2 | $arg = self::convertBooleanValue($arg); |
|
135 | } |
||
136 | 4 | $returnValue .= $arg; |
|
137 | } |
||
138 | |||
139 | 4 | return $returnValue; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * DOLLAR. |
||
144 | * |
||
145 | * This function converts a number to text using currency format, with the decimals rounded to the specified place. |
||
146 | * The format used is $#,##0.00_);($#,##0.00).. |
||
147 | * |
||
148 | * @param float $value The value to format |
||
149 | * @param int $decimals The number of digits to display to the right of the decimal point. |
||
150 | * If decimals is negative, number is rounded to the left of the decimal point. |
||
151 | * If you omit decimals, it is assumed to be 2 |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 6 | public static function DOLLAR($value = 0, $decimals = 2) |
|
156 | { |
||
157 | 6 | $value = Functions::flattenSingleValue($value); |
|
158 | 6 | $decimals = $decimals === null ? 0 : Functions::flattenSingleValue($decimals); |
|
159 | |||
160 | // Validate parameters |
||
161 | 6 | if (!is_numeric($value) || !is_numeric($decimals)) { |
|
162 | 2 | return Functions::NAN(); |
|
163 | } |
||
164 | 4 | $decimals = floor($decimals); |
|
165 | |||
166 | 4 | $mask = '$#,##0'; |
|
167 | 4 | if ($decimals > 0) { |
|
168 | 2 | $mask .= '.' . str_repeat('0', $decimals); |
|
169 | } else { |
||
170 | 2 | $round = pow(10, abs($decimals)); |
|
171 | 2 | if ($value < 0) { |
|
172 | $round = 0 - $round; |
||
173 | } |
||
174 | 2 | $value = MathTrig::MROUND($value, $round); |
|
175 | } |
||
176 | |||
177 | 4 | return NumberFormat::toFormattedString($value, $mask); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * SEARCHSENSITIVE. |
||
182 | * |
||
183 | * @param string $needle The string to look for |
||
184 | * @param string $haystack The string in which to look |
||
185 | * @param int $offset Offset within $haystack |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 13 | public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1) |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * SEARCHINSENSITIVE. |
||
217 | * |
||
218 | * @param string $needle The string to look for |
||
219 | * @param string $haystack The string in which to look |
||
220 | * @param int $offset Offset within $haystack |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 11 | public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1) |
|
225 | { |
||
226 | 11 | $needle = Functions::flattenSingleValue($needle); |
|
227 | 11 | $haystack = Functions::flattenSingleValue($haystack); |
|
228 | 11 | $offset = Functions::flattenSingleValue($offset); |
|
229 | |||
230 | 11 | if (!is_bool($needle)) { |
|
231 | 11 | if (is_bool($haystack)) { |
|
232 | 2 | $haystack = ($haystack) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
233 | } |
||
234 | |||
235 | 11 | if (($offset > 0) && (StringHelper::countCharacters($haystack) > $offset)) { |
|
236 | 11 | if (StringHelper::countCharacters($needle) === 0) { |
|
237 | return $offset; |
||
238 | } |
||
239 | |||
240 | 11 | $pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8'); |
|
241 | 11 | if ($pos !== false) { |
|
242 | 9 | return ++$pos; |
|
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | 2 | return Functions::VALUE(); |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * FIXEDFORMAT. |
||
252 | * |
||
253 | * @param mixed $value Value to check |
||
254 | * @param int $decimals |
||
255 | * @param bool $no_commas |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 5 | public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false) |
|
260 | { |
||
261 | 5 | $value = Functions::flattenSingleValue($value); |
|
262 | 5 | $decimals = Functions::flattenSingleValue($decimals); |
|
263 | 5 | $no_commas = Functions::flattenSingleValue($no_commas); |
|
264 | |||
265 | // Validate parameters |
||
266 | 5 | if (!is_numeric($value) || !is_numeric($decimals)) { |
|
267 | 2 | return Functions::NAN(); |
|
268 | } |
||
269 | 3 | $decimals = floor($decimals); |
|
270 | |||
271 | 3 | $valueResult = round($value, $decimals); |
|
272 | 3 | if ($decimals < 0) { |
|
273 | $decimals = 0; |
||
274 | } |
||
275 | 3 | if (!$no_commas) { |
|
276 | 1 | $valueResult = number_format($valueResult, $decimals); |
|
1 ignored issue
–
show
|
|||
277 | } |
||
278 | |||
279 | 3 | return (string) $valueResult; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * LEFT. |
||
284 | * |
||
285 | * @param string $value Value |
||
286 | * @param int $chars Number of characters |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 11 | public static function LEFT($value = '', $chars = 1) |
|
291 | { |
||
292 | 11 | $value = Functions::flattenSingleValue($value); |
|
293 | 11 | $chars = Functions::flattenSingleValue($chars); |
|
294 | |||
295 | 11 | if ($chars < 0) { |
|
296 | 1 | return Functions::VALUE(); |
|
297 | } |
||
298 | |||
299 | 10 | if (is_bool($value)) { |
|
300 | 2 | $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
301 | } |
||
302 | |||
303 | 10 | return mb_substr($value, 0, $chars, 'UTF-8'); |
|
304 | } |
||
305 | |||
306 | /** |
||
307 | * MID. |
||
308 | * |
||
309 | * @param string $value Value |
||
310 | * @param int $start Start character |
||
311 | * @param int $chars Number of characters |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 9 | public static function MID($value = '', $start = 1, $chars = null) |
|
316 | { |
||
317 | 9 | $value = Functions::flattenSingleValue($value); |
|
318 | 9 | $start = Functions::flattenSingleValue($start); |
|
319 | 9 | $chars = Functions::flattenSingleValue($chars); |
|
320 | |||
321 | 9 | if (($start < 1) || ($chars < 0)) { |
|
322 | 2 | return Functions::VALUE(); |
|
323 | } |
||
324 | |||
325 | 7 | if (is_bool($value)) { |
|
326 | 2 | $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
327 | } |
||
328 | |||
329 | 7 | if (empty($chars)) { |
|
330 | 1 | return ''; |
|
331 | } |
||
332 | |||
333 | 6 | return mb_substr($value, --$start, $chars, 'UTF-8'); |
|
334 | } |
||
335 | |||
336 | /** |
||
337 | * RIGHT. |
||
338 | * |
||
339 | * @param string $value Value |
||
340 | * @param int $chars Number of characters |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | 11 | public static function RIGHT($value = '', $chars = 1) |
|
345 | { |
||
346 | 11 | $value = Functions::flattenSingleValue($value); |
|
347 | 11 | $chars = Functions::flattenSingleValue($chars); |
|
348 | |||
349 | 11 | if ($chars < 0) { |
|
350 | 1 | return Functions::VALUE(); |
|
351 | } |
||
352 | |||
353 | 10 | if (is_bool($value)) { |
|
354 | 2 | $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
355 | } |
||
356 | |||
357 | 10 | return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8'); |
|
358 | } |
||
359 | |||
360 | /** |
||
361 | * STRINGLENGTH. |
||
362 | * |
||
363 | * @param string $value Value |
||
364 | * |
||
365 | * @return int |
||
366 | */ |
||
367 | 11 | public static function STRINGLENGTH($value = '') |
|
368 | { |
||
369 | 11 | $value = Functions::flattenSingleValue($value); |
|
370 | |||
371 | 11 | if (is_bool($value)) { |
|
372 | 2 | $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
373 | } |
||
374 | |||
375 | 11 | return mb_strlen($value, 'UTF-8'); |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * LOWERCASE. |
||
380 | * |
||
381 | * Converts a string value to upper case. |
||
382 | * |
||
383 | * @param string $mixedCaseString |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | 4 | public static function LOWERCASE($mixedCaseString) |
|
388 | { |
||
389 | 4 | $mixedCaseString = Functions::flattenSingleValue($mixedCaseString); |
|
390 | |||
391 | 4 | if (is_bool($mixedCaseString)) { |
|
392 | 2 | $mixedCaseString = ($mixedCaseString) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
393 | } |
||
394 | |||
395 | 4 | return StringHelper::strToLower($mixedCaseString); |
|
396 | } |
||
397 | |||
398 | /** |
||
399 | * UPPERCASE. |
||
400 | * |
||
401 | * Converts a string value to upper case. |
||
402 | * |
||
403 | * @param string $mixedCaseString |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | 4 | public static function UPPERCASE($mixedCaseString) |
|
416 | } |
||
417 | |||
418 | /** |
||
419 | * PROPERCASE. |
||
420 | * |
||
421 | * Converts a string value to upper case. |
||
422 | * |
||
423 | * @param string $mixedCaseString |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | 3 | public static function PROPERCASE($mixedCaseString) |
|
428 | { |
||
429 | 3 | $mixedCaseString = Functions::flattenSingleValue($mixedCaseString); |
|
430 | |||
431 | 3 | if (is_bool($mixedCaseString)) { |
|
432 | 2 | $mixedCaseString = ($mixedCaseString) ? Calculation::getTRUE() : Calculation::getFALSE(); |
|
433 | } |
||
434 | |||
435 | 3 | return StringHelper::strToTitle($mixedCaseString); |
|
436 | } |
||
437 | |||
438 | /** |
||
439 | * REPLACE. |
||
440 | * |
||
441 | * @param string $oldText String to modify |
||
442 | * @param int $start Start character |
||
443 | * @param int $chars Number of characters |
||
444 | * @param string $newText String to replace in defined position |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | 5 | public static function REPLACE($oldText, $start, $chars, $newText) |
|
459 | } |
||
460 | |||
461 | /** |
||
462 | * SUBSTITUTE. |
||
463 | * |
||
464 | * @param string $text Value |
||
465 | * @param string $fromText From Value |
||
466 | * @param string $toText To Value |
||
467 | * @param int $instance Instance Number |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | 6 | public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) |
|
496 | } |
||
497 | |||
498 | /** |
||
499 | * RETURNSTRING. |
||
500 | * |
||
501 | * @param mixed $testValue Value to check |
||
502 | * |
||
503 | * @return null|string |
||
504 | */ |
||
505 | 5 | public static function RETURNSTRING($testValue = '') |
|
506 | { |
||
507 | 5 | $testValue = Functions::flattenSingleValue($testValue); |
|
508 | |||
509 | 5 | if (is_string($testValue)) { |
|
510 | 2 | return $testValue; |
|
511 | } |
||
512 | |||
513 | 3 | return null; |
|
514 | } |
||
515 | |||
516 | /** |
||
517 | * TEXTFORMAT. |
||
518 | * |
||
519 | * @param mixed $value Value to check |
||
520 | * @param string $format Format mask to use |
||
521 | * |
||
522 | * @return string |
||
523 | */ |
||
524 | 13 | public static function TEXTFORMAT($value, $format) |
|
525 | { |
||
526 | 13 | $value = Functions::flattenSingleValue($value); |
|
527 | 13 | $format = Functions::flattenSingleValue($format); |
|
528 | |||
529 | 13 | if ((is_string($value)) && (!is_numeric($value)) && Date::isDateTimeFormatCode($format)) { |
|
530 | 2 | $value = DateTime::DATEVALUE($value); |
|
531 | } |
||
532 | |||
533 | 13 | return (string) NumberFormat::toFormattedString($value, $format); |
|
534 | } |
||
535 | |||
536 | /** |
||
537 | * VALUE. |
||
538 | * |
||
539 | * @param mixed $value Value to check |
||
540 | * |
||
541 | * @return bool |
||
542 | */ |
||
543 | 10 | public static function VALUE($value = '') |
|
580 | } |
||
581 | |||
582 | /** |
||
583 | * NUMBERVALUE. |
||
584 | * |
||
585 | * @param mixed $value Value to check |
||
586 | * @param string $decimalSeparator decimal separator, defaults to locale defined value |
||
587 | * @param string $groupSeparator group/thosands separator, defaults to locale defined value |
||
588 | * |
||
589 | * @return float|string |
||
590 | */ |
||
591 | 12 | public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null) |
|
626 | } |
||
627 | |||
628 | /** |
||
629 | * Compares two text strings and returns TRUE if they are exactly the same, FALSE otherwise. |
||
630 | * EXACT is case-sensitive but ignores formatting differences. |
||
631 | * Use EXACT to test text being entered into a document. |
||
632 | * |
||
633 | * @param $value1 |
||
634 | * @param $value2 |
||
635 | * |
||
636 | * @return bool |
||
637 | */ |
||
638 | 7 | public static function EXACT($value1, $value2) |
|
639 | { |
||
640 | 7 | $value1 = Functions::flattenSingleValue($value1); |
|
641 | 7 | $value2 = Functions::flattenSingleValue($value2); |
|
642 | |||
643 | 7 | return (string) $value2 === (string) $value1; |
|
644 | } |
||
645 | |||
646 | /** |
||
647 | * TEXTJOIN. |
||
648 | * |
||
649 | * @param mixed $delimiter |
||
650 | * @param mixed $ignoreEmpty |
||
651 | * @param mixed $args |
||
652 | * |
||
653 | * @return string |
||
654 | */ |
||
655 | 6 | public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args) |
|
668 | } |
||
669 | } |
||
670 |