Total Complexity | 45 |
Total Lines | 251 |
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 NEGATIVE_MINUS = '-'; |
||
29 | public const NEGATIVE_RED_MINUS = 'red-'; |
||
30 | public const NEGATIVE_PARENS = '()'; |
||
31 | public const NEGATIVE_RED_PARENS = 'red()'; |
||
32 | |||
33 | protected const NEGATIVE_START = [ |
||
34 | self::NEGATIVE_MINUS => '-', |
||
35 | self::NEGATIVE_RED_MINUS => '-', |
||
36 | self::NEGATIVE_PARENS => '\\(', |
||
37 | self::NEGATIVE_RED_PARENS => '\\(', |
||
38 | ]; |
||
39 | |||
40 | protected const NEGATIVE_END = [ |
||
41 | self::NEGATIVE_MINUS => '', |
||
42 | self::NEGATIVE_RED_MINUS => '', |
||
43 | self::NEGATIVE_PARENS => '\\)', |
||
44 | self::NEGATIVE_RED_PARENS => '\\)', |
||
45 | ]; |
||
46 | |||
47 | protected const NEGATIVE_COLOR = [ |
||
48 | self::NEGATIVE_RED_MINUS => '[Red]', |
||
49 | self::NEGATIVE_RED_PARENS => '[Red]', |
||
50 | ]; |
||
51 | |||
52 | public const DEFAULT_NEGATIVE = self::NEGATIVE_MINUS; |
||
53 | |||
54 | protected string $negative = self::NEGATIVE_MINUS; |
||
55 | |||
56 | protected ?bool $overrideSpacing = null; |
||
57 | |||
58 | protected ?string $overrideNegative = null; |
||
59 | |||
60 | // Not sure why original code uses nbsp |
||
61 | private string $spaceOrNbsp = ' '; // or "\u{a0}" |
||
62 | |||
63 | /** |
||
64 | * @param string $currencyCode the currency symbol or code to display for this mask |
||
65 | * @param int $decimals number of decimal places to display, in the range 0-30 |
||
66 | * @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not |
||
67 | * @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value |
||
68 | * Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL |
||
69 | * @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value |
||
70 | * Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING |
||
71 | * However, Currency always uses WITHOUT and Accounting always uses WITH |
||
72 | * @param ?string $locale Set the locale for the currency format; or leave as the default null. |
||
73 | * If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF). |
||
74 | * Note that setting a locale will override any other settings defined in this class |
||
75 | * other than the currency code; or decimals (unless the decimals value is set to 0). |
||
76 | * @param bool $stripLeadingRLM remove leading RLM added with |
||
77 | * ICU 72.1+. |
||
78 | * @param string $negative How to display negative numbers. |
||
79 | * Always use parentheses for Accounting. |
||
80 | * 4 options for Currency. |
||
81 | * |
||
82 | * @throws Exception If a provided locale code is not a valid format |
||
83 | */ |
||
84 | public function __construct( |
||
102 | } |
||
103 | |||
104 | public function setCurrencyCode(string $currencyCode): void |
||
105 | { |
||
106 | $this->currencyCode = $currencyCode; |
||
107 | } |
||
108 | |||
109 | public function setCurrencySymbolPosition(bool $currencySymbolPosition = self::LEADING_SYMBOL): void |
||
112 | } |
||
113 | |||
114 | public function setCurrencySymbolSpacing(bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING): void |
||
117 | } |
||
118 | |||
119 | public function setStripLeadingRLM(bool $stripLeadingRLM): void |
||
120 | { |
||
121 | $this->stripLeadingRLM = $stripLeadingRLM; |
||
122 | } |
||
123 | |||
124 | public function setNegative(string $negative): void |
||
125 | { |
||
126 | $this->negative = $negative; |
||
127 | } |
||
128 | |||
129 | protected function getLocaleFormat(): string |
||
130 | { |
||
131 | $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY); |
||
132 | $mask = $formatter->format($this->stripLeadingRLM); |
||
133 | if ($this->decimals === 0) { |
||
134 | $mask = (string) preg_replace('/\.0+/miu', '', $mask); |
||
135 | } |
||
136 | |||
137 | return str_replace('¤', $this->formatCurrencyCode(), $mask); |
||
138 | } |
||
139 | |||
140 | private function formatCurrencyCode(): string |
||
147 | } |
||
148 | |||
149 | public function format(): string |
||
259 | } |
||
260 | } |
||
261 |