Total Complexity | 58 |
Total Lines | 410 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like TrapDirectorTable 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 TrapDirectorTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | abstract class TrapDirectorTable |
||
11 | { |
||
12 | /** @var array $titles table titles (name, display value) */ |
||
13 | protected $titles = null; |
||
14 | |||
15 | /** @var array $content table content (name, sb column name). name index must be the same as $titles*/ |
||
16 | protected $content = null; |
||
17 | |||
18 | /** @var array $columnNames names of columns for filtering */ |
||
19 | protected $columnNames = array(); |
||
20 | |||
21 | /** @var Selectable $dbConn connection to database */ |
||
22 | protected $dbConn = null; |
||
23 | |||
24 | /** Current view **/ |
||
25 | protected $view; |
||
26 | |||
27 | protected $urlPath; |
||
28 | |||
29 | // Database stuff |
||
30 | /** @var array $table (db ref, name) */ |
||
31 | protected $table = array(); |
||
32 | |||
33 | /** @var Queryable $query Query in database; */ |
||
34 | protected $query = null; |
||
35 | |||
36 | /** @var array $order : (db column, 'ASC' | 'DESC') */ |
||
37 | protected $order = array(); |
||
38 | protected $orderQuery = ''; |
||
39 | |||
40 | /********* Filter ********/ |
||
41 | /** @var string $filterString : string filter for db columns */ |
||
42 | protected $filterString = ''; |
||
43 | |||
44 | /** @var array $filterColumn : columns to apply filter to */ |
||
45 | protected $filterColumn = array(); |
||
46 | |||
47 | protected $filterQuery=''; |
||
48 | |||
49 | /*************** Paging *************/ |
||
50 | protected $maxPerPage = 25; |
||
51 | |||
52 | protected $currentPage = 0; |
||
53 | |||
54 | function __construct(array $table,array $titles, array $columns, array $columnNames, $dbConn , $view, $urlPath) |
||
55 | { |
||
56 | $this->table = $table; |
||
57 | $this->titles = $titles; |
||
58 | $this->content = $columns; |
||
59 | $this->columnNames = $columnNames; |
||
60 | $this->dbConn = $dbConn; |
||
61 | |||
62 | $this->view = $view; |
||
63 | $this->urlPath = $urlPath; |
||
64 | |||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | |||
69 | /************** GET variables and URLs *************/ |
||
70 | public function getParams(array $getVars) |
||
71 | { |
||
72 | $this->getFilterQuery($getVars); |
||
73 | $this->getPagingQuery($getVars); |
||
74 | } |
||
75 | |||
76 | public function getCurrentURL() |
||
77 | { |
||
78 | return '?'; |
||
79 | } |
||
80 | |||
81 | protected function getCurrentURLAndQS(string $caller) |
||
82 | { |
||
83 | $actionURL = $this->getCurrentURL() . '?' ; |
||
84 | $QSList=array(); |
||
85 | if ($caller != 'filter' && $this->curFilterQuery() != '') |
||
86 | array_push($QSList , $this->curFilterQuery()); |
||
87 | |||
88 | if ($caller != 'paging' && $caller != 'filter' && $this->curPagingQuery() != '') |
||
89 | array_push($QSList , $this->curPagingQuery()); |
||
90 | |||
91 | if ($caller != 'order' && $this->curOrderQuery() != '') |
||
92 | array_push($QSList , $this->curOrderQuery()); |
||
93 | |||
94 | if (count($QSList) != 0) |
||
95 | $actionURL .= implode('&', $QSList); |
||
96 | |||
97 | return $actionURL; |
||
98 | } |
||
99 | |||
100 | /************* DB queries ******************/ |
||
101 | /** |
||
102 | * Get base query in $this->query |
||
103 | * @return TrapDirectorTable |
||
104 | */ |
||
105 | public function getBaseQuery() |
||
106 | { |
||
107 | $this->query = $this->dbConn->select(); |
||
108 | $this->query = $this->query->from( |
||
109 | $this->table, |
||
110 | $this->content |
||
111 | ); |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | public function fullQuery() |
||
117 | { |
||
118 | $this->getBaseQuery() |
||
119 | ->applyFilter() |
||
120 | ->applyPaging() |
||
121 | ->applyOrder(); |
||
122 | |||
123 | return $this->dbConn->fetchAll($this->query); |
||
124 | //return $this->query->fetchAll(); |
||
125 | } |
||
126 | |||
127 | |||
128 | /**************** Filtering ******************/ |
||
129 | public function applyFilter() |
||
130 | { |
||
131 | if ($this->filterString == '' || count($this->filterColumn) == 0) |
||
132 | { |
||
133 | return $this; |
||
134 | } |
||
135 | $filter=''; |
||
136 | foreach ($this->filterColumn as $column) |
||
137 | { |
||
138 | if ($filter != "") $filter.=' OR '; |
||
139 | //$filter .= "'" . $column . "' LIKE '%" . $this->filterString. "%'"; |
||
140 | $filter .= $column . " LIKE '%" . $this->filterString. "%'"; |
||
141 | } |
||
142 | //echo $filter; |
||
143 | |||
144 | $this->query=$this->query->where($filter); |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | public function setFilter(string $filter, array $filterCol) |
||
150 | { |
||
151 | $this->filterString = $filter; |
||
152 | $this->filterColumn = $filterCol; |
||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | |||
157 | public function renderFilter() |
||
158 | { |
||
159 | |||
160 | $html=' <form id="genfilter" name="mainFilterGen" |
||
161 | enctype="application/x-www-form-urlencoded" |
||
162 | action="'.$this->getCurrentURLAndQS('filter').'" |
||
163 | method="get">'; |
||
164 | $html.='<input type="text" name="f" title="Search is simple! Try to combine multiple words" |
||
165 | placeholder="Search..." value="'.$this->filterQuery.'">'; |
||
166 | |||
167 | $html.='</form>'; |
||
168 | return $html; |
||
169 | } |
||
170 | |||
171 | public function getFilterQuery(array $getVars) |
||
172 | { |
||
173 | if (isset($getVars['f'])) |
||
174 | { |
||
175 | $this->filterQuery = $getVars['f']; |
||
176 | $this->setFilter(html_entity_decode($getVars['f']), $this->columnNames); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | protected function curFilterQuery() |
||
181 | { |
||
182 | if ($this->filterQuery == '') return ''; |
||
183 | return 'f='.$this->filterQuery; |
||
184 | } |
||
185 | |||
186 | /***************** Ordering ********************/ |
||
187 | public function applyOrder() |
||
188 | { |
||
189 | if (count($this->order) == 0) |
||
190 | { |
||
191 | return $this; |
||
192 | } |
||
193 | $orderSQL=''; |
||
194 | foreach ($this->order as $column => $direction) |
||
195 | { |
||
196 | if ($orderSQL != "") $orderSQL.=','; |
||
197 | |||
198 | $orderSQL .= $column . ' ' . $direction; |
||
199 | } |
||
200 | $this->query = $this->query->order($orderSQL); |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | public function setOrder(array $order) |
||
206 | { |
||
207 | $this->order = $order; |
||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | public function getOrderQuery(array $getVars) |
||
212 | { |
||
213 | if (isset($getVars['o'])) |
||
214 | { |
||
215 | $this->orderQuery = $getVars['o']; |
||
216 | // TODO |
||
217 | } |
||
218 | } |
||
219 | |||
220 | protected function curOrderQuery() |
||
221 | { |
||
222 | if ($this->orderQuery == '') return ''; |
||
223 | return 'o='.$this->orderQuery; |
||
224 | } |
||
225 | |||
226 | /***************** Paging and counting *********/ |
||
227 | public function countQuery() |
||
228 | { |
||
229 | $this->query = $this->dbConn->select(); |
||
230 | $this->query = $this->query |
||
231 | ->from( |
||
232 | $this->table, |
||
233 | array('COUNT(*)') |
||
234 | ); |
||
235 | $this->applyFilter(); |
||
236 | } |
||
237 | |||
238 | public function count() |
||
239 | { |
||
240 | $this->countQuery(); |
||
241 | return $this->dbConn->fetchOne($this->query); |
||
242 | } |
||
243 | |||
244 | public function setMaxPerPage(int $max) |
||
245 | { |
||
246 | $this->maxPerPage = $max; |
||
247 | } |
||
248 | |||
249 | protected function getPagingQuery(array $getVars) |
||
250 | { |
||
251 | if (isset($getVars['page'])) |
||
252 | { |
||
253 | $this->currentPage = $getVars['page']; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | protected function curPagingQuery() |
||
258 | { |
||
259 | if ($this->currentPage == '') return ''; |
||
260 | return 'page='.$this->currentPage; |
||
261 | } |
||
262 | |||
263 | public function renderPagingHeader() |
||
264 | { |
||
265 | $count = $this->count(); |
||
266 | if ($count <= $this->maxPerPage ) |
||
267 | { |
||
268 | return 'count : ' . $this->count() . '<br>'; |
||
269 | } |
||
270 | |||
271 | if ($this->currentPage == 0) $this->currentPage = 1; |
||
272 | |||
273 | $numPages = intdiv($count , $this->maxPerPage); |
||
274 | if ($count % $this->maxPerPage != 0 ) $numPages++; |
||
275 | |||
276 | $html = '<div class="pagination-control" role="navigation">'; |
||
277 | $html .= '<ul class="nav tab-nav">'; |
||
278 | if ($this->currentPage <=1) |
||
279 | { |
||
280 | $html .= ' |
||
281 | <li class="nav-item disabled" aria-hidden="true"> |
||
282 | <span class="previous-page"> |
||
283 | <span class="sr-only">Previous page</span> |
||
284 | <i aria-hidden="true" class="icon-angle-double-left"></i> |
||
285 | </span> |
||
286 | </li> |
||
287 | '; |
||
288 | } |
||
289 | else |
||
290 | { |
||
291 | $html .= ' |
||
292 | <li class="nav-item"> |
||
293 | <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. ($this->currentPage - 1 ).'" class="previous-page" > |
||
294 | <i aria-hidden="true" class="icon-angle-double-left"></i> |
||
295 | </a> |
||
296 | </li> |
||
297 | '; |
||
298 | } |
||
299 | |||
300 | for ($i=1; $i <= $numPages ; $i++) |
||
301 | { |
||
302 | $active = ($this->currentPage == $i) ? 'active' : ''; |
||
303 | $first = ($i-1)*$this->maxPerPage+1; |
||
304 | $last = $i * $this->maxPerPage; |
||
305 | if ($last > $count) $last = $count; |
||
306 | $display = 'Show rows '. $first . ' to '. $last .' of '. $count; |
||
307 | $html .= '<li class="' . $active . ' nav-item"> |
||
308 | <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. $i .'" title="' . $display . '" aria-label="' . $display . '"> |
||
309 | '.$i.' |
||
310 | </a> |
||
311 | </li>'; |
||
312 | } |
||
313 | |||
314 | if ($this->currentPage == $numPages) |
||
315 | { |
||
316 | $html .= ' |
||
317 | <li class="nav-item disabled" aria-hidden="true"> |
||
318 | <span class="previous-page"> |
||
319 | <span class="sr-only">Previous page</span> |
||
320 | <i aria-hidden="true" class="icon-angle-double-right"></i> |
||
321 | </span> |
||
322 | </li> |
||
323 | '; |
||
324 | } |
||
325 | else |
||
326 | { |
||
327 | $html .= ' |
||
328 | <li class="nav-item"> |
||
329 | <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. ($this->currentPage + 1 ).'" class="next-page"> |
||
330 | <i aria-hidden="true" class="icon-angle-double-right"></i> |
||
331 | </a> |
||
332 | </li> |
||
333 | '; |
||
334 | } |
||
335 | |||
336 | $html .= '</ul> </div>'; |
||
337 | |||
338 | $htmlOLD = ' |
||
339 | <ul class="nav tab-nav"> |
||
340 | |||
341 | <li class="active nav-item"> |
||
342 | <a href="/icingaweb2/trapdirector/received/?q=&page=1" title="Show rows 1 to 25 of 29" aria-label="Show rows 1 to 25 of 29"> |
||
343 | 1 |
||
344 | </a> |
||
345 | </li> |
||
346 | <li class="nav-item"> |
||
347 | <a href="/icingaweb2/trapdirector/received/?q=&page=2" title="Show rows 26 to 29 of 29" aria-label="Show rows 26 to 29 of 29"> |
||
348 | 2 |
||
349 | </a> |
||
350 | </li> |
||
351 | <li class="nav-item"> |
||
352 | <a href="/icingaweb2/trapdirector/received/?q=&page=2" title="Show rows 26 to 50 of 29" aria-label="Show rows 26 to 50 of 29" class="next-page"> |
||
353 | <i aria-hidden="true" class="icon-angle-double-right"></i> |
||
354 | </a> |
||
355 | </li> |
||
356 | </ul> |
||
357 | '; |
||
358 | return $html; |
||
359 | } |
||
360 | |||
361 | public function applyPaging() |
||
362 | { |
||
363 | $this->query->limitPage($this->currentPage,$this->maxPerPage); |
||
364 | return $this; |
||
365 | } |
||
366 | |||
367 | /*************** Rendering *************************/ |
||
368 | public function renderTitles() |
||
369 | { |
||
370 | $html = "<thead>\n<tr>\n"; |
||
371 | foreach ($this->titles as $values) |
||
372 | { |
||
373 | $html .= '<th>' . $values . '</th>'; |
||
374 | } |
||
375 | $html .= "</tr>\n</thead>\n"; |
||
376 | return $html; |
||
377 | } |
||
378 | |||
379 | public function renderLine( $value) |
||
380 | { |
||
381 | $html = ''; |
||
382 | $titleNames = array_keys($this->titles); |
||
383 | foreach ($titleNames as $name ) |
||
384 | { |
||
385 | $html .= '<td>'; |
||
386 | $html .= $value->$name; |
||
387 | $html .= "</td>\n"; |
||
388 | } |
||
389 | return $html; |
||
390 | } |
||
391 | |||
392 | public function renderTable(array $values) |
||
393 | { |
||
394 | $html = '<tbody id="obj_table_body">'; |
||
395 | foreach($values as $value) |
||
396 | { |
||
397 | $html .= "<tr>\n"; |
||
398 | $html .= $this->renderLine($value); |
||
399 | $html .= "</tr>\n"; |
||
400 | } |
||
401 | $html .= '</tbody>'; |
||
402 | return $html; |
||
403 | } |
||
404 | |||
405 | public function render() |
||
420 | } |
||
421 | |||
422 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths