Completed
Pull Request — master (#7)
by hu
02:46
created

Money   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 10.53 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 14
loc 133
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toCapital() 0 10 2
B getIntPart() 6 26 3
A getDecimalPart() 8 18 4
B setMoney() 0 9 5
A parse() 0 5 1
A __construct() 0 3 1
A getMoney() 0 3 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
/*
4
 * This file is part of the trendsoft/capital.
5
 * (c) jabber <[email protected]>
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Capital;
11
12
class Money
13
{
14
    private $money;
15
16
    private $uppers = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
17
18
    private $units = ['分', '角'];
19
20
    private $grees = ['元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰'];
21
22
    private $thanOne = false;
23
24
    /**
25
     * Money constructor.
26
     *
27
     * @param int|number|string $money default 0
28
     */
29 12
    public function __construct($money = 0)
30
    {
31 12
        $this->setMoney($money);
32 11
    }
33
34
    /**
35
     * Get Init Money.
36
     *
37
     * @return string
38
     */
39 2
    public function getMoney(): string
40
    {
41 2
        return $this->money;
42
    }
43
44
    /**
45
     * @param int|number|string $money default 0
46
     */
47 12
    public function setMoney($money = 0)
48
    {
49 12
        if (!(is_float($money) || is_numeric($money) || is_int($money))) {
50 1
            throw new \InvalidArgumentException($money);
51
        }
52 11
        if ($money > 1) {
53 6
            $this->thanOne = true;
54
        }
55 11
        $this->money = number_format($money, 2, '.', '');
0 ignored issues
show
Bug introduced by
It seems like $money can also be of type string; however, parameter $number of number_format() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $this->money = number_format(/** @scrutinizer ignore-type */ $money, 2, '.', '');
Loading history...
56 11
    }
57
58
    /**
59
     * Convert to Capital.
60
     *
61
     * @return string
62
     */
63 4
    public function toCapital(): string
64
    {
65 4
        @list($intPart, $decimalPart) = explode('.', $this->money, 2);
66 4
        if (0.0 === floatval($this->money)) {
67 3
            return '零元';
68
        }
69 3
        $result = $this->getIntPart($intPart);
70 3
        $result .= $this->getDecimalPart($decimalPart);
71
72 3
        return $result;
73
    }
74
75
    /**
76
     * Parse to Capital.
77
     *
78
     * @param int|number|string $money default 0
79
     *
80
     * @return string
81
     */
82 1
    public function parse($money): string
83
    {
84 1
        $this->setMoney($money);
85
86 1
        return $this->toCapital();
87
    }
88
89
    /**
90
     * Get Int Part.
91
     *
92
     * @return string
93
     */
94 3
    private function getIntPart($intPart)
95
    {
96 3
        $result = '';
97 3
        $gree = strlen($intPart) - 1;
98 3 View Code Duplication
        if ($intPart > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99 3
            for ($i = 0; $i < strlen($intPart); ++$i) {
100 3
                $num = $intPart[$i];
101 3
                $result .= $this->uppers[$num].$this->grees[$gree--];
102
            }
103
        }
104
105 3
        $result = str_replace('零亿', '亿零', $result);
106 3
        $result = str_replace('零万', '万零', $result);
107
108 3
        $result = str_replace('零拾', '零', $result);
109 3
        $result = str_replace('零佰', '零', $result);
110 3
        $result = str_replace('零仟', '零', $result);
111
112 3
        $result = str_replace('零零', '零', $result);
113 3
        $result = str_replace('零零', '零', $result);
114
115 3
        $result = str_replace('零亿', '亿', $result);
116 3
        $result = str_replace('零万', '万', $result);
117 3
        $result = str_replace('零元', '元', $result);
118
119 3
        return $result;
120
    }
121
122
    /**
123
     * Get Decimal Part.
124
     *
125
     * @return string
126
     */
127 3
    private function getDecimalPart($decimalPart)
128
    {
129 3
        $result = '';
130 3 View Code Duplication
        if ($decimalPart > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131 1
            $unit = strlen($decimalPart) - 1;
132 1
            for ($i = 0; $i < strlen($decimalPart); ++$i) {
133 1
                $num = $decimalPart[$i];
134 1
                $result .= $this->uppers[$num].$this->units[$unit--];
135
            }
136
        }
137 3
        $result = str_replace('零分', '', $result);
138 3
        if ($this->thanOne) {
139 2
            $result = str_replace('零角', '零', $result);
140
        } else {
141 3
            $result = str_replace('零角', '', $result);
142
        }
143
144 3
        return $result;
145
    }
146
}
147