Money   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 154
Duplicated Lines 23.38 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 36
loc 154
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getCurrency() 0 4 1
A getAmount() 0 4 1
A getFormatted() 0 13 1
A add() 18 18 2
A subtract() 18 18 2
A hasSameCurrencyAs() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace MoneyMan;
4
5
use MoneyMan\Exception\AmountIsNotAnIntegerException;
6
use MoneyMan\Exception\CannotAddDifferentCurrenciesException;
7
use MoneyMan\Exception\CannotSubtractDifferentCurrenciesException;
8
9
/**
10
 * The Money Class
11
 *
12
 * This class represents a money entity which consists
13
 * of an amount and a currency code.
14
 *
15
 * @author Graham A. Sutton <[email protected]>
16
 */
17
class Money
18
{
19
    /**
20
     * The amount of the monetary unit.
21
     * @var int
22
     */
23
    private $amount;
24
25
    /**
26
     * The currency of the monetary unit.
27
     * @var \MoneyMan\Currency
28
     */
29
    private $currency;
30
31
    /**
32
     * Constructor
33
     *
34
     * Sets the immutable amount and currency.
35
     *
36
     * @param  int                 $amount
37
     * @param  \MoneyMan\Currency  $currency
38
     */
39 32
    public function __construct($amount, Currency $currency)
40
    {
41 32
        if (!is_int($amount)) {
42 1
            throw new AmountIsNotAnIntegerException(
43 1
                'Cannot set a non-integer value as an amount on a \MoneyMan\Money object.'
44
            );
45
        }
46
47 31
        $this->amount   = $amount;
48 31
        $this->currency = $currency;
49 31
    }
50
51
    /**
52
     * Get the currency of the monetary unit.
53
     *
54
     * @return \MoneyMan\Currency
55
     */
56 30
    public function getCurrency()
57
    {
58 30
        return $this->currency;
59
    }
60
61
    /**
62
     * Get the amount of the monetary unit.
63
     *
64
     * @return int
65
     */
66 26
    public function getAmount()
67
    {
68 26
        return $this->amount;
69
    }
70
71
    /**
72
     * Get the amount and currency code in a human
73
     * readable format.
74
     *
75
     * e.g. (new \MoneyMan\Money(800, new \MoneyMan\Currency('USD')))->getFormatted(); // => "$8.00"
76
     *
77
     * @param string $locale  The locale to format the output to.
78
     *
79
     * @return string
80
     */
81 8
    public function getFormatted($locale = 'en_US')
82
    {
83 8
        $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
84
85
        // Divide by 100 since formatCurrency accepts a float as first argument
86 8
        $money_string = $formatter->formatCurrency(
87 8
            $this->getAmount() / 100,
88 8
            $this->getCurrency()->getCode()
89
        );
90
91
        // Replace invisible characters with a space.
92 8
        return str_replace("\xC2\xA0", " ", $money_string);
93
    }
94
95
    /**
96
     * Adds two \MoneyMan\Money objects together by combining the amounts and
97
     * returning a new \MoneyMan\Money object.
98
     *
99
     * This method can only add two money objects of the same \MoneyMan\Currency
100
     * type.
101
     *
102
     * @param \MoneyMan\Money $money
103
     *
104
     * @return \MoneyMan\Money
105
     *
106
     * @throws \MoneyMan\Exception\CannotAddDifferentCurrenciesException
107
     */
108 7 View Code Duplication
    public function add(Money $money)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        // Validate they are of the same currency.
111 7
        if (!$this->hasSameCurrencyAs($money)) {
112 1
            throw new CannotAddDifferentCurrenciesException(
113
                'To directly add two money objects together, they must be of the same currency.' .
114
                'Use \MoneyMan\Exchange::add(\MoneyMan\Money, \MoneyMan\Money) to add \MoneyMan\Money ' .
115 1
                'objects of different currencies.'
116
            );
117
        }
118
119 6
        $total_amount = $this->getAmount() + $money->getAmount();
120
121 6
        return new self(
122
            $total_amount,
123 6
            $this->getCurrency()
124
        );
125
    }
126
127
    /**
128
     * Subtracts the incoming \MoneyMan\Money object's amount from this object.
129
     *
130
     * This method can only add two money objects of the same \MoneyMan\Currency
131
     * type.
132
     *
133
     * @param \MoneyMan\Money $money
134
     *
135
     * @return \MoneyMan\Money
136
     *
137
     * @throws \MoneyMan\Exception\CannotSubtractDifferentCurrenciesException
138
     */
139 7 View Code Duplication
    public function subtract(Money $money)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        // Validate they are of the same currency.
142 7
        if (!$this->hasSameCurrencyAs($money)) {
143 1
            throw new CannotSubtractDifferentCurrenciesException(
144
                'To directly subtract one money object from another, they must be of the same currency.' .
145
                'Use \MoneyMan\Exchange::subtract(\MoneyMan\Money, \MoneyMan\Money) to subtract \MoneyMan\Money ' .
146 1
                'objects of different currencies.'
147
            );
148
        }
149
150 6
        $total_amount = $this->getAmount() - $money->getAmount();
151
152 6
        return new self(
153
            $total_amount,
154 6
            $this->getCurrency()
155
        );
156
    }
157
158
    /**
159
     * Determine if this \MoneyMan\Money object has the same currency as another
160
     * \MoneyMan\Money object.
161
     *
162
     * @param \MoneyMan\Money $money The money object to compare against.
163
     *
164
     * @return bool
165
     */
166 16
    public function hasSameCurrencyAs(Money $money)
167
    {
168 16
        return $this->getCurrency()->equals($money->getCurrency());
169
    }
170
}
171