Comparisons   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 188
Duplicated Lines 12.77 %

Coupling/Cohesion

Components 0
Dependencies 23

Test Coverage

Coverage 100%

Importance

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

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 declare(strict_types=1);
2
3
namespace Limoncello\Validation\Validator;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeInterface;
22
use Limoncello\Validation\Contracts\Rules\RuleInterface;
23
use Limoncello\Validation\Rules\Comparisons\DateTimeBetween;
24
use Limoncello\Validation\Rules\Comparisons\DateTimeEquals;
25
use Limoncello\Validation\Rules\Comparisons\DateTimeLessOrEquals;
26
use Limoncello\Validation\Rules\Comparisons\DateTimeLessThan;
27
use Limoncello\Validation\Rules\Comparisons\DateTimeMoreOrEquals;
28
use Limoncello\Validation\Rules\Comparisons\DateTimeMoreThan;
29
use Limoncello\Validation\Rules\Comparisons\DateTimeNotEquals;
30
use Limoncello\Validation\Rules\Comparisons\IsNotNull;
31
use Limoncello\Validation\Rules\Comparisons\IsNull;
32
use Limoncello\Validation\Rules\Comparisons\NumericBetween;
33
use Limoncello\Validation\Rules\Comparisons\NumericLessOrEquals;
34
use Limoncello\Validation\Rules\Comparisons\NumericLessThan;
35
use Limoncello\Validation\Rules\Comparisons\NumericMoreOrEqualsThan;
36
use Limoncello\Validation\Rules\Comparisons\NumericMoreThan;
37
use Limoncello\Validation\Rules\Comparisons\ScalarEquals;
38
use Limoncello\Validation\Rules\Comparisons\ScalarInValues;
39
use Limoncello\Validation\Rules\Comparisons\ScalarNotEquals;
40
use Limoncello\Validation\Rules\Comparisons\StringLengthBetween;
41
use Limoncello\Validation\Rules\Comparisons\StringLengthMax;
42
use Limoncello\Validation\Rules\Comparisons\StringLengthMin;
43
use Limoncello\Validation\Rules\Comparisons\StringRegExp;
44
use Limoncello\Validation\Rules\Generic\AndOperator;
45
use Limoncello\Validation\Rules\Generic\OrOperator;
46
47
/**
48
 * @package Limoncello\Validation
49
 */
50
trait Comparisons
51
{
52
    /**
53
     * @param mixed              $value
54
     * @param RuleInterface|null $next
55
     *
56
     * @return RuleInterface
57
     *
58
     * @SuppressWarnings(PHPMD.ElseExpression)
59
     */
60 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...
61
    {
62 5
        if ($value === null) {
63 3
            $rule = new IsNull();
64 4
        } elseif ($value instanceof DateTimeInterface) {
65 1
            $rule = new DateTimeEquals($value);
66
        } else {
67 3
            $rule = new ScalarEquals($value);
68
        }
69
70 5
        return $next === null ? $rule : new AndOperator(static::equals($value), $next);
71
    }
72
73
    /**
74
     * @param mixed              $value
75
     * @param RuleInterface|null $next
76
     *
77
     * @return RuleInterface
78
     *
79
     * @SuppressWarnings(PHPMD.ElseExpression)
80
     */
81 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...
82
    {
83 2
        if ($value === null) {
84 1
            $rule = new IsNotNull();
85 2
        } elseif ($value instanceof DateTimeInterface) {
86 1
            $rule = new DateTimeNotEquals($value);
87
        } else {
88 1
            $rule = new ScalarNotEquals($value);
89
        }
90
91 2
        return $next === null ? $rule : new AndOperator(static::notEquals($value), $next);
92
    }
93
94
    /**
95
     * @param array              $scalars
96
     * @param RuleInterface|null $next
97
     *
98
     * @return RuleInterface
99
     */
100 1
    protected static function inValues(array $scalars, RuleInterface $next = null): RuleInterface
101
    {
102 1
        return $next === null ? new ScalarInValues($scalars) : new AndOperator(static::inValues($scalars), $next);
103
    }
104
105
    /**
106
     * @param mixed              $value
107
     * @param RuleInterface|null $next
108
     *
109
     * @return RuleInterface
110
     */
111 2
    protected static function lessThan($value, RuleInterface $next = null): RuleInterface
112
    {
113 2
        return $next === null ?
114 2
            $value instanceof DateTimeInterface ? new DateTimeLessThan($value) : new NumericLessThan($value) :
115 2
            new AndOperator(static::lessThan($value), $next);
116
    }
117
118
    /**
119
     * @param mixed              $value
120
     * @param RuleInterface|null $next
121
     *
122
     * @return RuleInterface
123
     */
124 2
    protected static function lessOrEquals($value, RuleInterface $next = null): RuleInterface
125
    {
126 2
        return $next === null ?
127 2
            $value instanceof DateTimeInterface ? new DateTimeLessOrEquals($value) : new NumericLessOrEquals($value) :
128 2
            new AndOperator(static::lessOrEquals($value), $next);
129
    }
130
131
    /**
132
     * @param mixed              $value
133
     * @param RuleInterface|null $next
134
     *
135
     * @return RuleInterface
136
     */
137 2
    protected static function moreThan($value, RuleInterface $next = null): RuleInterface
138
    {
139 2
        return $next === null ?
140 2
            $value instanceof DateTimeInterface ? new DateTimeMoreThan($value) : new NumericMoreThan($value) :
141 2
            new AndOperator(static::moreThan($value), $next);
142
    }
143
144
    /**
145
     * @param mixed              $value
146
     * @param RuleInterface|null $next
147
     *
148
     * @return RuleInterface
149
     */
150 2
    protected static function moreOrEquals($value, RuleInterface $next = null): RuleInterface
151
    {
152 2
        return $next === null ?
153 2
            ($value instanceof DateTimeInterface ?
154 2
                new DateTimeMoreOrEquals($value) : new NumericMoreOrEqualsThan($value)) :
155 2
            new AndOperator(static::moreOrEquals($value), $next);
156
    }
157
158
    /**
159
     * @param mixed              $lowerLimit
160
     * @param mixed              $upperLimit
161
     * @param RuleInterface|null $next
162
     *
163
     * @return RuleInterface
164
     */
165 3
    protected static function between($lowerLimit, $upperLimit, RuleInterface $next = null): RuleInterface
166
    {
167 3
        $areLimitsDates = $lowerLimit instanceof DateTimeInterface && $upperLimit instanceof DateTimeInterface;
168
169 3
        return $next === null ?
170 3
            ($areLimitsDates ?
171 3
                new DateTimeBetween($lowerLimit, $upperLimit) : new NumericBetween($lowerLimit, $upperLimit)) :
172 3
            new AndOperator(static::between($lowerLimit, $upperLimit), $next);
173
    }
174
175
    /**
176
     * @param int                $min
177
     * @param int                $max
178
     * @param RuleInterface|null $next
179
     *
180
     * @return RuleInterface
181
     */
182 2
    protected static function stringLengthBetween(int $min, int $max, RuleInterface $next = null): RuleInterface
183
    {
184 2
        return $next === null ?
185 2
            new StringLengthBetween($min, $max) :
186 2
            new AndOperator(static::stringLengthBetween($min, $max), $next);
187
    }
188
189
    /**
190
     * @param int                $min
191
     * @param RuleInterface|null $next
192
     *
193
     * @return RuleInterface
194
     */
195 1
    protected static function stringLengthMin(int $min, RuleInterface $next = null): RuleInterface
196
    {
197 1
        return $next === null ?
198 1
            new StringLengthMin($min) :
199 1
            new AndOperator(static::stringLengthMin($min), $next);
200
    }
201
202
    /**
203
     * @param int                $max
204
     * @param RuleInterface|null $next
205
     *
206
     * @return RuleInterface
207
     */
208 1
    protected static function stringLengthMax(int $max, RuleInterface $next = null): RuleInterface
209
    {
210 1
        return $next === null ?
211 1
            new StringLengthMax($max) :
212 1
            new AndOperator(static::stringLengthMax($max), $next);
213
    }
214
215
    /**
216
     * @param string             $pattern
217
     * @param RuleInterface|null $next
218
     *
219
     * @return RuleInterface
220
     */
221 1
    protected static function regexp(string $pattern, RuleInterface $next = null): RuleInterface
222
    {
223 1
        return $next === null ?
224 1
            new StringRegExp($pattern) :
225 1
            new AndOperator(static::regexp($pattern), $next);
226
    }
227
228
    /**
229
     * @param RuleInterface $rule
230
     *
231
     * @return RuleInterface
232
     */
233 3
    protected static function nullable(RuleInterface $rule): RuleInterface
234
    {
235 3
        return new OrOperator(static::equals(null), $rule);
236
    }
237
}
238