Passed
Branch next (ee2197)
by Bas
02:37
created

ValidatesExpressions   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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