Passed
Pull Request — master (#90)
by Jonathan
02:20
created

RulesetInflector::doInflect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
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 527
    public function __construct(Ruleset $ruleset)
18
    {
19 527
        $this->ruleset = $ruleset;
20 527
    }
21
22 511
    public function inflect(string $word) : string
23
    {
24 511
        if (! isset($this->wordInflectionCache[$word])) {
25 511
            $this->wordInflectionCache[$word] = $this->doInflect($word);
26
        }
27
28 511
        return $this->wordInflectionCache[$word];
29
    }
30
31 511
    private function doInflect(string $word) : string
32
    {
33 511
        $inflected = $this->ruleset->getIrregular()->inflect($word);
34
35 511
        if ($inflected !== null) {
36 92
            return $inflected;
37
        }
38
39 421
        if ($this->ruleset->getUninflected()->isUninflected($word)) {
40 244
            return $word;
41
        }
42
43 179
        return $this->ruleset->getRules()->inflect($word);
44
    }
45
}
46