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

Ruleset::inflect()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 5
rs 8.7624
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector\Rules;
6
7
use function array_keys;
8
use function implode;
9
use function preg_match;
10
use function preg_replace;
11
use function strtolower;
12
use function substr;
13
14
class Ruleset
15
{
16
    /** @var string[] */
17
    private $rules = [];
18
19
    /** @var string[] */
20
    private $uninflected = [];
21
22
    /** @var string[] */
23
    private $irregular = [];
24
25
    /**
26
     * @param string[] $rules
27
     * @param string[] $uninflected
28
     * @param string[] $irregular
29
     */
30 526
    public function __construct(array $rules, array $uninflected, array $irregular)
31
    {
32 526
        $this->rules = $rules;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33 526
        $this->uninflected = $uninflected;
34 526
        $this->irregular = $irregular;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35 526
    }
36
37
    /**
38
     * @return string[]
39
     */
40
    public function getRules() : array
41
    {
42
        return $this->rules;
43
    }
44
45
    /**
46
     * @return string[]
47
     */
48
    public function getUninflected() : array
49
    {
50
        return $this->uninflected;
51
    }
52
53
    /**
54
     * @return string[]
55
     */
56
    public function getIrregular() : array
57
    {
58
        return $this->irregular;
59
    }
60
61
    /**
62
     * Adds custom inflection rules.
63
     *
64
     * $singularRuleset->addRules(['/^(inflect)or$/i' => '\1ables']);
65
     * $pluralRuleset->addRules([
66
     *     'rules' => ['/^(inflect)ors$/i' => '\1ables'],
67
     *     'uninflected' => ['dontinflectme'],
68
     *     'irregular' => ['red' => 'redlings']
69
     * ]);
70
     *
71
     * @param mixed[] $rules An array of rules to be added. new rules that are being defined in $rules.
72
     * @param bool    $reset If true, will unset default inflections for all new rules that are being defined in $rules.
73
     */
74 4
    public function addRules(array $rules, bool $reset = false) : void
0 ignored issues
show
Unused Code introduced by
The parameter $reset is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public function addRules(array $rules, /** @scrutinizer ignore-unused */ bool $reset = false) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76 4
        if ($this->isFullSyntax($rules)) {
77 4
            $this->rules = $rules['rules'] ?? $this->rules;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
78 4
            $this->uninflected = $rules['uninflected'] ?? $this->uninflected;
79 4
            $this->irregular = $rules['irregular'] ?? $this->irregular;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80
        } else {
81 2
            $this->rules = $rules;
82
        }
83 4
    }
84
85 510
    public function inflect(string $word) : string
86
    {
87 510
        $irregularRegex = '(?:' . implode('|', array_keys($this->irregular)) . ')';
88
89 510
        if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) {
90 93
            return $regs[1] . $word[0] . substr($this->irregular[strtolower($regs[2])], 1);
91
        }
92
93 420
        $uninflectedRegex = '(?:' . implode('|', $this->uninflected) . ')';
94
95 420
        if (preg_match('/^(' . $uninflectedRegex . ')$/i', $word, $regs)) {
96 244
            return $word;
97
        }
98
99 179
        foreach ($this->rules as $rule => $replacement) {
100 179
            if (preg_match($rule, $word)) {
101 179
                return preg_replace($rule, $replacement, $word);
102
            }
103
        }
104
105 2
        return $word;
106
    }
107
108
    /**
109
     * @param mixed[] $rules
110
     */
111 4
    private function isFullSyntax(array $rules) : bool
112
    {
113 4
        return isset($rules['rules']) || isset($rules['irregular']) || isset($rules['uninflected']);
114
    }
115
}
116