Completed
Push — master ( 6a872b...95bb87 )
by f
01:14
created

CardinalNumeralGenerator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 13
loc 78
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCases() 0 3 1
A getCase() 0 3 1
D 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
6
class CardinalNumeralGenerator extends NumeralGenerator
7
{
8
    public static $words = array(
9
        1 => 'one',
10
        2 => 'two',
11
        3 => 'three',
12
        4 => 'four',
13
        5 => 'five',
14
        6 => 'six',
15
        7 => 'seven',
16
        8 => 'eight',
17
        9 => 'nine',
18
        10 => 'ten',
19
        11 => 'eleven',
20
        12 => 'twelve',
21
        13 => 'thirteen',
22
        14 => 'fourteen',
23
        15 => 'fifteen',
24
        16 => 'sixteen',
25
        17 => 'seventeen',
26
        18 => 'eighteen',
27
        19 => 'nineteen',
28
        20 => 'twenty',
29
        30 => 'thirty',
30
        40 => 'forty',
31
        50 => 'fifty',
32
        60 => 'sixty',
33
        70 => 'seventy',
34
        80 => 'eighty',
35
        90 => 'ninety',
36
    );
37
38
    public static $exponents = array(
39
        '100' => 'hundred',
40
        '1000' => 'thousand',
41
        '1000000' => 'million',
42
        '1000000000' => 'billion',
43
        '1000000000000' => 'trillion',
44
    );
45
46
    public static function getCases($number)
47
    {
48
    }
49
    public static function getCase($number, $case)
50
    {
51
    }
52
53
    public static function generate($number)
54
    {
55
        // simple numeral
56
        if (isset(self::$words[$number]) || isset(self::$exponents[$number])) {
57
            return isset(self::$words[$number]) ? self::$words[$number] : self::$exponents[$number];
58
        }
59
        // compound numeral
60
        else {
61
            $parts = array();
62
            $words = array();
63
64 View Code Duplication
            foreach (array_reverse(self::$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...
65
                if ($number >= $word_number) {
66
                    $count = floor($number / $word_number);
67
                    $number = $number % ($count * $word_number);
68
                    $parts[] = self::generate($count).' '.self::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null);
69
                }
70
            }
71
72 View Code Duplication
            foreach (array_reverse(self::$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...
73
                if ($number >= $word_number) {
74
                    $words[] = self::generate($word_number);
75
                    $number %= $word_number;
76
                }
77
            }
78
            $parts[] = implode('-', $words);
79
80
            return implode(' ', $parts);
81
        }
82
    }
83
}
84