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 ( f5899e...bb347a )
by Serge
01:41
created

Number2Text::checkSingleChunk()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
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
 * @author    Sergey Kanashin <[email protected]>
11
 * @copyright 2003-2017
12
 */
13
final class Number2Text
14
{
15
    private $data;
16
    private $iNumber;
17
    private $currency;
18
    private $arrChunks;
19
20 1
    public function currency(bool $show = true)
21
    {
22 1
        $this->currency = $show;
23 1
    }
24
25 7
    public function convert(string $input): string
26
    {
27 7
        $this->initData($input);
28 7
        $input === '0' ? $fullResult = 'ноль ' : $fullResult = '';
29 7
        $numGroups = count($this->arrChunks);
30
31 7
        $this->data = new Data();
32
33 7
        for ($i = $numGroups; $i >= 1; $i--) {
34 7
            $fullResult .= $this->getWords($i);
35
        }
36
37 7
        return $fullResult;
38
    }
39
40 7
    private function initData(string $number)
41
    {
42 7
        $this->iNumber = preg_replace("/[^\d]/", "", $number);
43 7
        $rvrsValue = strrev($this->iNumber);
44 7
        $chunks = chunk_split($rvrsValue, 3);
45 7
        $this->arrChunks = explode("\r\n", $chunks);
46 7
    }
47
48 7
    private function getWords(int $iterator): string
49
    {
50 7
        $currChunk = (int)strrev($this->arrChunks[$iterator - 1]);
51 7
        $iterator < 3 ? $this->switchArray($iterator) : true;
52 7
        $preResult = $this->makeWords($currChunk);
53
54 7
        if ($currChunk !== 0 || $iterator === 1) {
55 7
            $preResult .= $this->getExponent($iterator, $currChunk);
56
        }
57
58 7
        return $preResult;
59
    }
60
61 7
    private function switchArray(int $group)
62
    {
63 7
        if ($group === 2) {
64 7
            $this->data->arrUnits[0] = 'одна ';
65 7
            $this->data->arrUnits[1] = 'две ';
66
67 7
            return;
68
        }
69 7
        $this->data->arrUnits[0] = 'один ';
70 7
        $this->data->arrUnits[1] = 'два ';
71 7
    }
72
73 7
    private function makeWords(int $cChunk): string
74
    {
75 7
        $decs = $cChunk % 100;
76 7
        $resWords = $this->getCentum($cChunk);
77
78 7
        if ($decs === 0) {
79 7
            return $resWords;
80
        }
81
82 7
        $resWords .= $this->getDecem($decs);
83
84 7
        return $resWords;
85
    }
86
87 7
    private function getCentum(int $chunk): string
88
    {
89 7
        $cent = (int)($chunk / 100);
90
91 7
        if ($cent >= 1) {
92 5
            return $this->data->arrHundreds[$cent - 1];
93
        }
94
95 7
        return '';
96
    }
97
98 7
    private function getDecem(int $decs): string
99
    {
100 7
        $result = '';
101 7
        if ($decs < 20) {
102 7
            $result .= $this->data->arrUnits[$decs - 1];
103
104 7
            return $result;
105
        }
106
107 5
        $result .= $this->data->arrTens[$decs / 10 - 1];
108
109 5
        if ($decs % 10 !== 0) {
110 5
            $result .= $this->data->arrUnits[$decs % 10 - 1];
111
        }
112
113 5
        return $result;
114
    }
115
116 7
    private function getExponent(int $chunkPos, int $chunkData): string
117
    {
118 7
        if (!$this->currency && $chunkPos === 1) {
119 6
            return '';
120
        }
121 4
        $exponent = $this->data->arrExponents[$chunkPos];
122 4
        $chunkPos > 3 ? $chunkPos = 3 : true;
123 4
        $index = $this->getIndex($chunkData % 100);
124 4
        $suffix = $this->data->arrSuffix[$index][$chunkPos];
125
126 4
        return $exponent . $suffix;
127
    }
128
129 4
    private function getIndex(int $lastDigits): int
130
    {
131 4
        $last = $lastDigits % 10;
132
133 4
        if ($lastDigits >= 11 && $lastDigits <= 14) {
134 3
            return 2;
135
        }
136
137 4
        return $this->checkSingleChunk($last);
138
    }
139
140 4
    public function checkSingleChunk($digit)
141
    {
142 4
        if ($digit === 1) {
143 4
            return 0;
144
        }
145 4
        if ($digit >= 2 && $digit <= 4) {
146 4
            return 1;
147
        }
148
149 3
        return 2;
150
    }
151
152
}
153