MoneyFormatter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 26.83 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 6
dl 22
loc 82
ccs 22
cts 24
cp 0.9167
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A formatI18nDecimal() 11 11 2
A formatI18n() 11 11 2
A formatCurrencyAsSymbol() 0 4 1
A formatCurrencyAsName() 0 4 1
A __construct() 0 7 2

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 Ivoba\Money\Formatter;
4
5
use Money\Currencies\ISOCurrencies;
6
use Money\Formatter\IntlMoneyFormatter;
7
use Money\Money;
8
use Symfony\Component\Intl\Intl;
9
10
class MoneyFormatter
11
{
12
    /**
13
     * @var
14
     */
15
    private $locale;
16
17
    /**
18
     * @param null|string $locale
19
     */
20 18
    public function __construct($locale = null)
21
    {
22 18
        $this->locale = $locale;
23 18
        if ($this->locale === null) {
24 6
            $this->locale = \Locale::getDefault();
25 2
        }
26 18
    }
27
28
    /**
29
     * localized format for money with the NumberFormatter class in Decimal mode
30
     * without currency symbol
31
     * like 1.234,56
32
     *
33
     * @param Money $money
34
     * @param null  $locale
35
     * @return bool|string
36
     */
37 3 View Code Duplication
    public function formatI18nDecimal(Money $money, $locale = null)
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...
38
    {
39 3
        if ($locale === null) {
40
            $locale = $this->locale;
41
        }
42
43 3
        $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
44 3
        $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
45
46 3
        return $moneyFormatter->format($money);
47
    }
48
49
    /**
50
     * localized format for money with the NumberFormatter class in Currency mode
51
     * with currency symbol
52
     * like €1.234,56
53
     *
54
     * @param Money $money
55
     * @param null  $locale
56
     * @return bool|string
57
     */
58 9 View Code Duplication
    public function formatI18n(Money $money, $locale = null)
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...
59
    {
60 9
        if ($locale === null) {
61 3
            $locale = $this->locale;
62 1
        }
63
64 9
        $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
65 9
        $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
66
67 9
        return $moneyFormatter->format($money);
68
    }
69
70
    /**
71
     * gets currency symbol
72
     *
73
     * @param Money $money
74
     * @return null|string
75
     */
76 6
    public function formatCurrencyAsSymbol(Money $money)
77
    {
78 6
        return Intl::getCurrencyBundle()->getCurrencySymbol($money->getCurrency()->getCode());
79
    }
80
81
    /**
82
     * gets currency name
83
     *
84
     * @param Money $money
85
     * @return null|string
86
     */
87 6
    public function formatCurrencyAsName(Money $money)
88
    {
89 6
        return Intl::getCurrencyBundle()->getCurrencyName($money->getCurrency()->getCode());
90
    }
91
}
92