Completed
Push — master ( e62ae9...24169f )
by f
01:16
created

OrdinalNumeral::generate()   C

Complexity

Conditions 16
Paths 55

Size

Total Lines 43
Code Lines 28

Duplication

Lines 7
Ratio 16.28 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 28
nc 55
nop 2
dl 7
loc 43
rs 5.0151
c 1
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\NumeralCreation;
5
6
class OrdinalNumeral extends NumeralCreation {
7
8
    static protected $words = array(
9
        1 => 'first',
10
        2 => 'second',
11
        3 => 'third',
12
        5 => 'fifth',
13
        8 => 'eighth',
14
        9 => 'ninth',
15
        12 => 'twelfth',
16
    );
17
18
    static public function getCases($number) {}
19
    static public function getCase($number, $case) {}
20
21
    static public function generate($number, $short = false) {
22
        // simple numeral
23
        if (isset(self::$words[$number])) {
24
            return !$short ? self::$words[$number] : $number.substr(self::$words[$number], -2);
25
        } else if (isset(CardinalNumeral::$words[$number]) || isset(CardinalNumeral::$exponents[$number])) {
26
            if ($short) return $number.'th';
27
            $word = isset(CardinalNumeral::$words[$number]) ? CardinalNumeral::$words[$number] : CardinalNumeral::$exponents[$number];
28
            if (substr($word, -1) == 'y') $word = substr($word, 0, -1).'ie';
29
            $word .= 'th';
30
            return $word;
31
        }
32
        // compound numeral
33
        else {
34
            $parts = array();
35
            $words = array();
36
37
            $original_number = $number;
38
39 View Code Duplication
            foreach (array_reverse(CardinalNumeral::$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...
40
                if ($number >= $word_number) {
41
                    $count = floor($number / $word_number);
42
                    $number = $number % ($count * $word_number);
43
                    $parts[] = CardinalNumeral::generate($count).' '.CardinalNumeral::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null);
44
                }
45
            }
46
47
            foreach (array_reverse(CardinalNumeral::$words, true) as $word_number => $word) {
48
                if ($number >= $word_number) {
49
                    // if last part
50
                    if ($number % $word_number === 0)
51
                        $words[] = self::generate($word_number);
52
                    else
53
                        $words[] = CardinalNumeral::generate($word_number);
54
                    $number %= $word_number;
55
                }
56
            }
57
            $parts[] = implode('-', $words);
58
59
            if ($short) return $original_number.substr(array_pop($words), -2);
60
61
            return implode(' ', $parts);
62
        }
63
    }
64
}
65