OrdinalNumeralGenerator   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 76
Duplicated Lines 9.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.88%

Importance

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