Completed
Push — test ( 99e4ec...dc13de )
by Temitope
02:33
created

Inflector::singularize()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 57
Code Lines 48

Duplication

Lines 10
Ratio 17.54 %
Metric Value
dl 10
loc 57
rs 7.6759
cc 7
eloc 48
nc 15
nop 1

How to fix   Long Method   

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
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
// +----------------------------------------------------------------------+
5
// | Akelos PHP Application Framework                                     |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 2002-2006, Akelos Media, S.L.  http://www.akelos.com/  |
8
// | Released under the GNU Lesser General Public License                 |
9
// +----------------------------------------------------------------------+
10
// | You should have received the following files along with this library |
11
// | - COPYRIGHT (Additional copyright notice)                            |
12
// | - DISCLAIMER (Disclaimer of warranty)                                |
13
// | - README (Important information regarding this library)              |
14
// +----------------------------------------------------------------------+
15
/**
16
 * Inflector for pluralize and singularize English nouns.
17
 *
18
 * This Inflector is a port of Ruby on Rails Inflector.
19
 *
20
 * It can be really helpful for developers that want to
21
 * create frameworks based on naming conventions rather than
22
 * configurations.
23
 *
24
 * It was ported to PHP for the Akelos Framework, a
25
 * multilingual Ruby on Rails like framework for PHP that will
26
 * be launched soon.
27
 *
28
 * @author Bermi Ferrer Martinez
29
 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
30
 * @license GNU Lesser General Public License
31
 * @since 0.1
32
 * @version $Revision 0.1 $
33
 */
34
namespace Laztopaz\potatoORM;
35
36
trait Inflector
37
{
38
	/**
39
	 * Pluralizes English nouns.
40
	 *
41
	 * @access public
42
	 * @static
43
	 * @param    string    $word    English noun to pluralize
44
	 * @return string Plural noun
45
	 */
46
	public static function pluralize($word)
47
	{
48
		$plural = array(
49
			'/(quiz)$/i'               => "$1zes",
50
			'/^(ox)$/i'                => "$1en",
51
			'/([m|l])ouse$/i'          => "$1ice",
52
			'/(matr|vert|ind)ix|ex$/i' => "$1ices",
53
			'/(x|ch|ss|sh)$/i'         => "$1es",
54
			'/([^aeiouy]|qu)y$/i'      => "$1ies",
55
			'/(hive)$/i'               => "$1s",
56
			'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
57
			'/(shea|lea|loa|thie)f$/i' => "$1ves",
58
			'/sis$/i'                  => "ses",
59
			'/([ti])um$/i'             => "$1a",
60
			'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
61
			'/(bu)s$/i'                => "$1ses",
62
			'/(alias)$/i'              => "$1es",
63
			'/(octop)us$/i'            => "$1i",
64
			'/(ax|test)is$/i'          => "$1es",
65
			'/(us)$/i'                 => "$1es",
66
			'/s$/i'                    => "s",
67
			'/$/'                      => "s"
68
		);
69
		$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
70
		$irregular = array(
71
			'person' => 'people',
72
			'man' => 'men',
73
			'child' => 'children',
74
			'sex' => 'sexes',
75
			'move' => 'moves');
76
		$lowercased_word = strtolower($word);
77
		foreach ($uncountable as $_uncountable){
78
			if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
79
				return $word;
80
			}
81
		}
82
		foreach ($irregular as $_plural=> $_singular){
83
			if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
84
				return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
85
			}
86
		}
87
		foreach ($plural as $rule => $replacement) {
88
			if (preg_match($rule, $word)) {
89
				return preg_replace($rule, $replacement, $word);
90
			}
91
		}
92
		return false;
93
	}
94
	
95
}