Inflector   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 37.21 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 12
c 2
b 1
f 1
lcom 1
cbo 0
dl 48
loc 129
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B pluralise() 26 26 6
B singularise() 22 22 6

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
/*
3
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2013 Gamer Network Ltd.
6
 *
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-core
10
 */
11
12
namespace yolk\helpers;
13
14
/**
15
 *
16
 * Inflector implementation from: http://subosito.com/inflector-in-symfony-2
17
 */
18
class Inflector {
19
20
	protected static $plural = array(
21
		'/(quiz)$/i'               => "$1zes",
22
		'/^(ox)$/i'                => "$1en",
23
		'/([m|l])ouse$/i'          => "$1ice",
24
		'/(matr|vert|ind)ix|ex$/i' => "$1ices",
25
		'/(x|ch|ss|sh)$/i'         => "$1es",
26
		'/([^aeiouy]|qu)y$/i'      => "$1ies",
27
		'/(hive)$/i'               => "$1s",
28
		'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
29
		'/(shea|lea|loa|thie)f$/i' => "$1ves",
30
		'/sis$/i'                  => "ses",
31
		'/([ti])um$/i'             => "$1a",
32
		'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
33
		'/(bu)s$/i'                => "$1ses",
34
		'/(alias)$/i'              => "$1es",
35
		'/(ax|test)is$/i'          => "$1es",
36
		'/(us)$/i'                 => "$1es",
37
		'/s$/i'                    => "s",
38
		'/$/'                      => "s"
39
	);
40
41
	protected static $singular = array(
42
		'/(quiz)zes$/i'             => "$1",
43
		'/(matr)ices$/i'            => "$1ix",
44
		'/(vert|ind)ices$/i'        => "$1ex",
45
		'/^(ox)en$/i'               => "$1",
46
		'/(alias)es$/i'             => "$1",
47
		'/(cris|ax|test)es$/i'      => "$1is",
48
		'/(shoe)s$/i'               => "$1",
49
		'/(o)es$/i'                 => "$1",
50
		'/(bus)es$/i'               => "$1",
51
		'/([m|l])ice$/i'            => "$1ouse",
52
		'/(x|ch|ss|sh)es$/i'        => "$1",
53
		'/(m)ovies$/i'              => "$1ovie",
54
		'/(s)eries$/i'              => "$1eries",
55
		'/([^aeiouy]|qu)ies$/i'     => "$1y",
56
		'/([lr])ves$/i'             => "$1f",
57
		'/(tive)s$/i'               => "$1",
58
		'/(hive)s$/i'               => "$1",
59
		'/(li|wi|kni)ves$/i'        => "$1fe",
60
		'/(shea|loa|lea|thie)ves$/i'=> "$1f",
61
		'/(^analy)ses$/i'           => "$1sis",
62
		'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i'  => "$1$2sis",
63
		'/([ti])a$/i'               => "$1um",
64
		'/(n)ews$/i'                => "$1ews",
65
		'/(h|bl)ouses$/i'           => "$1ouse",
66
		'/(corpse)s$/i'             => "$1",
67
		'/(us)es$/i'                => "$1",
68
		'/s$/i'                     => ""
69
	);
70
71
	protected static $irregular = array(
72
		'move'   => 'moves',
73
		'foot'   => 'feet',
74
		'goose'  => 'geese',
75
		'sex'    => 'sexes',
76
		'child'  => 'children',
77
		'man'    => 'men',
78
		'tooth'  => 'teeth',
79
		'person' => 'people'
80
	);
81
82
	protected static $uncountable = array(
83
		'sheep',
84
		'fish',
85
		'deer',
86
		'series',
87
		'species',
88
		'money',
89
		'rice',
90
		'information',
91
		'equipment'
92
	);
93
94
	// TODO: cache results to speed up subsequent use?
95 View Code Duplication
	public static function pluralise( $string ) {
96
97
		// save some time in the case that singular and plural are the same
98
		if ( in_array( mb_strtolower( $string ), self::$uncountable ) )
99
			return $string;
100
101
102
		// check for irregular singular forms
103
		foreach ( self::$irregular as $pattern => $result )
104
		{
105
			$pattern = '/' . $pattern . '$/iu';
106
107
			if ( preg_match( $pattern, $string ) )
108
				return preg_replace( $pattern, $result, $string);
109
		}
110
111
		// check for matches using regular expressions
112
		foreach ( self::$plural as $pattern => $result )
113
		{
114
			if ( preg_match( $pattern, $string ) )
115
				return preg_replace( $pattern, $result, $string );
116
		}
117
118
		return $string;
119
120
	}
121
122
	// TODO: cache results to speed up subsequent use?
123 View Code Duplication
	public static function singularise( $string ) {
124
125
		// save some time in the case that singular and plural are the same
126
		if ( in_array(mb_strtolower($string), self::$uncountable) )
127
			return $string;
128
129
		// check for irregular plural forms
130
		foreach( self::$irregular as $result => $pattern ) {
131
			$pattern = '/' . $pattern . '$/iu';
132
			if( preg_match($pattern, $string) )
133
				return preg_replace($pattern, $result, $string);
134
		}
135
136
		// check for matches using regular expressions
137
		foreach( self::$singular as $pattern => $result ) {
138
			if( preg_match($pattern, $string) )
139
				return preg_replace($pattern, $result, $string);
140
		}
141
142
		return $string;
143
144
	}
145
146
}
147
148
// EOF