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

RulesetInflector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A inflect() 0 7 2
A doInflect() 0 13 3
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