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.

Number2Text::switchArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
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 with all triplets
40
     *
41
     * @var mixed array
42
     */
43
    private $arrChunks;
44
45
    /**
46
     * Load of functional data from Data class.
47
     *
48
     * Number2Text constructor.
49
     */
50 7
    public function __construct()
51
    {
52 7
        $this->data = new Data();
53 7
    }
54
55
    /**
56
     * Sets the visibility of currency name
57
     *
58
     * @param bool $show
59
     */
60 1
    public function currency(bool $show = true)
61
    {
62 1
        $this->currency = $show;
63 1
    }
64
65 6
    public function convert(string $input): string
66
    {
67 6
        $this->initData($input);
68
69 6
        $input === '0' ? $fullResult = 'ноль ' : $fullResult = null;
70
71 6
        return implode($this->fetchData($fullResult));
72
    }
73
74
    /**
75
     * Iterates through triplets and calls main converter method
76
     *
77
     * @param $fres
78
     * @return array
79
     */
80 6
    public function fetchData($fres): array
81
    {
82 6
        $numGroups = count($this->arrChunks);
83
84 6
        $fullResult[] = $fres;
1 ignored issue
show
Coding Style Comprehensibility introduced by
$fullResult was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fullResult = 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...
85 6
        for ($i = $numGroups; $i >= 1; $i--) {
86 6
            $fullResult[] = $this->getWords($i);
87
        }
88
89 6
        return $fullResult;
90
    }
91
92
    /**
93
     * Removes non numeric data from number and divides it to by chunks, 3 digits each (triplets)
94
     *
95
     * @param string $number
96
     */
97 6
    private function initData(string $number)
98
    {
99 6
        $this->iNumber = preg_replace("/[^\d]/", "", $number);
100 6
        $rvrsValue = strrev($this->iNumber);
101 6
        $chunks = chunk_split($rvrsValue, 3);
102 6
        $this->arrChunks = explode("\r\n", $chunks);
103 6
    }
104
105
    /**
106
     * Get the triplet and sends it to the methods to process
107
     *
108
     * @param int $iterator
109
     * @return string
110
     */
111 6
    private function getWords(int $iterator): string
112
    {
113 6
        $currChunk = (int)strrev($this->arrChunks[$iterator - 1]);
114 6
        $iterator < 3 ? $this->switchArray($iterator) : true;
115 6
        $preResult = $this->makeWords($currChunk);
116
117 6
        if ($currChunk !== 0 || $iterator === 1) {
118 6
            $preResult .= $this->getExponent($iterator, $currChunk);
119
        }
120
121 6
        return $preResult;
122
    }
123
124
    /**
125
     * Depending on group's gender switches array to adjust that
126
     *
127
     * @param int $group
128
     */
129 6
    private function switchArray(int $group)
130
    {
131 6
        if ($group === 2) {
132 6
            $this->data->arrUnits[0] = 'одна ';
133 6
            $this->data->arrUnits[1] = 'две ';
134
135 6
            return;
136
        }
137 6
        $this->data->arrUnits[0] = 'один ';
138 6
        $this->data->arrUnits[1] = 'два ';
139 6
    }
140
141
    /**
142
     * Parent method for transforming the triplet to its text representation
143
     *
144
     * @param int $cChunk
145
     * @return string
146
     */
147 6
    private function makeWords(int $cChunk): string
148
    {
149 6
        $decs = $cChunk % 100;
150 6
        $resWords = $this->getCentum($cChunk);
151
152 6
        if ($decs === 0) {
153 6
            return $resWords;
154
        }
155
156 6
        $resWords .= $this->getDecem($decs);
157
158 6
        return $resWords;
159
    }
160
161
    /**
162
     * Returns wording for hundreds
163
     *
164
     * @param int $chunk
165
     * @return string
166
     */
167 6
    private function getCentum(int $chunk): string
168
    {
169 6
        $cent = (int)($chunk / 100);
170
171 6
        if ($cent >= 1) {
172 4
            return $this->data->arrHundreds[$cent - 1];
173
        }
174
175 6
        return '';
176
    }
177
178
    /**
179
     *  Returns wording for tens and/or teens
180
     *
181
     * @param int $decs
182
     * @return string
183
     */
184 6
    private function getDecem(int $decs): string
185
    {
186 6
        $result = '';
187 6
        if ($decs < 20) {
188 6
            $result .= $this->data->arrUnits[$decs - 1];
189
190 6
            return $result;
191
        }
192
193 4
        $result .= $this->data->arrTens[$decs / 10 - 1];
194
195 4
        if ($decs % 10 !== 0) {
196 4
            $result .= $this->data->arrUnits[$decs % 10 - 1];
197
        }
198
199 4
        return $result;
200
    }
201
202
    /**
203
     * Gets the exponent name and returns it along with suffix (language specific)
204
     *
205
     * @param int $chunkPos
206
     * @param int $chunkData
207
     * @return string
208
     */
209 6
    private function getExponent(int $chunkPos, int $chunkData): string
210
    {
211 6
        if (!$this->currency && $chunkPos === 1) {
212 5
            return '';
213
        }
214 3
        $exponent = $this->data->arrExponents[$chunkPos];
215 3
        $chunkPos > 3 ? $chunkPos = 3 : true;
216 3
        $index = $this->getIndex($chunkData % 100);
217 3
        $suffix = $this->data->arrSuffix[$index][$chunkPos];
218
219 3
        return $exponent . $suffix;
220
    }
221
222
    /**
223
     * Returns index for suffix of two-digit number
224
     *
225
     * @param int $lastDigits
226
     * @return int
227
     */
228 3
    private function getIndex(int $lastDigits): int
229
    {
230 3
        $last = $lastDigits % 10;
231
232 3
        if ($lastDigits >= 11 && $lastDigits <= 14) {
233 2
            return 2;
234
        }
235
236 3
        return $this->checkSingleChunk($last);
237
    }
238
239
    /**
240
     * Returns index for suffix of single digit number
241
     *
242
     * @param int $digit
243
     * @return int
244
     */
245 3
    public function checkSingleChunk(int $digit): int
246
    {
247 3
        if ($digit === 1) {
248 3
            return 0;
249
        }
250 3
        if ($digit >= 2 && $digit <= 4) {
251 3
            return 1;
252
        }
253
254 2
        return 2;
255
    }
256
}
257