Passed
Pull Request — develop (#7)
by Olivier
11:46
created

Active   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 344
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 2
cbo 2

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkUrlIs() 0 4 1
A checkUrlHas() 0 9 1
B checkRouteIs() 0 17 5
A checkRouteIn() 0 4 1
A checkQueryIs() 0 9 3
A checkQueryHas() 0 7 1
A checkQueryHasOnly() 0 8 1
A checkQueryContains() 0 6 1
A check() 0 4 1
A ifUrlIs() 0 4 1
A ifUrlHas() 0 4 1
A ifRouteIs() 0 4 1
A ifRouteIn() 0 4 1
A ifQueryIs() 0 4 1
A ifQueryHas() 0 4 1
A ifQueryHasOnly() 0 4 1
A ifQueryContains() 0 4 1
A getState() 0 4 2
A getActiveValue() 0 8 2
A getInactiveValue() 0 8 2
A setActiveValue() 0 7 2
A setInactiveValue() 0 7 2
A state() 0 8 2
A resetValues() 0 5 1
1
<?php
2
3
namespace Arcesilas\ActiveState;
4
5
use Request;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request as HttpRequest;
8
use Illuminate\Routing\Exceptions\UrlGenerationException;
9
10
class Active
11
{
12
    /**
13
     * Value returned when state is "active"
14
     * @var string
15
     */
16
    protected $activeValue;
17
18
    /**
19
     * Whether $activeValue should be reset after a check or not
20
     * @var boolean
21
     */
22
    protected $activeValuePersistent = false;
23
24
    /**
25
     * Value returned when state is "inactive"
26
     * @var string
27
     */
28
    protected $inactiveValue;
29
30
    /**
31
     * Whether $inactiveValue should be reset after a check or not
32
     * @var [type]
33
     */
34
    protected $inactiveValuePersistent = false;
35
36
    /**
37
     * The Request instance
38
     * @var \Illuminate\Http\Request
39
     */
40
    protected $request;
41
42
    /**
43
     * Obviously: the constructor
44
     * @method __construct
45
     * @param  \Illuminate\Http\Request  $request The HTTP Request handled by Laravel
46
     */
47
    public function __construct(HttpRequest $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
48
    {
49
        $this->request = $request;
50
    }
51
52
    /**
53
     * Check whether url matches the given patterns
54
     * @param  string $patterns Patterns to match
55
     * @return boolean
56
     */
57
    public function checkUrlIs(...$patterns)
58
    {
59
        return $this->request->is(...$patterns);
60
    }
61
62
    /**
63
     * Check whether url contains given strings
64
     * @param  string  $patterns
65
     * @return boolean
66
     */
67
    public function checkUrlHas(...$patterns)
68
    {
69
        return $this->request->is(array_map(
70
            function ($item) {
71
                return "*{$item}*";
72
            },
73
            $patterns
74
        ));
75
    }
76
77
    /**
78
     * Check if the current route name is the one given and the parameters match the current url
79
     * @param  string  $route       The route name to check
80
     * @param  array  $routeparameters The route parameters, used to build the url
0 ignored issues
show
Documentation introduced by
There is no parameter named $routeparameters. Did you maybe mean $routeParameters?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
81
     * @return boolean
82
     */
83
    public function checkRouteIs($route, array $routeParameters = [])
84
    {
85
        if (!is_array($route)) {
86
            $route = [$route => $routeParameters];
87
        }
88
        foreach ($route as $r => $parameters) {
89
            try {
90
                $routeUri = route($r, $parameters, true);
91
                if ($this->request->fullUrlIs($routeUri)) {
92
                    return true;
93
                }
94
            } catch (\Exception $e) {
95
                // Just ignore the exception: route does not exist
96
            }
97
        }
98
        return false;
99
    }
100
101
    /**
102
     * Check whether the current route is one of the given routes
103
     * @method checkRouteIn
104
     * @param  string  $routes
105
     * @return boolean
106
     */
107
    public function checkRouteIn(...$routes)
108
    {
109
        return $this->request->routeIs(...$routes);
110
    }
111
112
    /**
113
     * Check that the parameters of the query string are exactly the ones given
114
     * The test returns true if at least one given array matches the query string parameters (keys and values)
115
     * @param  array  $parameters
116
     * @return boolean
117
     */
118
    public function checkQueryIs(array ...$parameters)
119
    {
120
        foreach ($parameters as $params) {
121
            if ($this->request->query->all() == $params) {
122
                return true;
123
            }
124
        }
125
        return false;
126
    }
127
128
    /**
129
     * Check if query has all of the given parameters, not taking their values into account
130
     * @param  array  $parameters
131
     * @return boolean
132
     */
133
    public function checkQueryHas(...$parameters)
134
    {
135
        $queryKeys = array_keys($this->request->query->all());
136
        return empty(
137
            array_diff($parameters, $queryKeys)
138
        );
139
    }
140
141
    /**
142
     * Check if query parameters names are exactly the ones given in argument
143
     * The order of the parameters does not matter
144
     * @param  string  $parameters string
145
     * @return boolean
146
     */
147
    public function checkQueryHasOnly(...$parameters)
148
    {
149
        $queryKeys = array_keys($this->request->query->all());
150
        sort($queryKeys);
151
        sort($parameters);
152
153
        return $parameters === $queryKeys;
154
    }
155
156
    /**
157
     * Check if query has the given parameters, with their given values
158
     * @param  array  $parameters  The parameters to check for
159
     * @return boolean
160
     */
161
    public function checkQueryContains($parameters)
162
    {
163
        return empty(
164
            array_diff($parameters, $this->request->query->all())
165
        );
166
    }
167
168
    /**
169
     * Common check function
170
     * @param  string  $check  The check method to use
171
     * @param  array  $arguments  Arguments to check with
172
     * @return string
173
     */
174
    protected function check($check, array $arguments)
175
    {
176
        return $this->getState(call_user_func_array([$this, $check], $arguments));
177
    }
178
179
    /**
180
     * Returns activeValue or inactiveValue for checkUrlIs test
181
     * @param  string  $patterns
182
     * @return string  Active state string
183
     * @see Active::checkUrl
184
     */
185
    public function ifUrlIs(...$patterns)
186
    {
187
        return $this->check('checkUrlIs', $patterns);
188
    }
189
190
    /**
191
     * Returns activeValue or inactiveValue for checkUrlHas test
192
     * @param  string  $patterns
193
     * @return string  Active state string
194
     * @see Active::checkUrlHas()
195
     */
196
    public function ifUrlHas(...$patterns)
197
    {
198
        return $this->check('checkUrlHas', $patterns);
199
    }
200
201
    /**
202
     * Returns activeValue or inactiveValue for checkRouteIs test
203
     * @param  string  $route
204
     * @param  array  $parameters
205
     * @return string  Active state string
206
     * @see Active::checkRouteIs()
207
     */
208
    public function ifRouteIs($route, array $parameters = [])
209
    {
210
        return $this->check('checkRouteIs', [$route, $parameters]);
211
    }
212
213
    /**
214
     * Return activeValue or inactiveValue for checkRouteIn test
215
     * @param  string  $routes
216
     * @return boolean
217
     * @see Active::checkRouteIn()
218
     */
219
    public function ifRouteIn(...$routes)
220
    {
221
        return $this->check('checkRouteIn', $routes);
222
    }
223
224
    /**
225
     * Returns activeValue or inactiveValue for checkQuery test
226
     * @param  string  $patterns
0 ignored issues
show
Bug introduced by
There is no parameter named $patterns. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
227
     * @return string               Active state string
228
     * @see Active::checkQueryIs()
229
     */
230
    public function ifQueryIs(array ...$parameters)
231
    {
232
        return $this->check('checkQueryIs', $parameters);
233
    }
234
235
    /**
236
     * Returns activeValue or inactiveValue for checkQueryHas test
237
     * @param  string  $patterns
0 ignored issues
show
Bug introduced by
There is no parameter named $patterns. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
238
     * @return string  Active state string
239
     * @see Active::checkQueryHas()
240
     */
241
    public function ifQueryHas(...$parameters)
242
    {
243
        return $this->check('checkQueryHas', $parameters);
244
    }
245
246
    /**
247
     * Returns activeValue or inactiveValue for checkQueryHasOnly test
248
     * @param  string  $patterns
0 ignored issues
show
Bug introduced by
There is no parameter named $patterns. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
249
     * @return string  Active state string
250
     * @see Active::checkQueryHasOnly()
251
     */
252
    public function ifQueryHasOnly(...$parameters)
253
    {
254
        return $this->check('checkQueryHasOnly', $parameters);
255
    }
256
257
    /**
258
     * Returns activeValue or inactiveValue for checkQueryContains test
259
     * @param  string  $parameters
260
     * @return string  Active state string
261
     * @see Active::checkQueryContains()
262
     */
263
    public function ifQueryContains($parameters)
264
    {
265
        return $this->check('checkQueryContains', $parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
266
    }
267
268
    /**
269
     * Returns the active state string depending on the actual state
270
     * @param  boolean  $active
271
     * @return string
272
     */
273
    public function getState($active)
274
    {
275
        return $active ? $this->getActiveValue() : $this->getInactiveValue();
276
    }
277
278
    /**
279
     * Returns the active state string
280
     * @return string
281
     */
282
    public function getActiveValue()
283
    {
284
        $return = $this->activeValue ?? config('active.active_state', 'active');
285
        if (! $this->activeValuePersistent) {
286
            $this->setActiveValue();
287
        }
288
        return $return;
289
    }
290
291
    /**
292
     * Return the inactive state string
293
     * @return string
294
     */
295
    public function getInactiveValue()
296
    {
297
        $return = $this->inactiveValue ?? config('active.inactive_state', '');
298
        if (! $this->inactiveValuePersistent) {
299
            $this->setInactiveValue();
300
        }
301
        return $return;
302
    }
303
304
    /**
305
     * Set the active state string
306
     * @param  string  $value  The string value
307
     * @param  boolean|null  $persistent Whether to reset the value after the next check
308
     */
309
    public function setActiveValue($value = null, $persistent = null)
310
    {
311
        $this->activeValue = $value;
312
        if (null !== $persistent) {
313
            $this->activeValuePersistent = (bool) $persistent;
314
        }
315
    }
316
317
    /**
318
     * Set the inactive state string
319
     * @param  string  $value      The string value
320
     * @param  boolean|null  $persistent Whether to reset the value after the next check
321
     */
322
    public function setInactiveValue($value = null, $persistent = null)
323
    {
324
        $this->inactiveValue = $value;
325
        if (null !== $persistent) {
326
            $this->inactiveValuePersistent = (bool) $persistent;
327
        }
328
    }
329
330
    /**
331
     * Change active and inactive state at runtime and allow method chaining
332
     * @param  string $active_state
333
     * @param  string $inactive_state
334
     * @return Active
335
     */
336
    public function state(string $active_state, string $inactive_state = null)
337
    {
338
        $this->setActiveValue($active_state, false);
339
        if ($inactive_state) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inactive_state of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
340
            $this->setInactiveValue($inactive_state, false);
341
        }
342
        return $this;
343
    }
344
345
    /**
346
     * Reset both active and inactive state strings
347
     */
348
    public function resetValues()
349
    {
350
        $this->activeValue = null;
351
        $this->inactiveValue = null;
352
    }
353
}
354