Pluralize   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A singular() 0 18 6
A plural() 0 19 6
1
<?php
2
3
namespace Erykai\Pluralize;
4
5
/**
6
 * Converts plural words to singular and singular to plural
7
 */
8
class Pluralize extends Resource
9
{
10
    /**
11
     * @param string $word
12
     * @return string
13
     */
14
    public function singular(string $word): string
15
    {
16
        if ($this->uncountable($word)) {
17
            return $word;
18
        }
19
        foreach ($this->getIrregular() as $result => $pattern) {
20
            $pattern = '/' . $pattern . '$/i';
21
22
            if (preg_match($pattern, $word)) {
23
                return preg_replace($pattern, $result, $word);
24
            }
25
        }
26
        foreach ($this->getSingular() as $pattern => $result) {
27
            if (preg_match($pattern, $word)) {
28
                return preg_replace($pattern, $result, $word);
29
            }
30
        }
31
        return $word;
32
    }
33
34
    /**
35
     * @param string $word
36
     * @return string
37
     */
38
    public function plural(string $word): string
39
    {
40
        if ($this->uncountable($word)) {
41
            return $word;
42
        }
43
        foreach ($this->getIrregular() as $pattern => $result) {
44
            $pattern = '/' . $pattern . '$/i';
45
46
            if (preg_match($pattern, $word)) {
47
                return preg_replace($pattern, $result, $word);
48
            }
49
        }
50
        foreach ($this->getPlural() as $pattern => $result) {
51
            if (preg_match($pattern, $word)) {
52
                return preg_replace($pattern, $result, $word);
53
            }
54
        }
55
56
        return $word;
57
    }
58
}