Completed
Pull Request — master (#90)
by Jonathan
02:59
created

RulesetInflector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inflect() 0 7 2
A addRules() 0 5 1
A doInflect() 0 13 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector;
6
7
use Doctrine\Inflector\Rules\Ruleset;
8
9
class RulesetInflector
10
{
11
    /** @var Ruleset */
12
    private $ruleset;
13
14
    /** @var string[] */
15
    private $wordInflectionCache = [];
16
17 526
    public function __construct(Ruleset $ruleset)
18
    {
19 526
        $this->ruleset = $ruleset;
20 526
    }
21
22
    /**
23
     * @param mixed[] $rules
24
     */
25 4
    public function addRules(array $rules) : void
26
    {
27 4
        $this->ruleset->addRules($rules);
28
29 4
        $this->wordInflectionCache = [];
30 4
    }
31
32 510
    public function inflect(string $word) : string
33
    {
34 510
        if (! isset($this->wordInflectionCache[$word])) {
35 510
            $this->wordInflectionCache[$word] = $this->doInflect($word);
36
        }
37
38 510
        return $this->wordInflectionCache[$word];
39
    }
40
41 510
    private function doInflect(string $word) : string
42
    {
43 510
        $inflected = $this->ruleset->getIrregular()->inflect($word);
44
45 510
        if ($inflected !== null) {
46 93
            return $inflected;
47
        }
48
49 420
        if ($this->ruleset->getUninflected()->isUninflected($word)) {
50 244
            return $word;
51
        }
52
53 179
        return $this->ruleset->getRules()->inflect($word);
54
    }
55
}
56