OrdinalNumeralGenerator   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 82
Duplicated Lines 8.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
dl 7
loc 82
ccs 29
cts 33
cp 0.8788
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCases() 0 4 1
A getCase() 0 4 1
C generate() 7 51 16

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 OrdinalNumeralGenerator extends NumeralGenerator
8
{
9
    /**
10
     * @var string[]
11
     * @phpstan-var array<int, string>
12
     */
13
    protected static $words = [
14
        1 => 'first',
15
        2 => 'second',
16
        3 => 'third',
17
        5 => 'fifth',
18
        8 => 'eighth',
19
        9 => 'ninth',
20
        12 => 'twelfth',
21
    ];
22
23
    public static function getCases($number)
24
    {
25
        throw new RuntimeException('Not implemented');
26
    }
27
    public static function getCase($number, $case)
28
    {
29
        throw new RuntimeException('Not implemented');
30
    }
31
32
    /**
33
     * @param int $number
34
     * @param bool $short
35
     * @return string
36
     */
37 5
    public static function generate($number, $short = false)
38
    {
39
        // simple numeral
40 5
        if (isset(static::$words[$number])) {
41 3
            return !$short ? static::$words[$number] : $number.substr(static::$words[$number], -2);
42 4
        } elseif (isset(CardinalNumeralGenerator::$words[$number]) || isset(CardinalNumeralGenerator::$exponents[$number])) {
43 2
            if ($short) {
44 1
                return $number.'th';
45
            }
46 2
            $word = isset(CardinalNumeralGenerator::$words[$number]) ? CardinalNumeralGenerator::$words[$number] : CardinalNumeralGenerator::$exponents[$number];
47 2
            if (substr($word, -1) == 'y') {
48 1
                $word = substr($word, 0, -1).'ie';
49
            }
50 2
            $word .= 'th';
51 2
            return $word;
52
        }
53
        // compound numeral
54
        else {
55 3
            $parts = [];
56 3
            $words = [];
57
58 3
            $original_number = $number;
59
60 3 View Code Duplication
            foreach (array_reverse(CardinalNumeralGenerator::$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...
61 3
                if ($number >= $word_number) {
62 3
                    $count = (int)floor($number / $word_number);
63 3
                    $number = $number % ($count * $word_number);
64 3
                    $parts[] = CardinalNumeralGenerator::generate($count).' '.CardinalNumeralGenerator::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null);
65
                }
66
            }
67
68 3
            foreach (array_reverse(CardinalNumeralGenerator::$words, true) as $word_number => $word) {
69 3
                if ($number >= $word_number) {
70
                    // if last part
71 3
                    if ($number % $word_number === 0) {
72 3
                        $words[] = static::generate($word_number);
73
                    } else {
74 2
                        $words[] = CardinalNumeralGenerator::generate($word_number);
75
                    }
76 3
                    $number %= $word_number;
77
                }
78
            }
79 3
            $parts[] = implode('-', $words);
80
81 3
            if ($short) {
82 3
                return $original_number.substr(array_pop($words), -2);
83
            }
84
85 3
            return implode(' ', $parts);
86
        }
87
    }
88
}
89