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