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

RulesetInflector::addRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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