Total Complexity | 45 |
Total Lines | 227 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CurrencyBase 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 CurrencyBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CurrencyBase extends Number |
||
9 | { |
||
10 | public const LEADING_SYMBOL = true; |
||
11 | |||
12 | public const TRAILING_SYMBOL = false; |
||
13 | |||
14 | public const SYMBOL_WITH_SPACING = true; |
||
15 | |||
16 | public const SYMBOL_WITHOUT_SPACING = false; |
||
17 | |||
18 | protected string $currencyCode = '$'; |
||
19 | |||
20 | protected bool $currencySymbolPosition = self::LEADING_SYMBOL; |
||
21 | |||
22 | protected bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING; |
||
23 | |||
24 | protected const DEFAULT_STRIP_LEADING_RLM = false; |
||
25 | |||
26 | protected bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM; |
||
27 | |||
28 | public const DEFAULT_NEGATIVE = CurrencyNegative::minus; |
||
29 | |||
30 | protected CurrencyNegative $negative = CurrencyNegative::minus; |
||
31 | |||
32 | protected ?bool $overrideSpacing = null; |
||
33 | |||
34 | protected ?CurrencyNegative $overrideNegative = null; |
||
35 | |||
36 | // Not sure why original code uses nbsp |
||
37 | private string $spaceOrNbsp = ' '; // or "\u{a0}" |
||
38 | |||
39 | /** |
||
40 | * @param string $currencyCode the currency symbol or code to display for this mask |
||
41 | * @param int $decimals number of decimal places to display, in the range 0-30 |
||
42 | * @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not |
||
43 | * @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value |
||
44 | * Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL |
||
45 | * @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value |
||
46 | * Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING |
||
47 | * However, Currency always uses WITHOUT and Accounting always uses WITH |
||
48 | * @param ?string $locale Set the locale for the currency format; or leave as the default null. |
||
49 | * If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF). |
||
50 | * Note that setting a locale will override any other settings defined in this class |
||
51 | * other than the currency code; or decimals (unless the decimals value is set to 0). |
||
52 | * @param bool $stripLeadingRLM remove leading RLM added with |
||
53 | * ICU 72.1+. |
||
54 | * @param CurrencyNegative $negative How to display negative numbers. |
||
55 | * Always use parentheses for Accounting. |
||
56 | * 4 options for Currency. |
||
57 | * |
||
58 | * @throws Exception If a provided locale code is not a valid format |
||
59 | */ |
||
60 | public function __construct( |
||
78 | } |
||
79 | |||
80 | public function setCurrencyCode(string $currencyCode): void |
||
81 | { |
||
82 | $this->currencyCode = $currencyCode; |
||
83 | } |
||
84 | |||
85 | public function setCurrencySymbolPosition(bool $currencySymbolPosition = self::LEADING_SYMBOL): void |
||
88 | } |
||
89 | |||
90 | public function setCurrencySymbolSpacing(bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING): void |
||
93 | } |
||
94 | |||
95 | public function setStripLeadingRLM(bool $stripLeadingRLM): void |
||
96 | { |
||
97 | $this->stripLeadingRLM = $stripLeadingRLM; |
||
98 | } |
||
99 | |||
100 | public function setNegative(CurrencyNegative $negative): void |
||
101 | { |
||
102 | $this->negative = $negative; |
||
103 | } |
||
104 | |||
105 | protected function getLocaleFormat(): string |
||
106 | { |
||
107 | $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY); |
||
108 | $mask = $formatter->format($this->stripLeadingRLM); |
||
109 | if ($this->decimals === 0) { |
||
110 | $mask = (string) preg_replace('/\.0+/miu', '', $mask); |
||
111 | } |
||
112 | |||
113 | return str_replace('¤', $this->formatCurrencyCode(), $mask); |
||
114 | } |
||
115 | |||
116 | private function formatCurrencyCode(): string |
||
123 | } |
||
124 | |||
125 | public function format(): string |
||
235 | } |
||
236 | } |
||
237 |