Failed Conditions
Pull Request — master (#90)
by Jonathan
02:14
created

Singularizer::inflect()   C

Complexity

Conditions 10
Paths 41

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 5.3454
c 0
b 0
f 0
cc 10
eloc 25
nc 41
nop 1

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
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector;
6
7
use Doctrine\Inflector\Rules\Singular;
8
use Doctrine\Inflector\Rules\Uninflected;
9
use function array_flip;
10
use function array_keys;
11
use function array_merge;
12
use function implode;
13
use function preg_match;
14
use function preg_replace;
15
use function strtolower;
16
use function substr;
17
18
class Singularizer extends InflectorService
19
{
20
    /** @var Uninflected */
21
    private $uninflected;
22
23
    /** @var Pluralizer */
24
    private $pluralizer;
25
26
    public function __construct(Uninflected $uninflected, Pluralizer $pluralizer)
27
    {
28
        $this->uninflected = $uninflected;
29
        $this->pluralizer  = $pluralizer;
30
31
        parent::__construct();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getRules() : array
38
    {
39
        return Singular::RULES;
40
    }
41
42
    /**
43
     * Returns a word in singular form.
44
     *
45
     * @param string $word The word in plural form.
46
     *
47
     * @return string The word in singular form.
48
     */
49
    public function inflect(string $word) : string
50
    {
51
        if (isset($this->cache['singularize'][$word])) {
52
            return $this->cache['singularize'][$word];
53
        }
54
55
        if (! isset($this->rules['merged']['uninflected'])) {
56
            $this->rules['merged']['uninflected'] = array_merge(
57
                $this->rules['uninflected'],
58
                $this->uninflected->getUninflected()
59
            );
60
        }
61
62
        if (! isset($this->rules['merged']['irregular'])) {
63
            $this->rules['merged']['irregular'] = array_merge(
64
                $this->rules['irregular'],
65
                array_flip($this->pluralizer->getRules()['irregular'])
66
            );
67
        }
68
69
        if (! isset($this->rules['cacheUninflected']) || ! isset($this->rules['cacheIrregular'])) {
70
            $this->rules['cacheUninflected'] = '(?:' . implode('|', $this->rules['merged']['uninflected']) . ')';
71
            $this->rules['cacheIrregular']   = '(?:' . implode('|', array_keys($this->rules['merged']['irregular'])) . ')';
72
        }
73
74
        if (preg_match('/(.*)\\b(' . $this->rules['cacheIrregular'] . ')$/i', $word, $regs)) {
75
            $this->cache['singularize'][$word] = $regs[1] . $word[0] . substr($this->rules['merged']['irregular'][strtolower($regs[2])], 1);
76
77
            return $this->cache['singularize'][$word];
78
        }
79
80
        if (preg_match('/^(' . $this->rules['cacheUninflected'] . ')$/i', $word, $regs)) {
81
            $this->cache['singularize'][$word] = $word;
82
83
            return $word;
84
        }
85
86
        foreach ($this->rules['rules'] as $rule => $replacement) {
87
            if (preg_match($rule, $word)) {
88
                $this->cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
89
90
                return $this->cache['singularize'][$word];
91
            }
92
        }
93
94
        $this->cache['singularize'][$word] = $word;
95
96
        return $word;
97
    }
98
}
99