CardinalNumeralGenerator::generate()   B
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 30

Duplication

Lines 13
Ratio 43.33 %

Code Coverage

Tests 16
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
nc 20
nop 1
dl 13
loc 30
ccs 16
cts 16
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   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
namespace morphos\English;
3
4
use morphos\NumeralGenerator;
5
use RuntimeException;
6
7
class CardinalNumeralGenerator extends NumeralGenerator
8
{
9
    /**
10
     * @var string[]
11
     * @phpstan-var array<int, string>
12
     */
13
    public static $words = [
14
        1 => 'one',
15
        2 => 'two',
16
        3 => 'three',
17
        4 => 'four',
18
        5 => 'five',
19
        6 => 'six',
20
        7 => 'seven',
21
        8 => 'eight',
22
        9 => 'nine',
23
        10 => 'ten',
24
        11 => 'eleven',
25
        12 => 'twelve',
26
        13 => 'thirteen',
27
        14 => 'fourteen',
28
        15 => 'fifteen',
29
        16 => 'sixteen',
30
        17 => 'seventeen',
31
        18 => 'eighteen',
32
        19 => 'nineteen',
33
        20 => 'twenty',
34
        30 => 'thirty',
35
        40 => 'forty',
36
        50 => 'fifty',
37
        60 => 'sixty',
38
        70 => 'seventy',
39
        80 => 'eighty',
40
        90 => 'ninety',
41
    ];
42
43
    /**
44
     * @var string[]
45
     * @phpstan-var array<int, string>
46
     */
47
    public static $exponents = [
48
        100 => 'hundred',
49
        1000 => 'thousand',
50
        1000000 => 'million',
51
        1000000000 => 'billion',
52
        1000000000000 => 'trillion',
53
    ];
54
55
    public static function getCases($number)
56
    {
57
        throw new RuntimeException('Not implemented');
58
    }
59
60
    public static function getCase($number, $case)
61
    {
62
        throw new RuntimeException('Not implemented');
63
    }
64
65
    /**
66
     * @param int $number
67
     * @return mixed|string
68
     */
69 6
    public static function generate($number)
70
    {
71
        // simple numeral
72 6
        if (isset(static::$words[$number]) || isset(static::$exponents[$number])) {
73 6
            return isset(static::$words[$number]) ? static::$words[$number] : static::$exponents[$number];
74
        }
75
        // compound numeral
76
        else {
77 5
            $parts = [];
78 5
            $words = [];
79
80 5 View Code Duplication
            foreach (array_reverse(static::$exponents, true) as $word_number => $word) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81 5
                if ($number >= $word_number) {
82 5
                    $count = (int)floor($number / $word_number);
83 5
                    $number = $number % ($count * $word_number);
84 5
                    $parts[] = static::generate($count).' '.static::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null);
85
                }
86
            }
87
88 5 View Code Duplication
            foreach (array_reverse(static::$words, true) as $word_number => $word) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89 5
                if ($number >= $word_number) {
90 5
                    $words[] = static::generate($word_number);
91 5
                    $number %= $word_number;
92
                }
93
            }
94 5
            $parts[] = implode('-', $words);
95
96 5
            return implode(' ', $parts);
97
        }
98
    }
99
}
100