Completed
Push — 2.1.x ( 7ab7e8 )
by f
23:20
created

Plurality::pluralize()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 21
nc 9
nop 2
dl 0
loc 26
rs 5.1612
c 0
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
class Plurality extends \morphos\Plurality {
5
6
	static private $exceptions = array(
7
		'chief' => 'chiefs',
8
		'basis' => 'bases',
9
		'crisis' => 'crises',
10
		'radius' => 'radii',
11
		'nucleus' => 'nuclei',
12
		'curriculum' => 'curricula',
13
		'man' => 'men',
14
		'woman' => 'women',
15
		'child' => 'children',
16
		'foot' => 'feet',
17
		'tooth' => 'teeth',
18
		'ox' => 'oxen',
19
		'goose' => 'geese',
20
		'mouse' => 'mice'
21
	);
22
23
	static private $without_paired_form = array(
24
		'knowledge',
25
		'progress',
26
		'advise',
27
		'ink',
28
		'money',
29
		'scissors',
30
		'spectacles',
31
		'trousers',
32
	);
33
34
	static public $consonants = array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w');
35
36
	public function pluralize($word, $count) {
37
		if ($count == 1)
38
			return $word;
39
40
		$word = lower($word);
41
		if (in_array($word, self::$without_paired_form))
42
			return $word;
43
		else if (isset(self::$exceptions[$word]))
44
			return self::$exceptions[$word];
45
46
		if (in_array(slice($word, -1), array('s', 'x')) || in_array(slice($word, -2), array('sh', 'ch'))) {
47
			return $word.'es';
48
		} else if (slice($word, -1) == 'o') {
49
			return $word.'es';
50
		} else if (slice($word, -1) == 'y' && in_array(slice($word, -2, -1), self::$consonants)) {
51
			return slice($word, 0, -1).'ies';
52
		} else if (slice($word, -2) == 'fe' || slice($word, -1) == 'f') {
53
			if (slice($word, -1) == 'f') {
54
				return slice($word, 0, -1).'ves';
55
			} else {
56
				return slice($word, 0, -2).'ves';
57
			}
58
		} else {
59
			return $word.'s';
60
		}
61
	}
62
}
63