Passed
Push — master ( a115e4...d08c22 )
by Zlatin
02:28
created

Currency::normalize()   C

Complexity

Conditions 14
Paths 21

Size

Total Lines 42
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 5.0864
c 0
b 0
f 0
cc 14
eloc 35
nc 21
nop 1

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
3
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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    const LANG_BG = 'bg';
7
    const LANG_EN = 'en';
8
9
    /**
10
     *
11
     * @var string
12
     */
13
    private static $lang;
14
    
15
    /**
16
     *
17
     * @var array
18
     */
19
    private static $trans;
20
21
    /**
22
     * 
23
     * @param int $amount
24
     * @param string $lang
25
     * 
26
     * @return string
27
     * 
28
     * @throws Exception
29
     */
30
    public static function convertToText($amount, $lang = self::LANG_BG)
31
    {
32
33
        self::$lang = $lang;
34
35
        if (null === self::$trans) {
36
            self::$trans = parse_ini_file(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'lang.ini', true);
37
        }
38
39
        if (!isset(self::$trans[self::$lang])) {
40
            throw new InvalidArgumentException('Invalid lang specified');
41
        }
42
        
43
        if (strchr($amount, '.')) {
44
            list($sum, $cents) = array_map(function($value) {
45
                settype($value, 'int');
46
                return $value;
47
            }, explode('.', $amount));
48
        } else {
49
            $sum = intval($amount);
50
            $cents = 00;
51
        }
52
53
        $results = array(
54
            self::normalize($sum),
55
            $sum == 1 ? self::$trans[self::$lang]['amount'] : self::$trans[self::$lang]['amounts']
56
        );
57
58
        if ($cents !== 0) {
59
            $results[] = trim(self::$trans[self::$lang]['and']);
60
            $results[] = self::normalize($cents);
61
            $results[] = $cents == 1 ? self::$trans[self::$lang]['cent'] : self::$trans[self::$lang]['cents'];
62
        }
63
64
        return self::standartize(implode(' ', $results));
65
    }
66
67
    /**
68
     * 
69
     * @param int $value
70
     * 
71
     * @return string
72
     * 
73
     * @throws Exception
74
     */
75
    public static function normalize($value)
76
    {
77
78
        switch (true) {
79
            case $value === 0 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
80
                return "";
81
            case isset(self::$trans[self::$lang][$value]) :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
82
                return self::$trans[self::$lang][$value];
83
            case $value < 100 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
84
                $module = $value % 10;
85
                $division = floor($value / 10);
86
                if (self::LANG_BG === self::$lang) {
87
                    if (isset(self::$trans[self::$lang][$value])) {
88
                        return self::$trans[self::$lang][$value];
89
                    } else {
90
                        return self::$trans[self::$lang][$division] . self::$trans[self::$lang]['tens'] . self::$trans[self::$lang]['and'] . self::$trans[self::$lang][$module];
91
                    }
92
                } else {
93
                    return self::$trans[self::$lang][$value - $module] . '-' . self::$trans[self::$lang][$module];
94
                }
95
            case $value < 1000 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
96
                $module = $value % 100;
97
                $division = floor($value / 100);
98
                $and = $module <= 20 || $module % 10 === 0 ? self::$trans[self::$lang]['and'] : ' ';
99
                if (self::LANG_BG === self::$lang) {
100
                    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);
101
                } else {
102
                    return self::$trans[self::$lang][$division] . ' ' . self::$trans[self::$lang]['hundreds'] . $and . self::normalize($module);
103
                }
104
            case $value < 1000000 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
105
                $module = $value % 1000;
106
                $division = $value / 1000;
107
                return self::normalize($division) . ' ' . self::$trans[self::$lang]['thousands'] . ' ' . self::normalize($module);
108
            case $value < 1000000000 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
109
                $module = $value % 1000000;
110
                $division = $value / 1000000;
111
                $millions = floor($value / 1000000);
112
                return self::normalize($division) . ' ' . ($millions == 1 ? self::$trans[self::$lang]['million'] : self::$trans[self::$lang]['millions']) . ' ' . self::normalize($module);
113
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
                throw new InvalidArgumentException("$value is greeater than 1000000000");
115
        }
116
    }
117
118
    /**
119
     * 
120
     * @param string $value
121
     * 
122
     * @return string
123
     */
124
    public static function standartize($value)
125
    {
126
        $original = array('един стотинка', 'два стотинки');
127
        $standartize = array('една стотинка', 'две стотинки');
128
        return str_replace($original, $standartize, $value);
129
    }
130
131
}
132