| Total Complexity | 58 |
| Total Lines | 212 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like table_setter 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 table_setter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class table_setter |
||
| 4 | { |
||
| 5 | /** @param array Columns (config) */ |
||
| 6 | public static $cols; |
||
| 7 | /** @param bool Export flag (for current request) */ |
||
| 8 | public static $export; |
||
| 9 | /** @param array Collector (values for current table call) */ |
||
| 10 | protected static $t = ['page' => 1, 'tables' => []]; |
||
| 11 | /** @param array Re-settable values for current table call */ |
||
| 12 | protected static $config; |
||
| 13 | |||
| 14 | static function assets($path = "/public/table") |
||
| 18 | } |
||
| 19 | |||
| 20 | public static function config($c) |
||
| 21 | { |
||
| 22 | self::$config = self::$config ?: [ |
||
| 23 | 'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI |
||
| 24 | 'EMPTY_TABLE_MSG' => 'No results found.', |
||
| 25 | 'EXPORT_FILE_NAME' => ':app - :items export (:datetime)', |
||
| 26 | 'FILTER_CASE_SENSITIVE' => false, |
||
| 27 | 'SAVES' => ['CSV', 'Excel'], |
||
| 28 | 'UTF8_ASC_SYMBOL' => '▲', |
||
| 29 | 'UTF8_DESC_SYMBOL' => '▼', |
||
| 30 | 'UTF8_LEFT_SYMBOL' => '‹', |
||
| 31 | 'UTF8_RIGHT_SYMBOL' => '›' |
||
| 32 | ]; |
||
| 33 | |||
| 34 | $getValid = function($ex, $v) { //disallow empty and mis-type overrides |
||
| 35 | return !empty($v) && gettype($ex) === gettype($v) ? $v : $ex; |
||
| 36 | }; |
||
| 37 | |||
| 38 | switch (gettype($c)) { |
||
| 39 | case 'array'; |
||
| 40 | foreach ($c as $k => $v) { |
||
| 41 | self::$config[$k] = $getValid(self::$config[$k], $v); |
||
| 42 | } |
||
| 43 | break; |
||
| 44 | case 'string': |
||
| 45 | return self::$config[$c]; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** Converts array to space separated key value list |
||
| 50 | * @param array $attributes |
||
| 51 | * @return string */ |
||
| 52 | protected static function attributes(array $attributes) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** Parses view to string |
||
| 70 | * @param string $template |
||
| 71 | * @param array $vars |
||
| 72 | * @return string */ |
||
| 73 | protected static function view($template, $vars = []) |
||
| 74 | { |
||
| 75 | extract($vars); |
||
| 76 | ob_start(); |
||
| 77 | require $template; |
||
| 78 | return (string) ob_get_clean(); |
||
| 79 | } |
||
| 80 | |||
| 81 | protected static function paging(&$v) |
||
| 82 | { |
||
| 83 | if (self::$t['pages'] > 1) { |
||
| 84 | $v['pages'] = self::$t['pages']; |
||
| 85 | $v['page'] = self::$t['page']; |
||
| 86 | $v['jumpL'] = self::pagingJumps(); |
||
| 87 | $v['jumpR'] = self::pagingJumps(); |
||
| 88 | $v['arrowL'] = self::config('UTF8_LEFT_SYMBOL'); |
||
| 89 | $v['arrowR'] = self::config('UTF8_RIGHT_SYMBOL'); |
||
| 90 | |||
| 91 | $limit = 10; |
||
| 92 | |||
| 93 | $v['start'] = $v['page'] > ($limit / 2) ? |
||
| 94 | ($v['page'] - $limit / 2) : |
||
| 95 | 1; |
||
| 96 | |||
| 97 | if ($v['page'] > ($v['pages'] - ($limit / 2))) { |
||
| 98 | $v['final'] = $v['pages']; |
||
| 99 | } else if ($v['page'] > ($limit / 2)) { |
||
| 100 | $v['final'] = $v['start'] + $limit; |
||
| 101 | } else { |
||
| 102 | $v['final'] = $limit; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** Jump links -1M|-100K|-10K|-1K|100|{paging}|100|+1K|+10K|+100K|+1M |
||
| 108 | * @param string $html Code to show |
||
| 109 | * @param null|int $m multiplier |
||
| 110 | * @return string */ |
||
| 111 | protected static function pagingJumps($html = '', $m = null) |
||
| 141 | } |
||
| 142 | |||
| 143 | protected static function filterValues(&$f, &$opts = []) |
||
| 144 | { |
||
| 145 | $f = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?: null; |
||
| 146 | |||
| 147 | $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT); |
||
| 148 | foreach (self::$cols as $k => $v) { |
||
| 149 | if (isset($v[2]['sort']) && $v[2]['sort'] === false) { |
||
| 150 | continue; |
||
| 151 | } |
||
| 152 | if (empty($v)) { |
||
| 153 | $v = [null]; |
||
| 154 | } // fix for column requested as [] |
||
| 155 | $selected = $by === $k ? ' selected' : null; |
||
| 156 | $opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>"; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | protected static function requestFilter() |
||
| 161 | { |
||
| 162 | $filter = filter_input(INPUT_GET, 'filter') ?: false; |
||
| 163 | if ($filter) { |
||
| 164 | $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT); |
||
| 165 | if ($by === false || is_null($by)) { |
||
| 166 | $by = []; |
||
| 167 | foreach (self::$cols as $v) { |
||
| 168 | if (isset($v[2]['sort']) && $v[2]['sort'] === false) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | $by[] = 'IFNULL(' . $v[1] . ', "")'; |
||
| 172 | } |
||
| 173 | $by = 'CONCAT(' . implode(',', $by) . ')'; |
||
| 174 | } else { |
||
| 175 | $by = self::$cols[$by][1]; |
||
| 176 | } |
||
| 177 | $by = 'CONCAT(" ",' . $by . ', " ")'; |
||
| 178 | if (self::config('FILTER_CASE_SENSITIVE') !== true) { |
||
| 179 | $by .= ' COLLATE ' . self::config('DB_COLLATION_CI'); |
||
| 180 | } |
||
| 181 | $filter = $by . ' LIKE ' . '"%' . $filter . '%"'; |
||
| 182 | } |
||
| 183 | return $filter; |
||
| 184 | } |
||
| 185 | |||
| 186 | protected static function requestOrderCol() |
||
| 194 | } |
||
| 195 | |||
| 196 | protected static function requestOrderDir() |
||
| 197 | { |
||
| 198 | $reset = filter_has_var(INPUT_GET, 'col') ? 'asc' : null; |
||
| 199 | return in_array(filter_input(INPUT_GET, 'ord'), ['asc', 'desc']) ? |
||
| 200 | filter_input(INPUT_GET, 'ord') : |
||
| 201 | ($reset ?: self::$t['order']['dir']); |
||
| 202 | } |
||
| 203 | |||
| 204 | protected static function requestExport() |
||
| 208 | } |
||
| 209 | |||
| 210 | protected static function requestPage() |
||
| 211 | { |
||
| 212 | return filter_has_var(INPUT_GET, 'pg') && self::$export == false ? |
||
| 215 | } |
||
| 216 | } |
||
| 217 |