Completed
Push — master ( 48514f...802925 )
by Ivo
02:35
created

MoneyFormatter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 31.82 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 4
dl 28
loc 88
ccs 27
cts 29
cp 0.931
rs 10

5 Methods

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