Failed Conditions
Push — master ( 6c9efc...83ff25 )
by Andreas
02:46 queued 12s
created

RulesetInflector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A inflect() 0 25 6
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 1105
    public function __construct(Ruleset $ruleset, Ruleset ...$rulesets)
25
    {
26 1105
        $this->rulesets = array_merge([$ruleset], $rulesets);
27 1105
    }
28
29 1089
    public function inflect(string $word) : string
30
    {
31 1089
        if ($word === '') {
32 2
            return '';
33
        }
34
35 1087
        foreach ($this->rulesets as $ruleset) {
36 1087
            if ($ruleset->getUninflected()->matches($word)) {
37 401
                return $word;
38
            }
39
40 686
            $inflected = $ruleset->getIrregular()->inflect($word);
41
42 686
            if ($inflected !== $word) {
43 195
                return $inflected;
44
            }
45
46 492
            $inflected = $ruleset->getRegular()->inflect($word);
47
48 492
            if ($inflected !== $word) {
49 478
                return $inflected;
50
            }
51
        }
52
53 13
        return $word;
54
    }
55
}
56