Passed
Push — master ( d98d70...91fc8a )
by Plamen
02:11
created

thead::request()   C

Complexity

Conditions 14
Paths 1

Size

Total Lines 57
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 41
nc 1
nop 0
dl 0
loc 57
rs 6.2666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class thead extends tbody
4
{
5
    /** @param array Attributes for the table Html tag */
6
    public static $attributes = [];
7
    /** @param null|str Extension (from $_SERVER['REQUEST_URI']) */
8
    public static $pageExt;
9
10
    protected static function load()
11
    {
12
        $items = self::$t['items'];
13
        $v = ['items' => $items];
14
15
        if (self::config('FILTER_ACTIVE')) {
16
            self::filterValues($v['f_value'], $v['f_options']);
17
        }
18
19
        $v['div_attributes'] = self::tagAttributes('div', $items);
20
21
        $v['table_attributes'] = self::tagAttributes('table', $items);
22
23
        $v['ths'] = self::$cols ? self::ths() : null;
24
25
        $v['body'] = self::rowsTbody();
26
27
        $v['footer'] = self::rowsTfoot();
28
29
        return self::view('views/table.html', $v);
30
    }
31
32
    protected static function filterValues(&$f, &$opts = [])
33
    {
34
        $f = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?: null;
35
36
        $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
37
        foreach (self::$cols as $k => $v) {
38
            if (isset($v[2]['sort']) && $v[2]['sort'] === false) {
39
                continue;
40
            }
41
            $selected = $by === $k ? ' selected' : null;
42
            $opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>";
43
        }
44
    }
45
46
    private static function tagAttributes($tag, $items)
47
    {
48
        $attr = $tag === 'div' ?
49
                ['id' => $items . '-list', 'class' => 'table'] :
50
                ['id' => $items . '-table', 'data-table' => 'js', 
51
                'data-sort-a' => self::config('UTF8_ASC_SYMBOL'),
52
                'data-sort-d' => self::config('UTF8_DESC_SYMBOL')];
53
        if (array_key_exists($tag, self::$attributes)) {
54
            $attr += self::$attributes[$tag];
55
        }
56
        if (isset(self::$attributes[$tag]['class'])) {
57
            $attr['class'] .= ' ' . self::$attributes[$tag]['class'];
58
        }
59
        return self::attributes($attr);
60
    }
61
62
    protected static function ths()
63
    {
64
        $ths = [];
65
        $length = sizeof(self::$cols);
66
        for ($i = 0; $i < $length; $i++) {
67
            list($lbl, $col, $arg) = array_pad(self::$cols[$i], 3, null);
68
69
            if (is_null($col)) {
70
                $arg['sort'] = false;
71
            }
72
73
            $sort = $del = null;
74
75
            $attr = self::thAttributes($col, $arg, $sort, $del);
76
77
            $ths[] = self::th($i, $attr, $sort, $del, $lbl);
78
        }
79
        return implode('', $ths);
80
    }
81
82
    private static function thAttributes($col, $arg, &$sort, &$del)
83
    {
84
        if (isset($arg['width'])) { // Width attribute -> style
85
            $width = 'width:' . $arg['width'] . ';';
86
            $arg['style'] = isset($arg['style']) ?
87
                    $width . $arg['style'] :
88
                    $width;
89
        }
90
91
        if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
92
            $sort = false;
93
        } else {
94
            $sort = isset($arg['sort']) ? $arg['sort'] : $col;
95
        }
96
97
        return array_diff_key((array) $arg, ['sort', 'type', 'width']);
98
    }
99
100
    private static function th($i, $attr, $sort, $del, $lbl)
101
    {
102
        $th = '<th' . self::attributes($attr) . '>';
103
        if ($sort) {
104
            $th .= '<a onclick="table.Sort(' . $i . ',this);">';
105
        }
106
        if (!$del) {
107
            if ($sort == self::$t['order']['col']) {
108
                $span = self::$t['order']['dir'] === 'desc' ?
109
                        self::config('UTF8_DESC_SYMBOL') :
110
                        self::config('UTF8_ASC_SYMBOL');
111
            } else {
112
                $span = "";
113
            }
114
            $th .= '<span>' . $span . '</span>' . $lbl;
115
        } else {
116
            $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
117
                    ' onclick=\"checkAllDeleteCheckboxes(this,' .
118
                    ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
119
        }
120
        if ($sort) {
121
            $th .= '</a>';
122
        }
123
        $th .= '</th>';
124
        return $th;
125
    }
126
    
127
    protected static function request()
128
    {
129
        $export = function(){
130
            $exp = filter_input(INPUT_GET, 'export', FILTER_SANITIZE_STRING);
131
            return in_array($exp, self::config('SAVES')) ? $exp : false;
0 ignored issues
show
Bug introduced by
It seems like self::config('SAVES') can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            return in_array($exp, /** @scrutinizer ignore-type */ self::config('SAVES')) ? $exp : false;
Loading history...
132
        };
133
        $order_dir = function(){
134
            $reset = filter_has_var(INPUT_GET, 'col') ? 'asc' : null;
135
            return
136
                    in_array(filter_input(INPUT_GET, 'ord'), ['asc', 'desc']) ?
137
                    filter_input(INPUT_GET, 'ord') :
138
                    ($reset ?: self::$t['order']['dir']);
139
        };
140
        $order_col = function(){
141
            $col = filter_input(INPUT_GET, 'col', FILTER_VALIDATE_INT);
142
            if ($col) {
143
                return isset(self::$cols[$col][2]['sort']) ?
144
                        self::$cols[$col][2]['sort'] :
145
                        self::$cols[$col][1];
146
            }
147
            return self::$t['order']['col'];
148
        };
149
        $filter = function(){
150
            $filter = filter_input(INPUT_GET, 'filter') ?: false;
151
            if ($filter) {
152
                $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
153
                if ($by === false || is_null($by)) {
154
                    $by = self::request('FilterByAll');
0 ignored issues
show
Unused Code introduced by
The call to thead::request() has too many arguments starting with 'FilterByAll'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
                    /** @scrutinizer ignore-call */ 
155
                    $by = self::request('FilterByAll');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
155
                } else {
156
                    $by = self::$cols[$by][1];
157
                }
158
                $by = 'CONCAT(" ",' . $by . ', " ")';
159
                if (self::config('FILTER_CASE_SENSITIVE') !== true) {
160
                    $by .= ' COLLATE ' . self::config('DB_COLLATION_CI');
161
                }
162
                $filter = $by . ' LIKE ' . '"%' . $filter . '%"';
163
            }
164
            return $filter;
165
        };
166
        $page = function(){
167
            return  filter_has_var(INPUT_GET, 'pg') && self::$export == false ?
168
                (int)filter_input(INPUT_GET, 'pg', FILTER_SANITIZE_NUMBER_INT) :
169
                self::$t['page'];
170
        };
171
        
172
        self::$export = $export();
173
174
        $t = [
175
            'order' => [
176
                    'dir' => $order_dir(),
177
                    'col' => $order_col()
178
                ],
179
            'filter' => $filter(),
180
            'page' => $page()
181
        ];
182
        //dd(array_merge(self::$t, $t));
183
        return array_merge(self::$t, $t);
184
    }
185
}
186