Total Complexity | 64 |
Total Lines | 600 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NumberFormatInfo 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 NumberFormatInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class NumberFormatInfo |
||
51 | { |
||
52 | |||
53 | /** |
||
54 | * ICU number formatting data. |
||
55 | * @var array |
||
56 | */ |
||
57 | private $data = []; |
||
58 | |||
59 | /** |
||
60 | * A list of properties that are accessable/writable. |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $properties = []; |
||
64 | |||
65 | /** |
||
66 | * The number pattern. |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $pattern = []; |
||
70 | |||
71 | const DECIMAL = 0; |
||
72 | const CURRENCY = 1; |
||
73 | const PERCENTAGE = 2; |
||
74 | const SCIENTIFIC = 3; |
||
75 | |||
76 | /** |
||
77 | * Allow functions that begins with 'set' to be called directly |
||
78 | * as an attribute/property to retrieve the value. |
||
79 | * @return mixed |
||
80 | */ |
||
81 | public function __get($name) |
||
82 | { |
||
83 | $getProperty = 'get' . $name; |
||
84 | if(in_array($getProperty, $this->properties)) |
||
85 | return $this->$getProperty(); |
||
86 | else |
||
87 | throw new Exception('Property ' . $name . ' does not exists.'); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Allow functions that begins with 'set' to be called directly |
||
92 | * as an attribute/property to set the value. |
||
93 | */ |
||
94 | public function __set($name, $value) |
||
95 | { |
||
96 | $setProperty = 'set' . $name; |
||
97 | if(in_array($setProperty, $this->properties)) |
||
98 | $this->$setProperty($value); |
||
99 | else |
||
100 | throw new Exception('Property ' . $name . ' can not be set.'); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Initializes a new writable instance of the NumberFormatInfo class |
||
105 | * that is dependent on the ICU data for number, decimal, and currency |
||
|
|||
106 | * formatting information. <b>N.B.</b>You should not initialize this |
||
107 | * class directly unless you know what you are doing. Please use use |
||
108 | * NumberFormatInfo::getInstance() to create an instance. |
||
109 | * @param array ICU data for date time formatting. |
||
110 | * @see getInstance() |
||
111 | */ |
||
112 | public function __construct($data = [], $type = NumberFormatInfo::DECIMAL) |
||
113 | { |
||
114 | $this->properties = get_class_methods($this); |
||
115 | |||
116 | if(empty($data)) |
||
117 | throw new Exception('Please provide the ICU data to initialize.'); |
||
118 | |||
119 | $this->data = $data; |
||
120 | |||
121 | $this->setPattern($type); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Set the pattern for a specific number pattern. The validate patterns |
||
126 | * NumberFormatInfo::DECIMAL, NumberFormatInfo::CURRENCY, |
||
127 | * NumberFormatInfo::PERCENTAGE, or NumberFormatInfo::SCIENTIFIC |
||
128 | * @param int pattern type. |
||
129 | */ |
||
130 | public function setPattern($type = NumberFormatInfo::DECIMAL) |
||
131 | { |
||
132 | if(is_int($type)) |
||
133 | $this->pattern = |
||
134 | $this->parsePattern($this->data['NumberPatterns'][$type]); |
||
135 | else |
||
136 | $this->pattern = $this->parsePattern($type); |
||
137 | |||
138 | $this->pattern['negInfty'] = |
||
139 | $this->data['NumberElements'][6] . |
||
140 | $this->data['NumberElements'][9]; |
||
141 | |||
142 | $this->pattern['posInfty'] = |
||
143 | $this->data['NumberElements'][11] . |
||
144 | $this->data['NumberElements'][9]; |
||
145 | } |
||
146 | |||
147 | public function getPattern() |
||
148 | { |
||
149 | return $this->pattern; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Gets the default NumberFormatInfo that is culture-independent |
||
154 | * (invariant). |
||
155 | * @return NumberFormatInfo default NumberFormatInfo. |
||
156 | */ |
||
157 | public static function getInvariantInfo($type = NumberFormatInfo::DECIMAL) |
||
158 | { |
||
159 | static $invariant; |
||
160 | if($invariant === null) |
||
161 | { |
||
162 | $culture = CultureInfo::getInvariantCulture(); |
||
163 | $invariant = $culture->NumberFormat; |
||
164 | $invariant->setPattern($type); |
||
165 | } |
||
166 | return $invariant; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Returns the NumberFormatInfo associated with the specified culture. |
||
171 | * @param CultureInfo the culture that gets the NumberFormat property. |
||
172 | * @param int the number formatting type, it should be |
||
173 | * NumberFormatInfo::DECIMAL, NumberFormatInfo::CURRENCY, |
||
174 | * NumberFormatInfo::PERCENTAGE, or NumberFormatInfo::SCIENTIFIC |
||
175 | * @return NumberFormatInfo NumberFormatInfo for the specified |
||
176 | * culture. |
||
177 | * @see getCurrencyInstance(); |
||
178 | * @see getPercentageInstance(); |
||
179 | * @see getScientificInstance(); |
||
180 | */ |
||
181 | public static function getInstance($culture = null, |
||
182 | $type = NumberFormatInfo::DECIMAL) |
||
183 | { |
||
184 | if ($culture instanceof CultureInfo) |
||
185 | { |
||
186 | $formatInfo = $culture->NumberFormat; |
||
187 | $formatInfo->setPattern($type); |
||
188 | return $formatInfo; |
||
189 | } |
||
190 | elseif(is_string($culture)) |
||
191 | { |
||
192 | $cultureInfo = new CultureInfo($culture); |
||
193 | $formatInfo = $cultureInfo->NumberFormat; |
||
194 | $formatInfo->setPattern($type); |
||
195 | return $formatInfo; |
||
196 | } |
||
197 | else |
||
198 | { |
||
199 | $cultureInfo = new CultureInfo(); |
||
200 | $formatInfo = $cultureInfo->NumberFormat; |
||
201 | $formatInfo->setPattern($type); |
||
202 | return $formatInfo; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Returns the currency format info associated with the specified culture. |
||
208 | * @param CultureInfo the culture that gets the NumberFormat property. |
||
209 | * @return NumberFormatInfo NumberFormatInfo for the specified |
||
210 | * culture. |
||
211 | */ |
||
212 | public static function getCurrencyInstance($culture = null) |
||
213 | { |
||
214 | return self::getInstance($culture, self::CURRENCY); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns the percentage format info associated with the specified culture. |
||
219 | * @param CultureInfo the culture that gets the NumberFormat property. |
||
220 | * @return NumberFormatInfo NumberFormatInfo for the specified |
||
221 | * culture. |
||
222 | */ |
||
223 | public static function getPercentageInstance($culture = null) |
||
224 | { |
||
225 | return self::getInstance($culture, self::PERCENTAGE); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Returns the scientific format info associated with the specified culture. |
||
230 | * @param CultureInfo the culture that gets the NumberFormat property. |
||
231 | * @return NumberFormatInfo NumberFormatInfo for the specified |
||
232 | * culture. |
||
233 | */ |
||
234 | public static function getScientificInstance($culture = null) |
||
235 | { |
||
236 | return self::getInstance($culture, self::SCIENTIFIC); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Parse the given pattern and return a list of known properties. |
||
241 | * @param string a number pattern. |
||
242 | * @return array list of pattern properties. |
||
243 | */ |
||
244 | protected function parsePattern($pattern) |
||
245 | { |
||
246 | $pattern = explode(';', $pattern); |
||
247 | |||
248 | $negative = null; |
||
249 | if(count($pattern) > 1) |
||
250 | $negative = $pattern[1]; |
||
251 | $pattern = $pattern[0]; |
||
252 | |||
253 | $comma = ','; |
||
254 | $dot = '.'; |
||
255 | $digit = '0'; |
||
256 | $hash = '#'; |
||
257 | |||
258 | //find the first group point, and decimal point |
||
259 | $groupPos1 = strrpos($pattern, $comma); |
||
260 | $decimalPos = strrpos($pattern, $dot); |
||
261 | |||
262 | $groupPos2 = false; |
||
263 | $groupSize1 = false; |
||
264 | $groupSize2 = false; |
||
265 | $decimalPoints = is_int($decimalPos)?-1:false; |
||
266 | |||
267 | $info['negPref'] = $this->data['NumberElements'][6]; |
||
268 | $info['negPost'] = ''; |
||
269 | |||
270 | $info['negative'] = $negative; |
||
271 | $info['positive'] = $pattern; |
||
272 | |||
273 | //find the negative prefix and postfix |
||
274 | if($negative) |
||
275 | { |
||
276 | $prefixPostfix = $this->getPrePostfix($negative); |
||
277 | $info['negPref'] = $prefixPostfix[0]; |
||
278 | $info['negPost'] = $prefixPostfix[1]; |
||
279 | } |
||
280 | |||
281 | $posfix = $this->getPrePostfix($pattern); |
||
282 | $info['posPref'] = $posfix[0]; |
||
283 | $info['posPost'] = $posfix[1]; |
||
284 | |||
285 | //var_dump($pattern); |
||
286 | //var_dump($decimalPos); |
||
287 | if(is_int($groupPos1)) |
||
288 | { |
||
289 | //get the second group |
||
290 | $groupPos2 = strrpos(substr($pattern, 0, $groupPos1), $comma); |
||
291 | |||
292 | //get the number of decimal digits |
||
293 | if(is_int($decimalPos)) |
||
294 | { |
||
295 | $groupSize1 = $decimalPos - $groupPos1 - 1; |
||
296 | |||
297 | } |
||
298 | else |
||
299 | { |
||
300 | //no decimal point, so traverse from the back |
||
301 | //to find the groupsize 1. |
||
302 | for($i = strlen($pattern) - 1; $i >= 0; $i--) |
||
303 | { |
||
304 | if($pattern{$i} == $digit || $pattern{$i} == $hash) |
||
305 | { |
||
306 | $groupSize1 = $i - $groupPos1; |
||
307 | break; |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | //get the second group size |
||
313 | if(is_int($groupPos2)) |
||
314 | $groupSize2 = $groupPos1 - $groupPos2 - 1; |
||
315 | } |
||
316 | |||
317 | if(is_int($decimalPos)) |
||
318 | { |
||
319 | for($i = strlen($pattern) - 1; $i >= 0; $i--) |
||
320 | { |
||
321 | if($pattern{$i} == $dot) break; |
||
322 | if($pattern{$i} == $digit) |
||
323 | { |
||
324 | $decimalPoints = $i - $decimalPos; |
||
325 | break; |
||
326 | } |
||
327 | } |
||
328 | } |
||
329 | |||
330 | if(is_int($decimalPos)) |
||
331 | $digitPattern = substr($pattern, 0, $decimalPos); |
||
332 | else |
||
333 | $digitPattern = $pattern; |
||
334 | |||
335 | $digitPattern = preg_replace('/[^0]/', '', $digitPattern); |
||
336 | |||
337 | $info['groupPos1'] = $groupPos1; |
||
338 | $info['groupSize1'] = $groupSize1; |
||
339 | $info['groupPos2'] = $groupPos2; |
||
340 | $info['groupSize2'] = $groupSize2; |
||
341 | $info['decimalPos'] = $decimalPos; |
||
342 | $info['decimalPoints'] = $decimalPoints; |
||
343 | $info['digitSize'] = strlen($digitPattern); |
||
344 | return $info; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Get the prefix and postfix of a pattern. |
||
349 | * @param string pattern |
||
350 | * @return array of prefix and postfix, array(prefix,postfix). |
||
351 | */ |
||
352 | protected function getPrePostfix($pattern) |
||
353 | { |
||
354 | $regexp = '/[#,\.0]+/'; |
||
355 | $result = preg_split($regexp, $pattern); |
||
356 | return [$result[0],$result[1]]; |
||
357 | } |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Indicates the number of decimal places. |
||
362 | * @return int number of decimal places. |
||
363 | */ |
||
364 | public function getDecimalDigits() |
||
365 | { |
||
366 | return $this->pattern['decimalPoints']; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Set the number of decimal places. |
||
371 | * @param int number of decimal places. |
||
372 | */ |
||
373 | public function setDecimalDigits($value) |
||
374 | { |
||
375 | return $this->pattern['decimalPoints'] = $value; |
||
376 | } |
||
377 | |||
378 | public function getDigitSize() |
||
379 | { |
||
380 | return $this->pattern['digitSize']; |
||
381 | } |
||
382 | |||
383 | public function setDigitSize($value) |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Gets the string to use as the decimal separator. |
||
390 | * @return string decimal separator. |
||
391 | */ |
||
392 | public function getDecimalSeparator() |
||
393 | { |
||
394 | return $this->data['NumberElements'][0]; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Set the string to use as the decimal separator. |
||
399 | * @param string the decimal point |
||
400 | */ |
||
401 | public function setDecimalSeparator($value) |
||
402 | { |
||
403 | return $this->data['NumberElements'][0] = $value; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Gets the string that separates groups of digits to the left |
||
408 | * of the decimal in currency values. |
||
409 | * @param parameter |
||
410 | * @return string currency group separator. |
||
411 | */ |
||
412 | public function getGroupSeparator() |
||
413 | { |
||
414 | return $this->data['NumberElements'][1]; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Set the string to use as the group separator. |
||
419 | * @param string the group separator. |
||
420 | */ |
||
421 | public function setGroupSeparator($value) |
||
422 | { |
||
423 | return $this->data['NumberElements'][1] = $value; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Gets the number of digits in each group to the left of the decimal |
||
428 | * There can be two grouping sizes, this fucntion |
||
429 | * returns <b>array(group1, group2)</b>, if there is only 1 grouping size, |
||
430 | * group2 will be false. |
||
431 | * @return array grouping size(s). |
||
432 | */ |
||
433 | public function getGroupSizes() |
||
434 | { |
||
435 | $group1 = $this->pattern['groupSize1']; |
||
436 | $group2 = $this->pattern['groupSize2']; |
||
437 | |||
438 | return [$group1, $group2]; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Set the number of digits in each group to the left of the decimal. |
||
443 | * There can be two grouping sizes, the value should |
||
444 | * be an <b>array(group1, group2)</b>, if there is only 1 grouping size, |
||
445 | * group2 should be false. |
||
446 | * @param array grouping size(s). |
||
447 | */ |
||
448 | public function setGroupSizes($groupSize) |
||
449 | { |
||
450 | $this->pattern['groupSize1'] = $groupSize[0]; |
||
451 | $this->pattern['groupSize2'] = $groupSize[1]; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Gets the format pattern for negative values. |
||
456 | * The negative pattern is composed of a prefix, and postfix. |
||
457 | * This function returns <b>array(prefix, postfix)</b>. |
||
458 | * @return arary negative pattern. |
||
459 | */ |
||
460 | public function getNegativePattern() |
||
461 | { |
||
462 | $prefix = $this->pattern['negPref']; |
||
463 | $postfix = $this->pattern['negPost']; |
||
464 | return [$prefix, $postfix]; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Set the format pattern for negative values. |
||
469 | * The negative pattern is composed of a prefix, and postfix in the form |
||
470 | * <b>array(prefix, postfix)</b>. |
||
471 | * @param arary negative pattern. |
||
472 | */ |
||
473 | public function setNegativePattern($pattern) |
||
474 | { |
||
475 | $this->pattern['negPref'] = $pattern[0]; |
||
476 | $this->pattern['negPost'] = $pattern[1]; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Gets the format pattern for positive values. |
||
481 | * The positive pattern is composed of a prefix, and postfix. |
||
482 | * This function returns <b>array(prefix, postfix)</b>. |
||
483 | * @return arary positive pattern. |
||
484 | */ |
||
485 | public function getPositivePattern() |
||
486 | { |
||
487 | $prefix = $this->pattern['posPref']; |
||
488 | $postfix = $this->pattern['posPost']; |
||
489 | return [$prefix, $postfix]; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Set the format pattern for positive values. |
||
494 | * The positive pattern is composed of a prefix, and postfix in the form |
||
495 | * <b>array(prefix, postfix)</b>. |
||
496 | * @param arary positive pattern. |
||
497 | */ |
||
498 | public function setPositivePattern($pattern) |
||
499 | { |
||
500 | $this->pattern['posPref'] = $pattern[0]; |
||
501 | $this->pattern['posPost'] = $pattern[1]; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Gets the string to use as the currency symbol. |
||
506 | * @return string currency symbol. |
||
507 | */ |
||
508 | public function getCurrencySymbol($currency = 'USD') |
||
509 | { |
||
510 | if(isset($this->pattern['symbol'])) |
||
511 | return $this->pattern['symbol']; |
||
512 | else |
||
513 | return $this->data['Currencies'][$currency][0]; |
||
514 | } |
||
515 | |||
516 | |||
517 | /** |
||
518 | * Set the string to use as the currency symbol. |
||
519 | * @param string currency symbol. |
||
520 | */ |
||
521 | public function setCurrencySymbol($symbol) |
||
522 | { |
||
523 | $this->pattern['symbol'] = $symbol; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Gets the string that represents negative infinity. |
||
528 | * @return string negative infinity. |
||
529 | */ |
||
530 | public function getNegativeInfinitySymbol() |
||
531 | { |
||
532 | return $this->pattern['negInfty']; |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Set the string that represents negative infinity. |
||
537 | * @param string negative infinity. |
||
538 | */ |
||
539 | public function setNegativeInfinitySymbol($value) |
||
540 | { |
||
541 | $this->pattern['negInfty'] = $value; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Gets the string that represents positive infinity. |
||
546 | * @return string positive infinity. |
||
547 | */ |
||
548 | public function getPositiveInfinitySymbol() |
||
549 | { |
||
550 | return $this->pattern['posInfty']; |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Set the string that represents positive infinity. |
||
555 | * @param string positive infinity. |
||
556 | */ |
||
557 | public function setPositiveInfinitySymbol($value) |
||
558 | { |
||
559 | $this->pattern['posInfty'] = $value; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Gets the string that denotes that the associated number is negative. |
||
564 | * @return string negative sign. |
||
565 | */ |
||
566 | public function getNegativeSign() |
||
567 | { |
||
568 | return $this->data['NumberElements'][6]; |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Set the string that denotes that the associated number is negative. |
||
573 | * @param string negative sign. |
||
574 | */ |
||
575 | public function setNegativeSign($value) |
||
576 | { |
||
577 | $this->data['NumberElements'][6] = $value; |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Gets the string that denotes that the associated number is positive. |
||
582 | * @return string positive sign. |
||
583 | */ |
||
584 | public function getPositiveSign() |
||
585 | { |
||
586 | return $this->data['NumberElements'][11]; |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * Set the string that denotes that the associated number is positive. |
||
591 | * @param string positive sign. |
||
592 | */ |
||
593 | public function setPositiveSign($value) |
||
594 | { |
||
595 | $this->data['NumberElements'][11] = $value; |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Gets the string that represents the IEEE NaN (not a number) value. |
||
600 | * @return string NaN symbol. |
||
601 | */ |
||
602 | public function getNaNSymbol() |
||
603 | { |
||
604 | return $this->data['NumberElements'][10]; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * Set the string that represents the IEEE NaN (not a number) value. |
||
609 | * @param string NaN symbol. |
||
610 | */ |
||
611 | public function setNaNSymbol($value) |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Gets the string to use as the percent symbol. |
||
618 | * @return string percent symbol. |
||
619 | */ |
||
620 | public function getPercentSymbol() |
||
621 | { |
||
622 | return $this->data['NumberElements'][3]; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Set the string to use as the percent symbol. |
||
627 | * @param string percent symbol. |
||
628 | */ |
||
629 | public function setPercentSymbol($value) |
||
632 | } |
||
633 | |||
634 | /** |
||
635 | * Gets the string to use as the per mille symbol. |
||
636 | * @return string percent symbol. |
||
637 | */ |
||
638 | public function getPerMilleSymbol() |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * Set the string to use as the per mille symbol. |
||
645 | * @param string percent symbol. |
||
646 | */ |
||
647 | public function setPerMilleSymbol($value) |
||
648 | { |
||
650 | } |
||
651 | } |
||
652 | |||
653 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths