Passed
Push — master ( 5d12c1...a56b32 )
by Zlatin
01:30
created

Currency::normalize()   C

Complexity

Conditions 14
Paths 21

Size

Total Lines 42
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 26.544

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 18
cts 30
cp 0.6
rs 5.0864
c 0
b 0
f 0
cc 14
eloc 35
nc 21
nop 1
crap 26.544

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Util;
3
4
class Currency
0 ignored issues
show
Coding Style introduced by
Currency does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
5
{
6
7
    const LANG_BG = 'bg';
8
    const LANG_EN = 'en';
9
10
    /**
11
     *
12
     * @var string
13
     */
14
    private static $lang;
15
    
16
    /**
17
     *
18
     * @var array
19
     */
20
    private static $trans;
21
22
    /**
23
     * 
24
     * @param int $amount
25
     * @param string $lang
26
     * 
27
     * @return string
28
     * 
29
     * @throws Exception
30
     */
31 2
    public static function convertToText($amount, $lang = self::LANG_BG)
32
    {
33
34 2
        self::$lang = $lang;
35
36 2
        if (null === self::$trans) {
37 1
            self::$trans = parse_ini_file(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'lang.ini', true);
38
        }
39
40 2
        if (!isset(self::$trans[self::$lang])) {
41 1
            throw new \InvalidArgumentException('Invalid lang specified');
42
        }
43
        
44 1
        if (strchr($amount, '.')) {
45 1
            list($sum, $cents) = array_map(function($value) {
46 1
                settype($value, 'int');
47 1
                return $value;
48 1
            }, explode('.', $amount));
49
        } else {
50
            $sum = intval($amount);
51
            $cents = 00;
52
        }
53
54
        $results = array(
55 1
            self::normalize($sum),
56 1
            $sum == 1 ? self::$trans[self::$lang]['amount'] : self::$trans[self::$lang]['amounts']
57
        );
58
59 1
        if ($cents !== 0) {
60 1
            $results[] = trim(self::$trans[self::$lang]['and']);
61 1
            $results[] = self::normalize($cents);
62 1
            $results[] = $cents == 1 ? self::$trans[self::$lang]['cent'] : self::$trans[self::$lang]['cents'];
63
        }
64
65 1
        return self::standartize(implode(' ', $results));
66
    }
67
68
    /**
69
     * 
70
     * @param int $value
71
     * 
72
     * @return string
73
     * 
74
     * @throws Exception
75
     */
76 1
    public static function normalize($value)
77
    {
78
79
        switch (true) {
80 1
            case $value === 0:
81
                return "";
82 1
            case isset(self::$trans[self::$lang][$value]):
83 1
                return self::$trans[self::$lang][$value];
84 1
            case $value < 100:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
85 1
                $module = $value % 10;
86 1
                $division = floor($value / 10);
87 1
                if (self::LANG_BG === self::$lang) {
88 1
                    if (isset(self::$trans[self::$lang][$value])) {
89
                        return self::$trans[self::$lang][$value];
90
                    } else {
91 1
                        return self::$trans[self::$lang][$division] . self::$trans[self::$lang]['tens'] . self::$trans[self::$lang]['and'] . self::$trans[self::$lang][$module];
92
                    }
93
                } else {
94 1
                    return self::$trans[self::$lang][$value - $module] . '-' . self::$trans[self::$lang][$module];
95
                }
96 1
            case $value < 1000:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
97 1
                $module = $value % 100;
98 1
                $division = floor($value / 100);
99 1
                $and = $module <= 20 || $module % 10 === 0 ? self::$trans[self::$lang]['and'] : ' ';
100 1
                if (self::LANG_BG === self::$lang) {
101 1
                    return (isset(self::$trans[self::$lang][$value - $module]) ? self::$trans[self::$lang][$value - $module] : self::$trans[self::$lang][$division] . self::$trans[self::$lang]['hundreds']) . $and . self::normalize($module);
102
                } else {
103 1
                    return self::$trans[self::$lang][$division] . ' ' . self::$trans[self::$lang]['hundreds'] . $and . self::normalize($module);
104
                }
105
            case $value < 1000000:
106
                $module = $value % 1000;
107
                $division = $value / 1000;
108
                return self::normalize($division) . ' ' . self::$trans[self::$lang]['thousands'] . ' ' . self::normalize($module);
109
            case $value < 1000000000:
110
                $module = $value % 1000000;
111
                $division = $value / 1000000;
112
                $millions = floor($value / 1000000);
113
                return self::normalize($division) . ' ' . ($millions == 1 ? self::$trans[self::$lang]['million'] : self::$trans[self::$lang]['millions']) . ' ' . self::normalize($module);
114
            default:
115
                throw new \InvalidArgumentException("$value is greeater than 1000000000");
116
        }
117
    }
118
119
    /**
120
     * 
121
     * @param string $value
122
     * 
123
     * @return string
124
     */
125 1
    public static function standartize($value)
126
    {
127 1
        $original = array('един стотинка', 'два стотинки');
128 1
        $standartize = array('една стотинка', 'две стотинки');
129 1
        return str_replace($original, $standartize, $value);
130
    }
131
132
}
133