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 ( c86feb...69b2b7 )
by Serge
03:31
created

Number2Text::magicConverter()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 2
crap 5
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 $sign;
19
    private $arrChunks;
20
21 8
    public function convert(string $input, bool $show = false): string
22
    {
23 8
        $this->currency = $show;
24 8
        $this->initData($input);
25 8
        $fullResult = '';
26 8
        if ($this->iNumber === '0') {
27 3
            $fullResult = 'ноль ';
28 3
            $this->sign = '';
29
        }
30 8
        $numGroups = count($this->arrChunks);
31
32 8
        return $this->magicConverter($numGroups, $fullResult);
33
    }
34
35 8
    private function magicConverter(int $numgrps, string $fullres): string
36
    {
37 8
        $this->data = new Data();
38
39 8
        for ($i = $numgrps; $i >= 1; $i--) {
40 8
            $currChunk = (int)strrev($this->arrChunks[$i - 1]);
41 8
            $i < 3 ? $this->fixArray($i) : true;
42 8
            $preResult = $this->makeWords($currChunk);
43 8
            if ($currChunk !== 0 || $i === 1) {
44 8
                $preResult .= $this->getRegister($i, $currChunk);
45
            }
46 8
            $fullres .= $preResult;
47
        }
48
49 8
        return $this->sign . $fullres;
50
    }
51
52 8
    private function initData(string $number)
53
    {
54 8
        $this->sign = '';
55 8
        if (substr($number, 0, 1) === "-") {
56 2
            $this->sign = 'минус ';
57 2
            $number = substr($number, 1);
58
        }
59 8
        $this->iNumber = preg_replace("/[^\d]/", "", $number);
60
61 8
        $rvrsValue = strrev($this->iNumber);
62 8
        $chunks = chunk_split($rvrsValue, 3);
63 8
        $this->arrChunks = explode("\r\n", $chunks);
64 8
    }
65
66 8
    private function fixArray(int $group)
67
    {
68 8
        if ($group === 2) {
69 8
            $this->data->arrUnits[0] = 'одна ';
70 8
            $this->data->arrUnits[1] = 'две ';
71
        } else {
72 8
            $this->data->arrUnits[0] = 'один ';
73 8
            $this->data->arrUnits[1] = 'два ';
74
        }
75 8
    }
76
77 8
    private function makeWords(int $cChunk): string
78
    {
79 8
        $resWords = '';
80 8
        $cent = (int)($cChunk / 100);
81 8
        $decs = $cChunk % 100;
82 8
        if ($cent >= 1) {
83 6
            $resWords .= $this->data->arrHundreds[$cent - 1];
84
        }
85 8
        if ($decs === 0) {
86 8
            return $resWords;
87
        }
88 8
        if ($decs < 20) {
89 8
            $resWords .= $this->data->arrUnits[$decs - 1];
90 8
            return $resWords;
91 6
        } elseif ($decs !== 0) {
92 6
            $resWords .= $this->data->arrTens[$decs / 10 - 1];
93
        }
94 6
        if ($decs % 10 !== 0) {
95 6
            $resWords .= $this->data->arrUnits[$decs % 10 - 1];
96
        }
97
98 6
        return $resWords;
99
    }
100
101 8
    private function getRegister(int $chunkPos, int $chunkData): string
102
    {
103 8
        if (!$this->currency && $chunkPos === 1) {
104 7
            return '';
105
        }
106 5
        $lastTwoDigits = $chunkData % 100;
107 5
        $exponent = $this->data->arrExponents[$chunkPos];
108 5
        if ($chunkPos > 3) {
109 3
            $chunkPos = 3;
110
        }
111 5
        $index = $this->getSuffix($lastTwoDigits);
112 5
        $suffix = $this->data->arrSuffix[$index][$chunkPos];
113 5
        return $exponent . $suffix;
114
    }
115
116 5
    private function getSuffix(int $lastDigits): int
117
    {
118 5
        $last = $lastDigits % 10;
119
120 5
        if ($lastDigits >= 11 && $lastDigits <= 14) {
121 3
            return 2;
122
        }
123
124 5
        if ($last === 1) {
125 4
            return 0;
126
        }
127 5
        if ($last >= 2 && $last <= 4) {
128 4
            return 1;
129
        }
130
131 4
        return 2;
132
    }
133
}
134