CardinalNumeralGenerator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 13.98 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 13
loc 93
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCases() 0 4 1
A getCase() 0 4 1
B generate() 13 30 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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