Passed
Pull Request — master (#139)
by Andreas
02:02
created

RulesetInflector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 16
c 1
b 0
f 0
dl 0
loc 39
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B inflect() 0 29 8
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
use function array_merge;
9
10
/**
11
 * Inflects based on multiple rulesets.
12
 *
13
 * Rules:
14
 * - If the word matches any uninflected word pattern, it is not inflected
15
 * - The first ruleset that returns a different value for an irregular word wins
16
 * - The first ruleset that returns a different value for a regular word wins
17
 * - If none of the above match, the word is left as-is
18
 */
19
class RulesetInflector implements WordInflector
20
{
21
    /** @var Ruleset[] */
22
    private $rulesets;
23
24 1109
    public function __construct(Ruleset $ruleset, Ruleset ...$rulesets)
25
    {
26 1109
        $this->rulesets = array_merge([$ruleset], $rulesets);
27 1109
    }
28
29 1570
    public function inflect(string $word) : string
30
    {
31 1570
        if ($word === '') {
32 4
            return '';
33
        }
34
35 1566
        foreach ($this->rulesets as $ruleset) {
36 1566
            if ($ruleset->getUninflected()->matches($word)) {
37 626
                return $word;
38
            }
39
        }
40
41 943
        foreach ($this->rulesets as $ruleset) {
42 943
            $inflected = $ruleset->getIrregular()->inflect($word);
43
44 943
            if ($inflected !== $word) {
45 299
                return $inflected;
46
            }
47
        }
48
49 647
        foreach ($this->rulesets as $ruleset) {
50 647
            $inflected = $ruleset->getRegular()->inflect($word);
51
52 647
            if ($inflected !== $word) {
53 634
                return $inflected;
54
            }
55
        }
56
57 13
        return $word;
58
    }
59
}
60