Passed
Pull Request — master (#97)
by Jonathan
02:24
created

Substitutions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getFlippedSubstitutions() 0 12 2
A inflect() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector\Rules;
6
7
use Doctrine\Inflector\WordInflector;
8
use function mb_substr;
9
use function strtolower;
10
11
class Substitutions implements WordInflector
12
{
13
    /** @var Substitution[] */
14
    private $substitutions;
15
16 524
    public function __construct(Substitution ...$substitutions)
17
    {
18 524
        foreach ($substitutions as $substitution) {
19 524
            $this->substitutions[$substitution->getFrom()->getWord()] = $substitution;
20
        }
21 524
    }
22
23 523
    public function getFlippedSubstitutions() : Substitutions
24
    {
25 523
        $substitutions = [];
26
27 523
        foreach ($this->substitutions as $substitution) {
28 523
            $substitutions[] = new Substitution(
29 523
                $substitution->getTo(),
30 523
                $substitution->getFrom()
31
            );
32
        }
33
34 523
        return new Substitutions(...$substitutions);
35
    }
36
37
38 265
    public function inflect(string $word) : string
39
    {
40 265
        $lowerWord = strtolower($word);
41
42 265
        if (isset($this->substitutions[$lowerWord])) {
43 104
            return mb_substr($word, 0, 1) . mb_substr($this->substitutions[$lowerWord]->getTo()->getWord(), 1);
44
        }
45
46 161
        return $word;
47
    }
48
}
49