Locale::numberToWords()   F
last analyzed

Complexity

Conditions 41
Paths > 20000

Size

Total Lines 110

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 78
CRAP Score 41

Importance

Changes 0
Metric Value
cc 41
nc 311067
nop 3
dl 0
loc 110
ccs 78
cts 78
cp 1
crap 41
rs 0
c 0
b 0
f 0

How to fix   Long Method    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
namespace NwLaravel\Locale;
4
5
class Locale
6
{
7
    /**
8
     * Extenso Ordinal
9
     *
10
     * @param float $number
11
     *
12
     * @return string
13
     * @static
14
     */
15 11
    public static function extensoOrdinal($number)
16
    {
17 11
        return self::numberToWords($number, false, true);
18
    }
19
20
    /**
21
     * Extenso Currency
22
     *
23
     * @param float $number
24
     *
25
     * @return string
26
     * @static
27
     */
28 22
    public static function extensoCurrency($number)
29
    {
30 22
        return self::numberToWords($number, true, false);
31
    }
32
33
    /**
34
     * Extenso Cardinal
35
     *
36
     * @param float $number
37
     *
38
     * @return string
39
     * @static
40
     */
41 20
    public static function extensoCardinal($number)
42
    {
43 20
        return self::numberToWords($number, false, false);
44
    }
45
46
    /**
47
     * Extenso
48
     *
49
     * @param float $number
50
     *
51
     * @return string
52
     * @static
53
     */
54
    public static function extenso($number)
55
    {
56
        return self::extensoCurrency($number);
57
    }
58
59
    /**
60
     * Translate
61
     *
62
     * @param boolean $currency
63
     * @param boolean $ordinals
64
     *
65
     * @return string
66
     * @static
67
     */
68 48
    protected static function translate($currency = false, $ordinals = false)
69
    {
70 48
        $translate = array();
71
72 48
        if ($ordinals) {
73 8
            $translate['singular'] = array("", "", "milésimo", "milhão", "bilhão", "trilhão", "quatrilhão");
74 8
            $translate['plural'] = array("", "", "milésimo", "milhões", "bilhões", "trilhões", "quatrilhões");
75 8
            $translate['centanas'] = array("", "centésimo", "ducentésimo", "trecentésimo", "quadrigentésimo", "quingentésimo", "sexcentésimo", "septigentésimo", "octigentésimo", "nongentésimo");
76 8
            $translate['dezenas'] = array("", "décimo", "vigésimo", "trigésimo", "quadragésimo", "quinquagésimo", "sexagésimo", "septuagésimo", "octogésimo", "nonagésimo");
77 8
            $translate['dezenas10'] = array("décimo", "décimo primeiro", "décimo segundo", "décimo terceiro", "décimo quarto", "décimo quinto", "décimo sexto", "décimo sétimo", "décimo oitavo", "décimo nono");
78 8
            $translate['unidades'] = array("", "primeiro", "segundo", "terceiro", "quarto", "quinto", "sexto", "sétimo", "oitavo", "nono");
79 8
            $translate['cento'] = "centésimo";
80 8
            $translate['separator'] = " ";
81 8
            $translate['preposicao'] = " ";
82 8
            $translate['zero'] = "-";
83 8
        } else {
84 40
            $translate['singular'] = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
85 40
            $translate['plural'] = array("", "", "mil", "milhões", "bilhões", "trilhões", "quatrilhões");
86 40
            $translate['centanas'] = array("", "cem", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos");
87 40
            $translate['dezenas'] = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa");
88 40
            $translate['dezenas10'] = array("dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove");
89 40
            $translate['unidades'] = array("", "um", "dois", "tres", "quatro", "cinco", "seis", "sete", "oito", "nove");
90 40
            $translate['cento'] = "cento";
91 40
            $translate['separator'] = " e ";
92 40
            $translate['preposicao'] = "";
93 40
            $translate['zero'] = "zero";
94 40
            $translate['separator_decimal'] = ", ";
95
96 40
            if($currency){
97 21
                $translate['singular'][0] = "centavo";
98 21
                $translate['singular'][1] = "real";
99 21
                $translate['plural'][0]   = "centavos";
100 21
                $translate['plural'][1]   = "reais";
101 21
                $translate['zero'] = "zero reais";
102 21
                $translate['preposicao'] = " de "; // ex: um milhao de reais
103 21
                $translate['separator_decimal'] = " e ";
104 21
            }
105
        }
106
107 48
        return $translate;
108
    }
109
110
    /**
111
     * Number to Extenso String
112
     *
113
     * @param float $number
114
     *
115
     * @return string
116
     */
117 53
    protected static function numberToWords($number, $currency = false, $ordinals = false)
118
    {
119 53
        $number = trim($number);
120 53
        if (preg_match("/[^0-9\.]/", $number)) {
121 3
            return $number;
122
        }
123
124 50
        $valor = $ordinals ? intval($number) : floatval($number);
125
126 50
        if ($ordinals && $valor >= 1000) {
127 2
            return $valor.'º';
128
        }
129
130 48
        $translate = self::translate($currency, $ordinals);
131
132 48
        $number = number_format($valor, 2, ".", ".");
133 48
        $inteiro = explode(".", $number);
134 48
        for ($i=0;$i<count($inteiro);$i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
135 48
            for ($ii=strlen($inteiro[$i]);$ii<3;$ii++) {
136 48
                $inteiro[$i] = "0".$inteiro[$i];
137 48
            }
138 48
        }
139
140 48
        $fim = count($inteiro) - ($inteiro[count($inteiro)-1] > 0 ? 1 : 2);
141 48
        $rt = '';
142 48
        $z = 0;
143 48
        for ($i=0;$i<count($inteiro);$i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
144 48
            $number = $inteiro[$i];
145 48
            if (($number > 100) && ($number < 200)) {
146 6
                $rc = $translate['cento'];
147 6
            } else {
148 48
                $rc = $translate['centanas'][$number[0]];
149
            }
150
151 48
            if ($number[1] < 2) {
152 48
                $rd = "";
153 48
            } else {
154 20
                $rd = $translate['dezenas'][$number[1]];
155
            }
156
157 48
            if ($number > 0) {
158 45
                if ($number[1] == 1) {
159 11
                    $ru = $translate['dezenas10'][$number[2]];
160 11
                } else {
161 39
                    $ru = $translate['unidades'][$number[2]];
162
                }
163 45
            } else {
164 39
                $ru = "";
165
            }
166
167 48
            $r = $rc;
168 48
            if ($rc && ($rd || $ru)) {
169 10
                $r .= $translate['separator'];
170 10
            }
171
172 48
            $r .= $rd;
173 48
            if ($rd && $ru) {
174 16
                $r .= $translate['separator'];
175 16
            }
176
177 48
            $r .= $ru;
178
179 48
            $t = count($inteiro)-1-$i;
180
181 48
            if ($r) {
182 45
                $r .= " ";
183 45
                if ($number > 1 || ($t == 1 && $valor >= 2)) {
184 42
                    $r .= $translate['plural'][$t];
185 42
                } else {
186 10
                    $r .= $translate['singular'][$t];
187
                }
188 45
            }
189
            
190 48
            if ($number == "000") {
191 39
                $z++; 
192 48
            } elseif ($z > 0) {
193 6
                $z--;
194 6
            }
195
            
196 48
            if (($t==1) && ($z>0) && ($inteiro[0] > 0)) {
197 13
                if ($z > 1) {
198 5
                    $r .= $translate['preposicao'];
199 5
                }
200
201 13
                $r .= $translate['plural'][$t];
202 13
            }
203
204 48
            if ($r) {
205 45
                $rt = $rt;
0 ignored issues
show
Bug introduced by
Why assign $rt to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
206 45
                if (($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) {
207
208 14
                    $rt .= (!$currency && $t == 0 && $i==$fim) ? ', ' : $translate['separator'];
209
210 14
                } else {
211 45
                    if ($t == 0 && $rt) {
212 2
                        $rt .= $currency ? $translate['separator'] : ', ';
213 2
                    } else {
214 45
                        $rt .= " ";
215
                    }
216
                }
217
218 45
                $rt .= $r;
219 45
            }
220 48
        }
221
        
222 48
        $rt = preg_replace("/\s+/", " ", $rt);
223 48
        $rt = preg_replace("/\s+,\s*/", ", ", $rt);
224
225 48
        return trim($rt) ?: $translate['zero'];
226
    }
227
228
    /**
229
     * Ufs
230
     *
231
     * @return array
232
     */
233
    public static function states()
234
    {
235
        return config('locales.ufs');
236
    }
237
238
    /**
239
     * Sigla Ufs
240
     *
241
     * @return array
242
     */
243
    public static function siglaStates()
244
    {
245
        $keys = array_keys(self::ufs());
0 ignored issues
show
Bug introduced by
The method ufs() does not seem to exist on object<NwLaravel\Locale\Locale>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
246
        return array_combine($keys, $keys);
247
    }
248
}
249