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

RulesetInflector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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