Passed
Push — master ( b4f91d...b5810c )
by Bas
07:20 queued 04:57
created

ValidatesExpressions   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 44
c 1
b 0
f 1
dl 0
loc 215
ccs 52
cts 56
cp 0.9286
rs 9.2
wmc 40

17 Methods

Rating   Name   Duplication   Size   Complexity  
A isNumber() 0 3 2
A isAssociativeArray() 0 7 2
A isRange() 0 7 3
A isBindParameter() 0 7 2
A isFunction() 0 3 1
A isObject() 0 7 4
A isIndexedArray() 0 7 2
A isKey() 0 10 3
A isId() 0 10 3
A isCollection() 0 7 3
A isGraphDirection() 0 7 3
A isBoolean() 0 3 3
A isQuery() 0 3 1
A isGraph() 0 3 1
A isSortDirection() 0 7 3
A isNull() 0 3 2
A isList() 0 3 2

How to fix   Complexity   

Complex Class

Complex classes like ValidatesExpressions 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.

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 ValidatesExpressions, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace LaravelFreelancerNL\FluentAQL\Traits;
4
5
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
6
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
7
8
trait ValidatesExpressions
9
{
10
    use ValidatesOperators;
11
    use ValidatesReferences;
12
13
    /**
14
     * @param mixed $value
15
     *
16
     * @return bool
17
     */
18 4
    public function isRange($value): bool
19
    {
20 4
        if (is_string($value) && preg_match('/^[0-9]+(?:\.[0-9]+)?+\.{2}[0-9]+(?:\.[0-9]+)?$/', $value)) {
21 2
            return true;
22
        }
23
24 3
        return false;
25
    }
26
27
    /**
28
     * @param mixed $value
29
     *
30
     * @return bool
31
     */
32 5
    public function isBoolean($value): bool
33
    {
34 5
        return is_bool($value) || $value === 'true' || $value === 'false';
35
    }
36
37
    /**
38
     * @param $value
39
     *
40
     * @return bool
41
     */
42 5
    public function isNull($value): bool
43
    {
44 5
        return $value === null || $value == 'null';
45
    }
46
47
    /**
48
     * @param $value
49
     *
50
     * @return bool
51
     */
52 6
    public function isNumber($value): bool
53
    {
54 6
        return is_numeric($value) && !is_string($value);
55
    }
56
57
    /**
58
     * @param mixed $value
59
     *
60
     * @return bool
61
     */
62 6
    public function isList($value): bool
63
    {
64 6
        return is_array($value) && $this->isIndexedArray($value);
65
    }
66
67
    /**
68
     * @param mixed $value
69
     * @return bool
70
     */
71
    public function isQuery($value): bool
72
    {
73
        return $value instanceof QueryBuilder;
74
    }
75
76
    /**
77
     * @param mixed $value
78
     * @return bool
79
     */
80 6
    public function isFunction($value): bool
81
    {
82 6
        return $value instanceof FunctionExpression;
83
    }
84
85
    /**
86
     * @param mixed $value
87
     * @return bool
88
     */
89 1
    public function isSortDirection($value): bool
90
    {
91 1
        if (is_string($value) && preg_match('/asc|desc/i', $value)) {
92 1
            return true;
93
        }
94
95 1
        return false;
96
    }
97
98
    /**
99
     * @param mixed $value
100
     * @return bool
101
     */
102 2
    public function isGraphDirection($value): bool
103
    {
104 2
        if (is_string($value) && preg_match('/outbound|inbound|any/i', $value)) {
105 2
            return true;
106
        }
107
108 2
        return false;
109
    }
110
111
    /**
112
     * @param mixed $value
113
     *
114
     * @return bool
115
     */
116 6
    public function isCollection($value): bool
117
    {
118 6
        if (is_string($value) && preg_match('/^[a-zA-Z0-9_-]+$/', $value)) {
119 6
            return true;
120
        }
121
122 4
        return false;
123
    }
124
125
    /**
126
     * @param mixed $value
127
     * @return bool
128
     */
129
    public function isGraph($value): bool
130
    {
131
        return $this->isCollection($value);
132
    }
133
134
    /**
135
     * @param mixed $value
136
     * @return bool
137
     */
138 2
    public function isKey($value): bool
139
    {
140
        if (
141 2
            is_string($value) &&
142 2
            preg_match("/^[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
143
        ) {
144 1
            return true;
145
        }
146
147 2
        return false;
148
    }
149
150
    /**
151
     * @param mixed $value
152
     * @return bool
153
     */
154 3
    public function isId($value): bool
155
    {
156
        if (
157 3
            is_string($value) &&
158 3
            preg_match("/^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
159
        ) {
160 2
            return true;
161
        }
162
163 2
        return false;
164
    }
165
166
    /**
167
     * @param mixed $value
168
     *
169
     * @return bool
170
     */
171 6
    public function isObject($value): bool
172
    {
173 6
        if (is_object($value) || (is_array($value) && $this->isAssociativeArray($value))) {
174 1
            return true;
175
        }
176
177 6
        return false;
178
    }
179
180
    /**
181
     * @param mixed $bindParameter
182
     * @return bool
183
     */
184 1
    public function isBindParameter($bindParameter): bool
185
    {
186 1
        if (preg_match('/^@?[a-zA-Z0-9][a-zA-Z0-9_]*$/', $bindParameter)) {
187 1
            return true;
188
        }
189
190 1
        return false;
191
    }
192
193
    /**
194
     * Check if the array is associative.
195
     *
196
     * @param array $array
197
     *
198
     * @return bool
199
     */
200 5
    public function isAssociativeArray(array $array): bool
201
    {
202 5
        if (empty($array)) {
203 1
            return true;
204
        }
205
206 5
        return !ctype_digit(implode('', array_keys($array)));
207
    }
208
209
    /**
210
     * Check if the array is numeric.
211
     *
212
     * @param array $array
213
     *
214
     * @return bool
215
     */
216 2
    public function isIndexedArray(array $array): bool
217
    {
218 2
        if (empty($array)) {
219 1
            return true;
220
        }
221
222 2
        return ctype_digit(implode('', array_keys($array)));
223
    }
224
}
225