Passed
Push — master ( eae2ea...251e76 )
by
unknown
01:43
created

RuleMapping::getList()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 101
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 74
nc 1
nop 0
dl 0
loc 101
ccs 25
cts 25
cp 1
crap 1
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

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
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 2/15/18
6
 * Time: 4:51 PM
7
 */
8
9
namespace PluginSimpleValidate;
10
11
use PluginSimpleValidate\Exception\RuleNotExist;
12
13
class RuleMapping extends \PluginSimpleValidate\BaseAbstract\RuleMapping
14
{
15
    /**
16
     * @var array
17
     */
18
    protected static $list;
19
20
    /**
21
     * @return array
22
     */
23 1
    protected static function getList()
24
    {
25
        return [
26 1
            static::VALIDATE_IS_TRUE => [
27
                'validation_method' => 'is_true',
28
                'lang_key' => 'is_true'
29
            ],
30 1
            static::VALIDATE_NUMBER => [
31
                'validation_method' => 'is_number',
32
                'lang_key' => 'numeric'
33
            ],
34 1
            static::VALIDATE_REQUIRED => [
35
                'validation_method' => 'is_required',
36
                'lang_key' => 'required'
37
            ],
38 1
            static::VALIDATE_EMAIL => [
39
                'validation_method' => 'is_valid_email',
40
                'lang_key' => 'email'
41
            ],
42 1
            static::VALIDATE_ALPHA => [
43
                'validation_method' => 'is_alpha',
44
                'lang_key' => 'alpha'
45
            ],
46 1
            static::VALIDATE_ALPHA_OR_NUMERIC => [
47
                'validation_method' => 'is_alpha_or_numeric',
48
                'lang_key' => 'alpha_or_numeric'
49
            ],
50 1
            static::VALIDATE_DECIMAL => [
51
                'validation_method' => 'is_decimal',
52
                'lang_key' => 'decimal'
53
            ],
54 1
            static::VALIDATE_INTEGER => [
55
                'validation_method' => 'is_integer',
56
                'lang_key' => 'integer'
57
            ],
58 1
            static::VALIDATE_NATURAL_NUMBER => [
59
                'validation_method' => 'is_natural',
60
                'lang_key' => 'natural'
61
            ],
62 1
            static::VALIDATE_NATURAL_NO_ZERO_NUMBER => [
63
                'validation_method' => 'is_natural_no_zero',
64
                'lang_key' => 'natural_no_zero'
65
            ],
66 1
            static::VALIDATE_EQUAL => [
67
                'validation_method' => 'is_equal',
68
                'lang_key' => 'equal'
69
            ],
70 1
            static::VALIDATE_LESS_THAN => [
71
                'validation_method' => 'less_than',
72
                'lang_key' => 'less_than'
73
            ],
74 1
            static::VALIDATE_GREATER_THAN => [
75
                'validation_method' => 'greater_than',
76
                'lang_key' => 'greater_than'
77
            ],
78 1
            static::VALIDATE_LESS_OR_EQUAL_THAN => [
79
                'validation_method' => 'less_or_equal_than',
80
                'lang_key' => 'less_or_equal_than'
81
            ],
82 1
            static::VALIDATE_GREATER_OR_EQUAL_THAN => [
83
                'validation_method' => 'greater_or_equal_than',
84
                'lang_key' => 'greater_or_equal_than'
85
            ],
86 1
            static::VALIDATE_BETWEEN => [
87
                'validation_method' => 'between',
88
                'lang_key' => 'between'
89
            ],
90 1
            static::VALIDATE_BETWEEN_OR_EQUAL_THAN => [
91
                'validation_method' => 'between_or_equal',
92
                'lang_key' => 'between_or_equal'
93
            ],
94 1
            static::VALIDATE_LENGTH => [
95
                'validation_method' => 'length',
96
                'lang_key' => 'length'
97
            ],
98 1
            static::VALIDATE_LENGTH_LESS_THAN => [
99
                'validation_method' => 'length_less_than',
100
                'lang_key' => 'length_less_than'
101
            ],
102 1
            static::VALIDATE_LENGTH_GREATER_THAN => [
103
                'validation_method' => 'length_greater_than',
104
                'lang_key' => 'length_greater_than'
105
            ],
106 1
            static::VALIDATE_LENGTH_LESS_OR_EQUAL_THAN => [
107
                'validation_method' => 'length_less_or_equal_than',
108
                'lang_key' => 'length_less_or_equal_than'
109
            ],
110 1
            static::VALIDATE_LENGTH_GREATER_OR_EQUAL_THAN => [
111
                'validation_method' => 'length_greater_or_equal_than',
112
                'lang_key' => 'length_greater_or_equal_than'
113
            ],
114 1
            static::VALIDATE_LENGTH_BETWEEN => [
115
                'validation_method' => 'length_between',
116
                'lang_key' => 'length_between'
117
            ],
118 1
            static::VALIDATE_LENGTH_BETWEEN_OR_EQUAL_THAN => [
119
                'validation_method' => 'length_between_or_equal',
120
                'lang_key' => 'length_between_or_equal'
121
            ],
122
        ];
123
    }
124
125
    /**
126
     * @param string $key
127
     * @param array $args
128
     * @return Contracts\Rule
129
     * @throws RuleNotExist
130
     */
131 8
    public static function getRule(string $key, array $args = []) : \PluginSimpleValidate\Contracts\Rule
132
    {
133 8
        if (is_null(static::$list)) {
134 1
            static::$list = static::getList();
135
        }
136
137 8
        if (!isset(static::$list[$key])) {
138
            throw new RuleNotExist('Rule does not exist');
139
        }
140
141 8
        return new Rule(
142 8
            static::$list[$key]['validation_method'],
143 8
            static::$list[$key]['lang_key'],
144 8
            $args
145
        );
146
    }
147
}