These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace morphos\English; |
||
3 | |||
4 | use morphos\NumeralGenerator; |
||
5 | |||
6 | class OrdinalNumeralGenerator extends NumeralGenerator |
||
7 | { |
||
8 | protected static $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 | public static function getCases($number) |
||
19 | { |
||
20 | } |
||
21 | public static function getCase($number, $case) |
||
22 | { |
||
23 | } |
||
24 | |||
25 | public static function generate($number, $short = false) |
||
26 | { |
||
27 | // simple numeral |
||
28 | if (isset(self::$words[$number])) { |
||
29 | return !$short ? self::$words[$number] : $number.substr(self::$words[$number], -2); |
||
30 | } elseif (isset(CardinalNumeralGenerator::$words[$number]) || isset(CardinalNumeralGenerator::$exponents[$number])) { |
||
31 | if ($short) { |
||
32 | return $number.'th'; |
||
33 | } |
||
34 | $word = isset(CardinalNumeralGenerator::$words[$number]) ? CardinalNumeralGenerator::$words[$number] : CardinalNumeralGenerator::$exponents[$number]; |
||
35 | if (substr($word, -1) == 'y') { |
||
36 | $word = substr($word, 0, -1).'ie'; |
||
37 | } |
||
38 | $word .= 'th'; |
||
39 | return $word; |
||
40 | } |
||
41 | // compound numeral |
||
42 | else { |
||
43 | $parts = array(); |
||
44 | $words = array(); |
||
45 | |||
46 | $original_number = $number; |
||
47 | |||
48 | View Code Duplication | foreach (array_reverse(CardinalNumeralGenerator::$exponents, true) as $word_number => $word) { |
|
0 ignored issues
–
show
|
|||
49 | if ($number >= $word_number) { |
||
50 | $count = floor($number / $word_number); |
||
51 | $number = $number % ($count * $word_number); |
||
52 | $parts[] = CardinalNumeralGenerator::generate($count).' '.CardinalNumeralGenerator::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | foreach (array_reverse(CardinalNumeralGenerator::$words, true) as $word_number => $word) { |
||
57 | if ($number >= $word_number) { |
||
58 | // if last part |
||
59 | if ($number % $word_number === 0) { |
||
60 | $words[] = self::generate($word_number); |
||
61 | } else { |
||
62 | $words[] = CardinalNumeralGenerator::generate($word_number); |
||
63 | } |
||
64 | $number %= $word_number; |
||
65 | } |
||
66 | } |
||
67 | $parts[] = implode('-', $words); |
||
68 | |||
69 | if ($short) { |
||
70 | return $original_number.substr(array_pop($words), -2); |
||
71 | } |
||
72 | |||
73 | return implode(' ', $parts); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 |
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.