Completed
Push — master ( d53204...9a9f15 )
by f
01:39
created

CasesHelper::canonizeCase()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 18.0184

Importance

Changes 0
Metric Value
cc 18
nc 18
nop 1
dl 0
loc 37
ccs 25
cts 26
cp 0.9615
crap 18.0184
rs 4.8666
c 0
b 0
f 0

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
namespace morphos;
3
4
use InvalidArgumentException;
5
6
trait CasesHelper
7
{
8
    /**
9
     * @param $case
10
     * @return string
11
     * @throws InvalidArgumentException If passed case is invalid.
12
     */
13 6
    public static function canonizeCase($case)
14
    {
15 6
        $case = S::lower($case);
16
        switch ($case) {
17 6
            case Cases::NOMINATIVE:
18 6
            case 'nominativus':
19 5
            case 'n':
20 1
                return Cases::NOMINATIVE;
21
22 5
            case Cases::GENITIVE:
23 5
            case Cases::GENETIVE:
24 5
            case 'genetivus':
25 4
            case 'g':
26 1
                return Cases::GENITIVE;
27
28 4
            case Cases::DATIVE:
29 4
            case 'dativus':
30 3
            case 'd':
31 1
                return Cases::DATIVE;
32
33 3
            case Cases::ACCUSATIVE:
34
                return Cases::ACCUSATIVE;
35
36 3
            case Cases::ABLATIVE:
37 3
            case 'ablativus':
38 2
            case 'a':
39 1
                return Cases::ABLATIVE;
40
41 2
            case Cases::PREPOSITIONAL:
42 2
            case 'praepositionalis':
43 1
            case 'p':
44 1
                return Cases::PREPOSITIONAL;
45
46
            default:
47 1
                throw new InvalidArgumentException('Invalid case: '.$case);
48
        }
49
    }
50
51
    /**
52
     * @return array
53
     */
54 25
    public static function getAllCases()
55
    {
56
        return [
57 25
            Cases::NOMINATIVE,
58
            Cases::GENITIVE,
59
            Cases::GENETIVE,
60
            Cases::DATIVE,
61
            Cases::ACCUSATIVE,
62
            Cases::ABLATIVE,
63
            Cases::PREPOSITIONAL,
64
        ];
65
    }
66
67
    /**
68
     * Составляет один массив с падежами из нескольких массивов падежей разных слов
69
     * @param array $words Двумерный массив слов и их падежей
70
     * @param string $delimiter Разделитель между падежами слов
71
     * @return array Одномерный массив падежей
72
     */
73 25
    public static function composeCasesFromWords(array $words, $delimiter = ' ') {
74 25
        $cases = [];
75 25
        $all_cases = CasesHelper::getAllCases();
76 25
        if (count($words[0]) === 7) $all_cases[] = \morphos\Russian\Cases::LOCATIVE;
77 25
        foreach ($all_cases as $case) {
78 25
            $composed_case = [];
79 25
            foreach ($words as $wordCases) {
80 25
                $composed_case[] = $wordCases[$case];
81
            }
82 25
            $cases[$case] = implode($delimiter, $composed_case);
83
        }
84 25
        return $cases;
85
    }
86
}
87