Complex classes like Money 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Money, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Keios\MoneyRight; |
||
28 | class Money implements Serializable, JsonSerializable |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @const |
||
33 | */ |
||
34 | const GAAP_PRECISION = 4; |
||
35 | |||
36 | /** |
||
37 | * @const |
||
38 | */ |
||
39 | const BASIC_PRECISION = 2; |
||
40 | |||
41 | /** |
||
42 | * @const |
||
43 | */ |
||
44 | const ROUND_HALF_UP = PHP_ROUND_HALF_UP; |
||
45 | |||
46 | /** |
||
47 | * @const |
||
48 | */ |
||
49 | const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN; |
||
50 | |||
51 | /** |
||
52 | * @const |
||
53 | */ |
||
54 | const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN; |
||
55 | |||
56 | /** |
||
57 | * @const |
||
58 | */ |
||
59 | const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $amount; |
||
65 | |||
66 | /** |
||
67 | * @var \Keios\MoneyRight\Currency |
||
68 | */ |
||
69 | private $currency; |
||
70 | |||
71 | /** |
||
72 | * Create a new Money Instance |
||
73 | * |
||
74 | * @param string $amount |
||
75 | * @param \Keios\MoneyRight\Currency $currency |
||
76 | */ |
||
77 | 183 | public function __construct($amount, Currency $currency) |
|
78 | { |
||
79 | 183 | if (is_int($amount)) { |
|
80 | 60 | $amount = $amount / 100; |
|
81 | 60 | } |
|
82 | |||
83 | 183 | $stringAmount = $this->castValueToString($amount); |
|
84 | |||
85 | 183 | $this->currency = $currency; |
|
86 | 183 | $this->bootWith($stringAmount); |
|
87 | 180 | } |
|
88 | |||
89 | /** |
||
90 | * Convenience factory method for an Keios\MoneyRight\Money object |
||
91 | * |
||
92 | * @example $fiveDollar = Money::USD(500); |
||
93 | * |
||
94 | * @param string $method |
||
95 | * @param array $arguments |
||
96 | * |
||
97 | * @return \Keios\MoneyRight\Money |
||
98 | */ |
||
99 | 33 | public static function __callStatic($method, $arguments) |
|
103 | |||
104 | // SERIALIZATION |
||
105 | |||
106 | /** |
||
107 | * Serialization of Money value object |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 3 | public function serialize() |
|
112 | { |
||
113 | 3 | return serialize( |
|
114 | [ |
||
115 | 3 | 'amount' => $this->amount, |
|
116 | 3 | 'currency' => serialize($this->currency), |
|
117 | ] |
||
118 | 3 | ); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Unserializing Money value object |
||
123 | * |
||
124 | * @param string $serialized |
||
125 | */ |
||
126 | 3 | public function unserialize($serialized) |
|
127 | { |
||
128 | 3 | $unserialized = unserialize($serialized); |
|
129 | |||
130 | 3 | $this->amount = $unserialized['amount']; |
|
131 | 3 | $this->currency = unserialize($unserialized['currency']); |
|
132 | 3 | } |
|
133 | |||
134 | /** |
||
135 | * @return string |
||
136 | */ |
||
137 | public function jsonserialize() |
||
138 | { |
||
139 | return [ |
||
140 | 'amount' => $this->amount, |
||
141 | 'currency' => $this->currency->jsonserialize(), |
||
142 | ]; |
||
143 | } |
||
144 | |||
145 | // GETTERS |
||
146 | |||
147 | /** |
||
148 | * Return full GAAP precision amount string |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 78 | public function getAmountString() |
|
156 | |||
157 | /** |
||
158 | * Compatibility with Verraes' Money |
||
159 | * |
||
160 | * @return integer |
||
161 | */ |
||
162 | 21 | public function getAmount() |
|
166 | |||
167 | /** |
||
168 | * Useful for payment systems that don't use high precision |
||
169 | * |
||
170 | * @param int $roundingMode |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getAmountBasic($roundingMode = self::ROUND_HALF_UP) |
||
178 | |||
179 | /** |
||
180 | * @return \Keios\MoneyRight\Currency |
||
181 | */ |
||
182 | 93 | public function getCurrency() |
|
186 | |||
187 | // LOGIC |
||
188 | |||
189 | /** |
||
190 | * @param \Keios\MoneyRight\Money |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | 6 | public function greaterThan(Money $other) |
|
198 | |||
199 | /** |
||
200 | * @param \Keios\MoneyRight\Money $other |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function greaterThanOrEqual(Money $other) |
||
208 | |||
209 | /** |
||
210 | * @param \Keios\MoneyRight\Money $other |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | 6 | public function lessThan(Money $other) |
|
218 | |||
219 | /** |
||
220 | * @param \Keios\MoneyRight\Money $other |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | public function lessThanOrEqual(Money $other) |
||
228 | |||
229 | /** |
||
230 | * Compatibility with Verraes' Money |
||
231 | * |
||
232 | * @deprecated Use getAmount() instead |
||
233 | * @return int |
||
234 | */ |
||
235 | public function getUnits() |
||
239 | |||
240 | /** |
||
241 | * @param \Keios\MoneyRight\Money $other |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 78 | public function isSameCurrency(Money $other) |
|
249 | |||
250 | /** |
||
251 | * @param \Keios\MoneyRight\Money $other |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 33 | public function equals(Money $other) |
|
259 | |||
260 | /** |
||
261 | * @param \Keios\MoneyRight\Money $other |
||
262 | * |
||
263 | * @return int |
||
264 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
265 | */ |
||
266 | 15 | public function compare(Money $other) |
|
267 | { |
||
268 | 15 | $this->assertSameCurrency($other); |
|
269 | |||
270 | 9 | return bccomp($this->amount, $other->getAmountString(), self::GAAP_PRECISION); |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param \Keios\MoneyRight\Money $addend |
||
275 | * |
||
276 | * @return \Keios\MoneyRight\Money |
||
277 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
278 | */ |
||
279 | 24 | public function add(Money $addend) |
|
280 | { |
||
281 | 24 | $this->assertSameCurrency($addend); |
|
282 | |||
283 | 18 | return new Money(bcadd($this->amount, $addend->getAmountString(), self::GAAP_PRECISION), $this->currency); |
|
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param \Keios\MoneyRight\Money $subtrahend |
||
288 | * |
||
289 | * @return \Keios\MoneyRight\Money |
||
290 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
291 | */ |
||
292 | 12 | public function subtract(Money $subtrahend) |
|
293 | { |
||
294 | 12 | $this->assertSameCurrency($subtrahend); |
|
295 | |||
296 | 6 | return new Money(bcsub($this->amount, $subtrahend->getAmountString(), self::GAAP_PRECISION), $this->currency); |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * Multiplying compatible with Verraes' Money |
||
301 | * To use GAAP precision rounding, pass true as third argument |
||
302 | * |
||
303 | * @param $operand |
||
304 | * @param int $roundingMode |
||
305 | * @param bool $useGaapPrecision |
||
306 | * |
||
307 | * @return \Keios\MoneyRight\Money |
||
308 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
309 | */ |
||
310 | 6 | public function multiply($operand, $roundingMode = self::ROUND_HALF_UP, $useGaapPrecision = false) |
|
332 | |||
333 | /** |
||
334 | * Division compatible with Verraes' Money |
||
335 | * To use GAAP precision rounding, pass true as third argument |
||
336 | * |
||
337 | * @param $operand |
||
338 | * @param int $roundingMode |
||
339 | * @param bool $useGaapPrecision |
||
340 | * |
||
341 | * @return \Keios\MoneyRight\Money |
||
342 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
343 | */ |
||
344 | 9 | public function divide($operand, $roundingMode = self::ROUND_HALF_UP, $useGaapPrecision = false) |
|
366 | |||
367 | /** |
||
368 | * Allocate the money according to a list of ratio's |
||
369 | * Allocation is compatible with Verraes' Money |
||
370 | * To use GAAP precision rounding, pass true as second argument |
||
371 | * |
||
372 | * @param array $ratios List of ratio's |
||
373 | * @param bool $useGaapPrecision |
||
374 | * |
||
375 | * @return array |
||
376 | */ |
||
377 | 9 | public function allocate(array $ratios, $useGaapPrecision = false) |
|
415 | |||
416 | /** |
||
417 | * @return \Keios\MoneyRight\Money |
||
418 | */ |
||
419 | 3 | public function abs() |
|
420 | { |
||
421 | 3 | $amount = $this->isNegative() ? substr($this->amount, 1): $this->amount; |
|
422 | |||
423 | 3 | return new Money($amount, $this->currency); |
|
424 | } |
||
425 | |||
426 | /** |
||
427 | * @return bool |
||
428 | */ |
||
429 | 6 | public function isZero() |
|
430 | { |
||
431 | 6 | return bccomp('0', $this->amount, self::GAAP_PRECISION) === 0; |
|
432 | } |
||
433 | |||
434 | /** |
||
435 | * @return bool |
||
436 | */ |
||
437 | 6 | public function isPositive() |
|
438 | { |
||
439 | 6 | return bccomp($this->amount, '0', self::GAAP_PRECISION) === 1; |
|
440 | } |
||
441 | |||
442 | /** |
||
443 | * @return bool |
||
444 | */ |
||
445 | 9 | public function isNegative() |
|
446 | { |
||
447 | 9 | return bccomp($this->amount, '0', self::GAAP_PRECISION) === -1; |
|
448 | } |
||
449 | |||
450 | // INTERNALS |
||
451 | |||
452 | /** |
||
453 | * @param mixed $value |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 183 | private function castValueToString($value) |
|
458 | { |
||
459 | 183 | return (string)$value; |
|
460 | } |
||
461 | |||
462 | /** |
||
463 | * @param $stringAmount |
||
464 | * |
||
465 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
466 | */ |
||
467 | 183 | private function bootWith($stringAmount) |
|
468 | { |
||
469 | 183 | $this->assertValidAmountString($stringAmount); |
|
470 | 180 | $this->amount = $this->normalizeAmountFromString($stringAmount); |
|
471 | 180 | } |
|
472 | |||
473 | /** |
||
474 | * @param $string |
||
475 | * |
||
476 | * @return string |
||
477 | */ |
||
478 | 183 | private function normalizeString($string) |
|
482 | |||
483 | /** |
||
484 | * @param $operand |
||
485 | * |
||
486 | * @return mixed |
||
487 | */ |
||
488 | 15 | private function normalizeOperand($operand) |
|
492 | |||
493 | /** |
||
494 | * @param $stringAmount |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | 180 | private function normalizeAmountFromString($stringAmount) |
|
504 | |||
505 | /** |
||
506 | * @param \Keios\MoneyRight\Money $other |
||
507 | * |
||
508 | * @return bool |
||
509 | */ |
||
510 | 30 | private function isSameAmount(Money $other) |
|
514 | |||
515 | // ASSERTIONS |
||
516 | |||
517 | /** |
||
518 | * @param $stringAmount |
||
519 | * |
||
520 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
521 | */ |
||
522 | 183 | private function assertValidAmountString($stringAmount) |
|
528 | |||
529 | /** |
||
530 | * @param $stringOperand |
||
531 | * |
||
532 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
533 | */ |
||
534 | 15 | private function assertValidOperandString($stringOperand) |
|
540 | |||
541 | /** |
||
542 | * @param \Keios\MoneyRight\Money $other |
||
543 | * |
||
544 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
545 | */ |
||
546 | 51 | private function assertSameCurrency(Money $other) |
|
558 | |||
559 | /** |
||
560 | * @param int|float $operand |
||
561 | * @param bool $isDivision |
||
562 | * |
||
563 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
564 | */ |
||
565 | 15 | private function assertOperand($operand, $isDivision = false) |
|
583 | |||
584 | /** |
||
585 | * @param $operand |
||
586 | * |
||
587 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
588 | */ |
||
589 | 9 | private function assertOperandNotZero($operand) |
|
596 | |||
597 | // STATIC METHODS |
||
598 | |||
599 | /** |
||
600 | * @param $string |
||
601 | * |
||
602 | * @throws \Keios\MoneyRight\Exceptions\InvalidArgumentException |
||
603 | * @return int |
||
604 | */ |
||
605 | 51 | public static function stringToUnits($string) |
|
611 | } |
||
612 |