Passed
Push — master ( 2da942...d633d7 )
by Paul
15:03 queued 05:40
created

Helper   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 43
eloc 66
dl 0
loc 242
ccs 77
cts 84
cp 0.9167
rs 8.96
c 1
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostId() 0 9 4
A inRange() 0 7 1
A filterInput() 0 10 4
A ifEmpty() 0 4 3
A buildClassName() 0 7 2
A compareVersions() 0 5 1
A ifTrue() 0 3 2
A buildMethodName() 0 3 1
A getPageNumber() 0 10 3
A buildPropertyName() 0 3 1
A filterInputArray() 0 7 4
A getIpAddress() 0 20 4
A isGreaterThanOrEqual() 0 3 1
A isLessThanOrEqual() 0 3 1
A isGreaterThan() 0 3 1
A isLessThan() 0 3 1
A isEmpty() 0 6 4
A isNotEmpty() 0 3 1
A runClosure() 0 6 4

How to fix   Complexity   

Complex Class

Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\Cache;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Helpers\Url;
9
use GeminiLabs\Vectorface\Whip\Whip;
10
11
class Helper
12
{
13
    /**
14
     * @param string $name
15
     * @param string $path
16
     * @return string
17
     */
18 8
    public static function buildClassName($name, $path = '')
19
    {
20 8
        $className = Str::camelCase($name);
21 8
        $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\');
22 8
        return !empty($path)
23 8
            ? __NAMESPACE__.'\\'.$path.'\\'.$className
24 8
            : $className;
25
    }
26
27
    /**
28
     * @param string $name
29
     * @param string $prefix
30
     * @return string
31
     */
32 43
    public static function buildMethodName($name, $prefix = '')
33
    {
34 43
        return lcfirst(Str::camelCase($prefix.'-'.$name));
35
    }
36
37
    /**
38
     * @param string $name
39
     * @return string
40
     */
41 1
    public static function buildPropertyName($name)
42
    {
43 1
        return static::buildMethodName($name);
44
    }
45
46
    /**
47
     * @param int|string $version1
48
     * @param int|string $version2
49
     * @param string $operator
50
     * @return bool
51
     */
52 12
    public static function compareVersions($version1, $version2, $operator = '=')
53
    {
54 12
        $version1 = implode('.', array_pad(explode('.', $version1), 3, 0));
55 12
        $version2 = implode('.', array_pad(explode('.', $version2), 3, 0));
56 12
        return version_compare($version1, $version2, $operator);
57
    }
58
59
    /**
60
     * @param string $key
61
     * @return mixed
62
     */
63 8
    public static function filterInput($key, array $request = [])
64
    {
65 8
        if (isset($request[$key])) {
66 7
            return $request[$key];
67
        }
68 8
        $variable = filter_input(INPUT_POST, $key);
69 8
        if (is_null($variable) && isset($_POST[$key])) {
70 8
            $variable = $_POST[$key];
71
        }
72 8
        return $variable;
73
    }
74
75
    /**
76
     * @param string $key
77
     * @return array
78
     */
79 8
    public static function filterInputArray($key)
80
    {
81 8
        $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
82 8
        if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) {
83 8
            $variable = $_POST[$key];
84
        }
85 8
        return Cast::toArray($variable);
86
    }
87
88
    /**
89
     * @return string
90
     */
91 16
    public static function getIpAddress()
92
    {
93 16
        $whitelist = [];
94 16
        $isUsingCloudflare = !empty(filter_input(INPUT_SERVER, 'CF-Connecting-IP'));
95 16
        if (glsr()->filterBool('whip/whitelist/cloudflare', $isUsingCloudflare)) {
96
            $cloudflareIps = glsr(Cache::class)->getCloudflareIps();
97
            $whitelist[Whip::CLOUDFLARE_HEADERS] = [Whip::IPV4 => $cloudflareIps['v4']];
98
            if (defined('AF_INET6')) {
99
                $whitelist[Whip::CLOUDFLARE_HEADERS][Whip::IPV6] = $cloudflareIps['v6'];
100
            }
101
        }
102 16
        $whitelist = glsr()->filterArray('whip/whitelist', $whitelist);
103 16
        $methods = glsr()->filterInt('whip/methods', Whip::ALL_METHODS);
104 16
        $whip = new Whip($methods, $whitelist);
105 16
        glsr()->action('whip', $whip);
106 16
        if (false !== ($clientAddress = $whip->getValidIpAddress())) {
107 16
            return (string) $clientAddress;
108
        }
109
        glsr_log()->error('Unable to detect IP address, please see the FAQ page for a possible solution.');
110
        return 'unknown';
111
    }
112
113
    /**
114
     * @param string $fromUrl
115
     * @param int $fallback
116
     * @return int
117
     */
118 2
    public static function getPageNumber($fromUrl = null, $fallback = 1)
119
    {
120 2
        $pagedQueryVar = glsr()->constant('PAGED_QUERY_VAR');
121 2
        $pageNum = empty($fromUrl)
122 2
            ? filter_input(INPUT_GET, $pagedQueryVar, FILTER_VALIDATE_INT)
123 2
            : filter_var(Url::query($fromUrl, $pagedQueryVar), FILTER_VALIDATE_INT);
124 2
        if (empty($pageNum)) {
125 2
            $pageNum = (int) $fallback;
126
        }
127 2
        return max(1, $pageNum);
128
    }
129
130
    /**
131
     * @param mixed $post
132
     * @return int
133
     */
134 3
    public static function getPostId($post)
135
    {
136 3
        if (is_numeric($post) || $post instanceof \WP_Post) {
137 3
            $post = get_post($post);
138
        }
139 3
        if ($post instanceof \WP_Post) {
140 3
            return $post->ID;
141
        }
142
        return 0;
143
    }
144
145
    /**
146
     * @param mixed $value
147
     * @param mixed $fallback
148
     * @return mixed
149
     */
150 47
    public static function ifEmpty($value, $fallback, $strict = false)
151
    {
152 47
        $isEmpty = $strict ? empty($value) : static::isEmpty($value);
153 47
        return $isEmpty ? $fallback : $value;
154
    }
155
156
    /**
157
     * @param bool $condition
158
     * @param mixed $ifTrue
159
     * @param mixed $ifFalse
160
     * @return mixed
161
     */
162 33
    public static function ifTrue($condition, $ifTrue, $ifFalse = null)
163
    {
164 33
        return $condition ? static::runClosure($ifTrue) : static::runClosure($ifFalse);
165
    }
166
167
    /**
168
     * @param mixed $value
169
     * @param string|int $min
170
     * @param string|int $max
171
     * @return bool
172
     */
173 6
    public static function inRange($value, $min, $max)
174
    {
175 6
        $inRange = filter_var($value, FILTER_VALIDATE_INT, ['options' => [
176 6
            'min_range' => intval($min),
177 6
            'max_range' => intval($max),
178
        ]]);
179 6
        return false !== $inRange;
180
    }
181
182
    /**
183
     * @param mixed $value
184
     * @return bool
185
     */
186 53
    public static function isEmpty($value)
187
    {
188 53
        if (is_string($value)) {
189 52
            return trim($value) === '';
190
        }
191 53
        return !is_numeric($value) && !is_bool($value) && empty($value);
192
    }
193
194
    /**
195
     * @param int|string $value
196
     * @param int|string $compareWithValue
197
     * @return bool
198
     */
199 8
    public static function isGreaterThan($value, $compareWithValue)
200
    {
201 8
        return static::compareVersions($value, $compareWithValue, '>');
202
    }
203
204
    /**
205
     * @param int|string $value
206
     * @param int|string $compareWithValue
207
     * @return bool
208
     */
209 1
    public static function isGreaterThanOrEqual($value, $compareWithValue)
210
    {
211 1
        return static::compareVersions($value, $compareWithValue, '>=');
212
    }
213
214
    /**
215
     * @param int|string $value
216
     * @param int|string $compareWithValue
217
     * @return bool
218
     */
219 1
    public static function isLessThan($value, $compareWithValue)
220
    {
221 1
        return static::compareVersions($value, $compareWithValue, '<');
222
    }
223
224
    /**
225
     * @param int|string $value
226
     * @param int|string $compareWithValue
227
     * @return bool
228
     */
229 1
    public static function isLessThanOrEqual($value, $compareWithValue)
230
    {
231 1
        return static::compareVersions($value, $compareWithValue, '<=');
232
    }
233
234
    /**
235
     * @param mixed $value
236
     * @return bool
237
     */
238 43
    public static function isNotEmpty($value)
239
    {
240 43
        return !static::isEmpty($value);
241
    }
242
243
    /**
244
     * @param mixed $value
245
     * @return mixed
246
     */
247 33
    public static function runClosure($value)
248
    {
249 33
        if ($value instanceof \Closure || (is_array($value) && is_callable($value))) {
250 31
            return call_user_func($value);
251
        }
252 27
        return $value;
253
    }
254
}
255