Failed Conditions
Pull Request — master (#90)
by Jonathan
02:14
created

Pluralizer::inflect()   D

Complexity

Conditions 10
Paths 41

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 21
nc 41
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector;
6
7
use Doctrine\Inflector\Rules\Plural;
8
use Doctrine\Inflector\Rules\Uninflected;
9
use function array_keys;
10
use function array_merge;
11
use function implode;
12
use function preg_match;
13
use function preg_replace;
14
use function strtolower;
15
use function substr;
16
17
class Pluralizer extends InflectorService
18
{
19
    /** @var Uninflected */
20
    private $uninflected;
21
22
    public function __construct(Uninflected $uninflected)
23
    {
24
        $this->uninflected = $uninflected;
25
26
        parent::__construct();
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getRules() : array
33
    {
34
        return Plural::RULES;
35
    }
36
37
    /**
38
     * Returns a word in plural form.
39
     *
40
     * @param string $word The word in singular form.
41
     *
42
     * @return string The word in plural form.
43
     */
44
    public function inflect(string $word) : string
45
    {
46
        if (isset($this->cache['pluralize'][$word])) {
47
            return $this->cache['pluralize'][$word];
48
        }
49
50
        if (! isset($this->rules['merged']['irregular'])) {
51
            $this->rules['merged']['irregular'] = $this->rules['irregular'];
52
        }
53
54
        if (! isset($this->rules['merged']['uninflected'])) {
55
            $this->rules['merged']['uninflected'] = array_merge(
56
                $this->rules['uninflected'],
57
                $this->uninflected->getUninflected()
58
            );
59
        }
60
61
        if (! isset($this->rules['cacheUninflected']) || ! isset($this->rules['cacheIrregular'])) {
62
            $this->rules['cacheUninflected'] = '(?:' . implode('|', $this->rules['merged']['uninflected']) . ')';
63
            $this->rules['cacheIrregular']   = '(?:' . implode('|', array_keys($this->rules['merged']['irregular'])) . ')';
64
        }
65
66
        if (preg_match('/(.*)\\b(' . $this->rules['cacheIrregular'] . ')$/i', $word, $regs)) {
67
            $this->cache['pluralize'][$word] = $regs[1] . $word[0] . substr($this->rules['merged']['irregular'][strtolower($regs[2])], 1);
68
69
            return $this->cache['pluralize'][$word];
70
        }
71
72
        if (preg_match('/^(' . $this->rules['cacheUninflected'] . ')$/i', $word, $regs)) {
73
            $this->cache['pluralize'][$word] = $word;
74
75
            return $word;
76
        }
77
78
        foreach ($this->rules['rules'] as $rule => $replacement) {
79
            if (preg_match($rule, $word)) {
80
                $this->cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
81
82
                return $this->cache['pluralize'][$word];
83
            }
84
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
85
    }
86
}
87