Completed
Push — master ( a694da...3962ba )
by Grzegorz
02:30
created

FilterHandlers::validFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace ComicVine\Api\Filters;
4
5
/**
6
 * Trait FilterHandlers
7
 *
8
 * @package grzgajda/comicvine-api
9
 * @author  Grzegorz Gajda <[email protected]>
10
 */
11
trait FilterHandlers
12
{
13
    /**
14
     * Validation for setted filters to stay
15
     * clean URL.
16
     *
17
     * @param array $filters Setted filters.
18
     * @param array $enabled Enabled filters.
19
     *
20
     * @return bool
21
     */
22
    protected function flushDisabledFilters(array $filters, array $enabled)
23
    {
24
        array_walk($filters, function (&$param, $field) use ($enabled) {
25
            // 'api_key' field
26
            if ($this->isApiKey($field) === true) {
27
                return $param;
28
            }
29
            // 'field_list'
30
            if ($this->isFieldList($field) === true) {
31
                return $param = $this->validFieldList($field, $param);
32
            }
33
            // 'sort', 'filter', 'limit', 'offset'
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
            if ($this->isFilter($field, $enabled) === true) {
35
                return $param = $this->validFilter($param);
36
            }
37
38
            return $param = [];
39
        });
40
41
        return $filters;
42
    }
43
44
    /**
45
     * Checking if current position of array_walk is
46
     * 'api_key'.
47
     *
48
     * @param string $field Key of field.
49
     *
50
     * @return bool
51
     */
52
    protected function isApiKey($field)
53
    {
54
        if ($field === "api_key") {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * Checking if current position of array_walk
63
     * is 'api_key'.
64
     *
65
     * @param string $field Key of field.
66
     *
67
     * @return bool
68
     */
69
    protected function isFieldList($field)
70
    {
71
        if ($field === "field_list") {
72
            return true;
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * Valid for 'field_list' key.
80
     *
81
     * @param string       $field Key of field.
82
     * @param string|array $param Param of field.
83
     *
84
     * @return array
85
     */
86
    protected function validFieldList($field, $param)
87
    {
88
        if ($field === 'field_list' && $param !== "") {
89
            return $param;
90
        }
91
92
        return [];
93
    }
94
95
    /**
96
     * Check if current position is enabled.
97
     *
98
     * @param string $field   Key of field
99
     * @param array  $enabled Array of enabled filters.
100
     *
101
     * @return bool
102
     */
103
    protected function isFilter($field, $enabled)
104
    {
105
        if (array_key_exists($field, $enabled) === false) {
106
            return false;
107
        }
108
109
        if ($enabled[$field] === true) {
110
            return true;
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
     * Check if params are valid.
118
     *
119
     * @param string|array $param Params of filter.
120
     *
121
     * @return string|array
122
     */
123
    protected function validFilter($param)
124
    {
125
        if ($param !== "") {
126
            return $param;
127
        }
128
129
        return [];
130
    }
131
132
}