Compare   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 95.31%

Importance

Changes 0
Metric Value
wmc 44
lcom 2
cbo 0
dl 0
loc 194
ccs 61
cts 64
cp 0.9531
rs 8.3396
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D apply() 0 44 30
A access() 0 4 2
A eq() 0 4 1
A oneOf() 0 4 1
A max() 0 4 1
A min() 0 4 1
A between() 0 5 2
A integer() 0 4 1
A positiveInteger() 0 4 1
A decimal() 0 4 1
A positiveDecimal() 0 4 1
A equalToField() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Compare often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Compare, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * 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, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Validate\Rule;
22
23
/**
24
 * Compares scalar values to some operand.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Compare implements \Caridea\Validate\Rule
30
{
31
    /**
32
     * @var string The operator type
33
     */
34
    private $operator;
35
    /**
36
     * @var mixed The comparison value
37
     */
38
    private $operand;
39
40
    /**
41
     * Creates a new CompareRule.
42
     *
43
     * @param string $operator The operator type
44
     * @param mixed $operand Optional comparison value
45
     */
46 10
    protected function __construct(string $operator, $operand = null)
47
    {
48 10
        $this->operator = $operator;
49 10
        $this->operand = $operand;
50 10
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 10
    public function apply($value, $data = []): ?array
56
    {
57 10
        if ("eqf" === $this->operator) {
58 1
            if ($value !== null && !is_scalar($value)) {
59 1
                return ['FORMAT_ERROR'];
60
            }
61 1
            return $value === $this->access($data, $this->operand) ?
62 1
                null : ['FIELDS_NOT_EQUAL'];
63
        }
64 9
        if (!is_scalar($value)) {
65 9
            return ['FORMAT_ERROR'];
66
        }
67 9
        switch ($this->operator) {
68 9
            case "in":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69 2
                return in_array($value, $this->operand, true) ? null : ['NOT_ALLOWED_VALUE'];
70 7
            case "lt":
71 1
                return $value > $this->operand ? ['TOO_HIGH'] : null;
72 6
            case "gt":
73 1
                return $value < $this->operand ? ['TOO_LOW'] : null;
74 5
            case "bt":
75 1
                if ($value > $this->operand[1]) {
76 1
                    return ['TOO_HIGH'];
77 1
                } elseif ($value < $this->operand[0]) {
78 1
                    return ['TOO_LOW'];
79
                }
80 1
                return null;
81 4
            case "int":
82 1
                return is_int($value) || ctype_digit(ltrim((string)$value, '-+')) ?
83 1
                    null : ['NOT_INTEGER'];
84 3
            case "+int":
85 1
                return (is_int($value) || ctype_digit(ltrim((string)$value, '-+'))) &&
86 1
                    ((int) $value) > 0 ? null : ['NOT_POSITIVE_INTEGER'];
87 2
            case "float":
88 1
                return is_float($value) || ($value === (string)(float)$value) ?
89 1
                    null : ['NOT_DECIMAL'];
90 1
            case "+float":
91 1
                if (is_float($value)) {
92 1
                    return $value <= 0 ? ['NOT_POSITIVE_DECIMAL'] : null;
93 1
                } elseif ($value === (string)(float)$value) {
94 1
                    return ((float) $value) <= 0 ? ['NOT_POSITIVE_DECIMAL'] : null;
95
                }
96 1
                return ['NOT_POSITIVE_DECIMAL'];
97
        }
98
    }
99
100
    /**
101
     * Gets a field from the values.
102
     *
103
     * This can be overridden to access by other means (e.g. object properties,
104
     * getter methods).
105
     *
106
     * @param mixed $values The values
107
     * @param string $field The field to access
108
     * @return mixed The accessed value
109
     */
110
    protected function access($values, string $field)
111
    {
112
        return isset($values[$field]) ? $values[$field] : null;
113
    }
114
115
    /**
116
     * Gets a rule that matches a value against another value.
117
     *
118
     * @param string $value The accepted value
119
     * @return \Caridea\Validate\Rule\Compare the created rule
120
     */
121 1
    public static function eq(string $value): Compare
122
    {
123 1
        return new Compare('in', [$value]);
124
    }
125
126
    /**
127
     * Gets a rule that matches a value against a list of accepted values.
128
     *
129
     * @param array $values The accepted values
130
     * @return \Caridea\Validate\Rule\Compare the created rule
131
     */
132 1
    public static function oneOf(array $values): Compare
133
    {
134 1
        return new Compare('in', $values);
135
    }
136
137
    /**
138
     * Gets a rule that requires numbers to be no greater than a limit.
139
     *
140
     * @param int|float $value The maximum value
141
     * @return \Caridea\Validate\Rule\Compare the created rule
142
     */
143 1
    public static function max($value): Compare
144
    {
145 1
        return new Compare('lt', $value);
146
    }
147
148
    /**
149
     * Gets a rule that requires numbers to be no less than a limit.
150
     *
151
     * @param int|float $value The minimum value
152
     * @return \Caridea\Validate\Rule\Compare the created rule
153
     */
154 1
    public static function min($value): Compare
155
    {
156 1
        return new Compare('gt', $value);
157
    }
158
159
    /**
160
     * Gets a rule that requires numbers to be in a given range.
161
     *
162
     * @param int|float $min The minimum value, inclusive
163
     * @param int|float $max The maximum value, inclusive
164
     * @return \Caridea\Validate\Rule\Compare the created rule
165
     */
166 1
    public static function between($min, $max): Compare
167
    {
168 1
        $value = $min > $max ? [$max, $min] : [$min, $max];
169 1
        return new Compare('bt', $value);
170
    }
171
172
    /**
173
     * Gets a rule that matches integers and strings with integer values.
174
     *
175
     * @return \Caridea\Validate\Rule\Compare the created rule
176
     */
177 1
    public static function integer(): Compare
178
    {
179 1
        return new Compare('int');
180
    }
181
182
    /**
183
     * Gets a rule that matches positive integers
184
     *
185
     * @return \Caridea\Validate\Rule\Compare the created rule
186
     */
187 1
    public static function positiveInteger(): Compare
188
    {
189 1
        return new Compare('+int');
190
    }
191
192
    /**
193
     * Gets a rule that matches floats and strings with float values.
194
     *
195
     * @return \Caridea\Validate\Rule\Compare the created rule
196
     */
197 1
    public static function decimal(): Compare
198
    {
199 1
        return new Compare('float');
200
    }
201
202
    /**
203
     * Gets a rule that matches positive floats
204
     *
205
     * @return \Caridea\Validate\Rule\Compare the created rule
206
     */
207 1
    public static function positiveDecimal(): Compare
208
    {
209 1
        return new Compare('+float');
210
    }
211
212
    /**
213
     * Gets a rule that compares two fields for equality
214
     *
215
     * @param string $field The other field whose value will be compared
216
     * @return \Caridea\Validate\Rule\Compare the created rule
217
     */
218 1
    public static function equalToField(string $field): Compare
219
    {
220 1
        return new Compare('eqf', $field);
221
    }
222
}
223