GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dc3c4a...6e5d77 )
by Serge
01:42
created

Number2Text::getWords()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Converter\Core;
5
6
use Converter\Init\Data;
7
8
/**
9
 * Converts a number up to 1e+510 to its text representation e.g. 312 -> триста двенадцать (Russian only).
10
 *
11
 * @author    Sergey Kanashin <[email protected]>
12
 * @copyright 2003-2017
13
 */
14
final class Number2Text
15
{
16
17
    /**
18
     * Contains all the language specific data for Number2Text class
19
     *
20
     * @var object
21
     */
22
    private $data;
23
24
    /**
25
     * The number to be converted to text
26
     *
27
     * @var string
28
     */
29
    private $iNumber;
30
31
    /**
32
     * Flag that indicates whether to show currency name
33
     *
34
     * @var bool
35
     */
36
    private $currency;
37
38
    /**
39
     * Array of triplets
40
     *
41
     * @var mixed array
42
     */
43
    private $arrChunks;
44
45
    /**
46
     * Number2Text constructor. Implements loading of functional data.
47
     */
48 7
    public function __construct()
49
    {
50 7
        $this->data = new Data();
51 7
    }
52
53
    /**
54
     * Sets the visibility of currency name
55
     *
56
     * @param bool $show
57
     */
58 1
    public function currency(bool $show = true)
59
    {
60 1
        $this->currency = $show;
61 1
    }
62
63 6
    public function convert(string $input): string
64
    {
65 6
        $this->initData($input);
66 6
        $input === '0' ? $fres[0] = 'ноль ' : $fres = [];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fres was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fres = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
68 6
        return implode($this->fetchData($fres));
69
    }
70
71 6
    public function fetchData(array $fullResult): array
72
    {
73 6
        $numGroups = count($this->arrChunks);
74
75 6
        for ($i = $numGroups; $i >= 1; $i--) {
76 6
            $fullResult[] = $this->getWords($i);
77
        }
78
79 6
        return $fullResult;
80
    }
81
82 6
    private function initData(string $number)
83
    {
84 6
        $this->iNumber = preg_replace("/[^\d]/", "", $number);
85 6
        $rvrsValue = strrev($this->iNumber);
86 6
        $chunks = chunk_split($rvrsValue, 3);
87 6
        $this->arrChunks = explode("\r\n", $chunks);
88 6
    }
89
90 6
    private function getWords(int $iterator): string
91
    {
92 6
        $currChunk = (int)strrev($this->arrChunks[$iterator - 1]);
93 6
        $iterator < 3 ? $this->switchArray($iterator) : true;
94 6
        $preResult = $this->makeWords($currChunk);
95
96 6
        if ($currChunk !== 0 || $iterator === 1) {
97 6
            $preResult .= $this->getExponent($iterator, $currChunk);
98
        }
99
100 6
        return $preResult;
101
    }
102
103
    /**
104
     * Depending on group's gender switches array to adjust that
105
     *
106
     * @param int $group
107
     */
108 6
    private function switchArray(int $group)
109
    {
110 6
        if ($group === 2) {
111 6
            $this->data->arrUnits[0] = 'одна ';
112 6
            $this->data->arrUnits[1] = 'две ';
113
114 6
            return;
115
        }
116 6
        $this->data->arrUnits[0] = 'один ';
117 6
        $this->data->arrUnits[1] = 'два ';
118 6
    }
119
120 6
    private function makeWords(int $cChunk): string
121
    {
122 6
        $decs = $cChunk % 100;
123 6
        $resWords = $this->getCentum($cChunk);
124
125 6
        if ($decs === 0) {
126 6
            return $resWords;
127
        }
128
129 6
        $resWords .= $this->getDecem($decs);
130
131 6
        return $resWords;
132
    }
133
134 6
    private function getCentum(int $chunk): string
135
    {
136 6
        $cent = (int)($chunk / 100);
137
138 6
        if ($cent >= 1) {
139 4
            return $this->data->arrHundreds[$cent - 1];
140
        }
141
142 6
        return '';
143
    }
144
145 6
    private function getDecem(int $decs): string
146
    {
147 6
        $result = '';
148 6
        if ($decs < 20) {
149 6
            $result .= $this->data->arrUnits[$decs - 1];
150
151 6
            return $result;
152
        }
153
154 4
        $result .= $this->data->arrTens[$decs / 10 - 1];
155
156 4
        if ($decs % 10 !== 0) {
157 4
            $result .= $this->data->arrUnits[$decs % 10 - 1];
158
        }
159
160 4
        return $result;
161
    }
162
163 6
    private function getExponent(int $chunkPos, int $chunkData): string
164
    {
165 6
        if (!$this->currency && $chunkPos === 1) {
166 5
            return '';
167
        }
168 3
        $exponent = $this->data->arrExponents[$chunkPos];
169 3
        $chunkPos > 3 ? $chunkPos = 3 : true;
170 3
        $index = $this->getIndex($chunkData % 100);
171 3
        $suffix = $this->data->arrSuffix[$index][$chunkPos];
172
173 3
        return $exponent . $suffix;
174
    }
175
176 3
    private function getIndex(int $lastDigits): int
177
    {
178 3
        $last = $lastDigits % 10;
179
180 3
        if ($lastDigits >= 11 && $lastDigits <= 14) {
181 2
            return 2;
182
        }
183
184 3
        return $this->checkSingleChunk($last);
185
    }
186
187 3
    public function checkSingleChunk(int $digit): int
188
    {
189 3
        if ($digit === 1) {
190 3
            return 0;
191
        }
192 3
        if ($digit >= 2 && $digit <= 4) {
193 3
            return 1;
194
        }
195
196 2
        return 2;
197
    }
198
}
199