NounPluralization::getCases()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace morphos\English;
3
4
use morphos\S;
5
use RuntimeException;
6
7
class NounPluralization extends \morphos\BasePluralization
8
{
9
    /**
10
     * @var string[]
11
     * @phpstan-var array<string, string>
12
     */
13
    private static $exceptions = [
14
        'chief' => 'chiefs',
15
        'basis' => 'bases',
16
        'crisis' => 'crises',
17
        'radius' => 'radii',
18
        'nucleus' => 'nuclei',
19
        'curriculum' => 'curricula',
20
        'man' => 'men',
21
        'woman' => 'women',
22
        'child' => 'children',
23
        'foot' => 'feet',
24
        'tooth' => 'teeth',
25
        'ox' => 'oxen',
26
        'goose' => 'geese',
27
        'mouse' => 'mice'
28
    ];
29
30
    /** @var string[]  */
31
    private static $without_paired_form = [
32
        'knowledge',
33
        'progress',
34
        'advise',
35
        'ink',
36
        'money',
37
        'scissors',
38
        'spectacles',
39
        'trousers',
40
    ];
41
42
    /** @var string[]  */
43
    public static $consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w'];
44
45
    /**
46
     * @param string $word
47
     * @param int $count
48
     * @return string
49
     */
50 50
    public static function pluralize($word, $count = 2)
51
    {
52 50
        if ($count == 1) {
53 9
            return $word;
54
        }
55
56 47
        $word = S::lower($word);
57 47
        if (in_array($word, static::$without_paired_form)) {
0 ignored issues
show
Bug introduced by
Since $without_paired_form is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $without_paired_form to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
58 8
            return $word;
59 39
        } elseif (isset(static::$exceptions[$word])) {
0 ignored issues
show
Bug introduced by
Since $exceptions is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $exceptions to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
60 14
            return static::$exceptions[$word];
0 ignored issues
show
Bug introduced by
Since $exceptions is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $exceptions to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
61
        }
62
63 25
        if (in_array(S::slice($word, -1), ['s', 'x']) || in_array(S::slice($word, -2), array('sh', 'ch'))) {
64 2
            return $word.'es';
65 23
        } elseif (S::slice($word, -1) == 'o') {
66 1
            return $word.'es';
67 22
        } elseif (S::slice($word, -1) == 'y' && in_array(S::slice($word, -2, -1), static::$consonants)) {
68 2
            return S::slice($word, 0, -1).'ies';
69 20
        } elseif (S::slice($word, -2) == 'fe' || S::slice($word, -1) == 'f') {
70 2
            if (S::slice($word, -1) == 'f') {
71 1
                return S::slice($word, 0, -1).'ves';
72
            } else {
73 1
                return S::slice($word, 0, -2).'ves';
74
            }
75
        } else {
76 18
            return $word.'s';
77
        }
78
    }
79
80
    public static function getCase($word, $case, $animateness = false)
81
    {
82
        throw new RuntimeException('Not implemented');
83
    }
84
85
    public static function getCases($word, $animateness = false)
86
    {
87
        throw new RuntimeException('Not implemented');
88
    }
89
}
90