Passed
Branchmaster (3d3cdc)
by Plamen
01:28
created

thead::ths()   C

Complexity

Conditions 13
Paths 146

Size

Total Lines 62
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 24
nc 146
nop 0
dl 0
loc 62
rs 6.2333
c 2
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 ths()
11
    {
12
        if (self::$cols) {
13
            $ths = [];
14
            $length = sizeof(self::$cols);
15
            for ($i = 0; $i < $length; $i++) {
16
                list($lbl, $col, $arg) = array_pad(self::$cols[$i], 3, null);
17
18
                if (is_null($col)) {
19
                    $arg['sort'] = false;
20
                }
21
22
                $sortable = !isset($arg['sort']) || $arg['sort'] !== false;
23
24
                $sort = $sortable ?
25
                        (isset($arg['sort']) ? $arg['sort'] : $col) :
26
                        null;
27
28
                if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
29
                    $sortable = false;
30
                    if (!isset($arg['width']) &&
31
                            false === strpos($arg['width'], 'width:')
32
                    ) {
33
                        $arg['width'] = '1%';
34
                    }
35
                }
36
37
                if (isset($arg['width'])) { // Width attribute -> style
38
                    $width = 'width:' . $arg['width'] . ';';
39
                    $arg['style'] = isset($arg['style']) ?
40
                            $width . $arg['style'] :
41
                            $width;
42
                }
43
44
                $attr = array_diff_key((array) $arg, ['sort', 'type', 'width']);
45
46
                /*$th = '<th' . self::attributes($attr) . '>';
47
                if ($sortable) {
48
                    $th .= '<a onclick="table.Sort(' . $i . ',this);">';
49
                }
50
                if (!$del) {
51
                    if ($sort == self::$t['order']['col']) {
52
                        $span = self::$t['order']['dir'] === 'desc' ?
53
                                self::config('UTF8_DESC_SYMBOL') :
54
                                self::config('UTF8_ASC_SYMBOL');
55
                    } else {
56
                        $span = "";
57
                    }
58
                    $th .= '<span>' . $span . '</span>' . $lbl;
59
                } else {
60
                    $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
61
                            ' onclick=\"checkAllDeleteCheckboxes(this,' .
62
                            ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
63
                }
64
                if ($sortable) {
65
                    $th .= '</a>';
66
                }
67
                $th .= '</th>';
68
                $ths[] = $th;*/
69
                $ths[] = self::th($i, $attr, $sort, $sortable, $del, $lbl);
70
            }
71
            return implode('', $ths);
72
        }
73
    }
74
    
75
    private static function th($i, $attr, $sort, $sortable, $del, $lbl)
76
    {
77
        $th = '<th' . self::attributes($attr) . '>';
78
        if ($sortable) {
79
            $th .= '<a onclick="table.Sort(' . $i . ',this);">';
80
        }
81
        if (!$del) {
82
            if ($sort == self::$t['order']['col']) {
83
                $span = self::$t['order']['dir'] === 'desc' ?
84
                        self::config('UTF8_DESC_SYMBOL') :
85
                        self::config('UTF8_ASC_SYMBOL');
86
            } else {
87
                $span = "";
88
            }
89
            $th .= '<span>' . $span . '</span>' . $lbl;
90
        } else {
91
            $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
92
                    ' onclick=\"checkAllDeleteCheckboxes(this,' .
93
                    ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
94
        }
95
        if ($sortable) {
96
            $th .= '</a>';
97
        }
98
        $th .= '</th>';
99
        return $th;
100
    }
101
102
103
    protected static function filterValues(&$f, &$opts = [])
104
    {
105
        $f = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?: null;
106
107
        $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
108
        foreach (self::$cols as $k => $v) {
109
            if (isset($v[2]['sort']) && $v[2]['sort'] === false) {
110
                continue;
111
            }
112
            if (empty($v)) {
113
                $v = [null];
114
            } // fix for column requested as []
115
            $selected = $by === $k ? ' selected' : null;
116
            $opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>";
117
        }
118
    }
119
120
    protected static function load()
121
    {
122
        $items = self::$t['items'];
123
        $v = ['items' => $items];
124
        $div_attr = ['id' => $items . '-list', 'class' => 'table'];
125
        if (array_key_exists('div', self::$attributes)) {
126
            $div_attr += self::$attributes['div'];
127
        }
128
        if (isset(self::$attributes['div']['class'])) {
129
            $div_attr['class'] .= ' ' . self::$attributes['div']['class'];
130
        }
131
        $v['div_attributes'] = self::attributes($div_attr);
132
133
        if (self::config('FILTER_ACTIVE')) {
134
            self::filterValues($v['f_value'], $v['f_options']);
135
        }
136
137
        $tbl_attr = ['id' => $items . '-table', 'data-table' => 'js'];
138
        if (array_key_exists('table', self::$attributes)) {
139
            $tbl_attr += self::$attributes['table'];
140
        }
141
        $v['table_attributes'] = self::attributes($tbl_attr);
142
143
        $v['ths'] = self::ths();
144
145
        $v["body"] = self::rowsTbody();
146
147
        $v["footer"] = self::rowsTfoot();
148
149
        return self::view('views/table.html', $v);
150
    }
151
152
    protected static function reset($tableId)
153
    {
154
        if (!in_array($tableId, self::$t["tables"])) {
155
            self::$t["tables"][] = $tableId;
156
            self::$cols = [];
157
            self::$t['rows'] = null;
158
        } else {
159
            echo 'Existing table-id used in table::create(): ' . $tableId;
160
        }
161
    }
162
}
163