Completed
Push — develop ( c2d572...f283ed )
by Neomerx
03:27 queued 01:55
created

Comparisons   D

Complexity

Total Complexity 35

Size/Duplication

Total Lines 184
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 0
Dependencies 23

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 35
lcom 0
cbo 23
dl 24
loc 184
rs 4.95
c 0
b 0
f 0
ccs 57
cts 57
cp 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A equals() 12 12 4
A notEquals() 12 12 4
A inValues() 0 4 2
A lessThan() 0 6 3
A lessOrEquals() 0 6 3
A moreThan() 0 6 3
A moreOrEquals() 0 7 3
A between() 0 9 4
A stringLengthBetween() 0 6 2
A stringLengthMin() 0 6 2
A stringLengthMax() 0 6 2
A regexp() 0 6 2
A nullable() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Limoncello\Validation\Validator;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use DateTimeInterface;
20
use Limoncello\Validation\Contracts\Rules\RuleInterface;
21
use Limoncello\Validation\Rules\Comparisons\DateTimeBetween;
22
use Limoncello\Validation\Rules\Comparisons\DateTimeEquals;
23
use Limoncello\Validation\Rules\Comparisons\DateTimeLessOrEquals;
24
use Limoncello\Validation\Rules\Comparisons\DateTimeLessThan;
25
use Limoncello\Validation\Rules\Comparisons\DateTimeMoreOrEquals;
26
use Limoncello\Validation\Rules\Comparisons\DateTimeMoreThan;
27
use Limoncello\Validation\Rules\Comparisons\DateTimeNotEquals;
28
use Limoncello\Validation\Rules\Comparisons\IsNotNull;
29
use Limoncello\Validation\Rules\Comparisons\IsNull;
30
use Limoncello\Validation\Rules\Comparisons\NumericBetween;
31
use Limoncello\Validation\Rules\Comparisons\NumericLessOrEquals;
32
use Limoncello\Validation\Rules\Comparisons\NumericLessThan;
33
use Limoncello\Validation\Rules\Comparisons\NumericMoreOrEqualsThan;
34
use Limoncello\Validation\Rules\Comparisons\NumericMoreThan;
35
use Limoncello\Validation\Rules\Comparisons\ScalarEquals;
36
use Limoncello\Validation\Rules\Comparisons\ScalarInValues;
37
use Limoncello\Validation\Rules\Comparisons\ScalarNotEquals;
38
use Limoncello\Validation\Rules\Comparisons\StringLengthBetween;
39
use Limoncello\Validation\Rules\Comparisons\StringLengthMax;
40
use Limoncello\Validation\Rules\Comparisons\StringLengthMin;
41
use Limoncello\Validation\Rules\Comparisons\StringRegExp;
42
use Limoncello\Validation\Rules\Generic\AndOperator;
43
use Limoncello\Validation\Rules\Generic\OrOperator;
44
45
/**
46
 * @package Limoncello\Validation
47
 */
48
trait Comparisons
49
{
50
    /**
51
     * @param mixed              $value
52
     * @param RuleInterface|null $next
53
     *
54
     * @return RuleInterface
55
     */
56 5 View Code Duplication
    protected static function equals($value, RuleInterface $next = null): RuleInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 5
        if ($value === null) {
59 3
            $rule = new IsNull();
60 4
        } elseif ($value instanceof DateTimeInterface) {
61 1
            $rule = new DateTimeEquals($value);
62
        } else {
63 3
            $rule = new ScalarEquals($value);
64
        }
65
66 5
        return $next === null ? $rule : new AndOperator(static::equals($value), $next);
67
    }
68
69
    /**
70
     * @param mixed              $value
71
     * @param RuleInterface|null $next
72
     *
73
     * @return RuleInterface
74
     */
75 2 View Code Duplication
    protected static function notEquals($value, RuleInterface $next = null): RuleInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 2
        if ($value === null) {
78 1
            $rule = new IsNotNull();
79 2
        } elseif ($value instanceof DateTimeInterface) {
80 1
            $rule = new DateTimeNotEquals($value);
81
        } else {
82 1
            $rule = new ScalarNotEquals($value);
83
        }
84
85 2
        return $next === null ? $rule : new AndOperator(static::notEquals($value), $next);
86
    }
87
88
    /**
89
     * @param array              $scalars
90
     * @param RuleInterface|null $next
91
     *
92
     * @return RuleInterface
93
     */
94 1
    protected static function inValues(array $scalars, RuleInterface $next = null): RuleInterface
95
    {
96 1
        return $next === null ? new ScalarInValues($scalars) : new AndOperator(static::inValues($scalars), $next);
97
    }
98
99
    /**
100
     * @param mixed              $value
101
     * @param RuleInterface|null $next
102
     *
103
     * @return RuleInterface
104
     */
105 2
    protected static function lessThan($value, RuleInterface $next = null): RuleInterface
106
    {
107 2
        return $next === null ?
108 2
            $value instanceof DateTimeInterface ? new DateTimeLessThan($value) : new NumericLessThan($value) :
109 2
            new AndOperator(static::lessThan($value), $next);
110
    }
111
112
    /**
113
     * @param mixed              $value
114
     * @param RuleInterface|null $next
115
     *
116
     * @return RuleInterface
117
     */
118 2
    protected static function lessOrEquals($value, RuleInterface $next = null): RuleInterface
119
    {
120 2
        return $next === null ?
121 2
            $value instanceof DateTimeInterface ? new DateTimeLessOrEquals($value) : new NumericLessOrEquals($value) :
122 2
            new AndOperator(static::lessOrEquals($value), $next);
123
    }
124
125
    /**
126
     * @param mixed              $value
127
     * @param RuleInterface|null $next
128
     *
129
     * @return RuleInterface
130
     */
131 2
    protected static function moreThan($value, RuleInterface $next = null): RuleInterface
132
    {
133 2
        return $next === null ?
134 2
            $value instanceof DateTimeInterface ? new DateTimeMoreThan($value) : new NumericMoreThan($value) :
135 2
            new AndOperator(static::moreThan($value), $next);
136
    }
137
138
    /**
139
     * @param mixed              $value
140
     * @param RuleInterface|null $next
141
     *
142
     * @return RuleInterface
143
     */
144 2
    protected static function moreOrEquals($value, RuleInterface $next = null): RuleInterface
145
    {
146 2
        return $next === null ?
147 2
            ($value instanceof DateTimeInterface ?
148 2
                new DateTimeMoreOrEquals($value) : new NumericMoreOrEqualsThan($value)) :
149 2
            new AndOperator(static::moreOrEquals($value), $next);
150
    }
151
152
    /**
153
     * @param mixed              $lowerLimit
154
     * @param mixed              $upperLimit
155
     * @param RuleInterface|null $next
156
     *
157
     * @return RuleInterface
158
     */
159 2
    protected static function between($lowerLimit, $upperLimit, RuleInterface $next = null): RuleInterface
160
    {
161 2
        $areLimitsDates = $lowerLimit instanceof DateTimeInterface && $upperLimit instanceof DateTimeInterface;
162
163 2
        return $next === null ?
164 2
            ($areLimitsDates ?
165 2
                new DateTimeBetween($lowerLimit, $upperLimit) : new NumericBetween($lowerLimit, $upperLimit)) :
166 2
            new AndOperator(static::between($lowerLimit, $upperLimit), $next);
167
    }
168
169
    /**
170
     * @param int                $min
171
     * @param int                $max
172
     * @param RuleInterface|null $next
173
     *
174
     * @return RuleInterface
175
     */
176 1
    protected static function stringLengthBetween(int $min, int $max, RuleInterface $next = null): RuleInterface
177
    {
178 1
        return $next === null ?
179 1
            new StringLengthBetween($min, $max) :
180 1
            new AndOperator(static::stringLengthBetween($min, $max), $next);
181
    }
182
183
    /**
184
     * @param int                $min
185
     * @param RuleInterface|null $next
186
     *
187
     * @return RuleInterface
188
     */
189 1
    protected static function stringLengthMin(int $min, RuleInterface $next = null): RuleInterface
190
    {
191 1
        return $next === null ?
192 1
            new StringLengthMin($min) :
193 1
            new AndOperator(static::stringLengthMin($min), $next);
194
    }
195
196
    /**
197
     * @param int                $max
198
     * @param RuleInterface|null $next
199
     *
200
     * @return RuleInterface
201
     */
202 1
    protected static function stringLengthMax(int $max, RuleInterface $next = null): RuleInterface
203
    {
204 1
        return $next === null ?
205 1
            new StringLengthMax($max) :
206 1
            new AndOperator(static::stringLengthMax($max), $next);
207
    }
208
209
    /**
210
     * @param string             $pattern
211
     * @param RuleInterface|null $next
212
     *
213
     * @return RuleInterface
214
     */
215 1
    protected static function regexp(string $pattern, RuleInterface $next = null): RuleInterface
216
    {
217 1
        return $next === null ?
218 1
            new StringRegExp($pattern) :
219 1
            new AndOperator(static::regexp($pattern), $next);
220
    }
221
222
    /**
223
     * @param RuleInterface $rule
224
     *
225
     * @return RuleInterface
226
     */
227 3
    protected static function nullable(RuleInterface $rule): RuleInterface
228
    {
229 3
        return new OrOperator(static::equals(null), $rule);
230
    }
231
}
232