Total Complexity | 64 |
Total Lines | 411 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HtmlTable 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 HtmlTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class HtmlTable extends HtmlSemDoubleElement { |
||
22 | use TableTrait; |
||
23 | private $_colCount; |
||
24 | private $_compileParts; |
||
25 | private $_footer; |
||
26 | private $_afterCompileEvents; |
||
27 | private $_activeRowSelector; |
||
28 | |||
29 | public function __construct($identifier, $rowCount, $colCount) { |
||
30 | parent::__construct($identifier, "table", "ui table"); |
||
31 | $this->content=array (); |
||
32 | $this->setRowCount($rowCount, $colCount); |
||
33 | $this->_variations=[ Variation::CELLED,Variation::PADDED,Variation::COMPACT ]; |
||
34 | $this->_compileParts=["thead","tbody","tfoot"]; |
||
35 | $this->_afterCompileEvents=[]; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
40 | * @param string $key |
||
41 | * @return HtmlTableContent |
||
42 | */ |
||
43 | public function getPart($key) { |
||
44 | if (\array_key_exists($key, $this->content) === false) { |
||
45 | $this->content[$key]=new HtmlTableContent("", $key); |
||
46 | if ($key !== "tbody") { |
||
47 | $this->content[$key]->setRowCount(1, $this->_colCount); |
||
48 | } |
||
49 | } |
||
50 | return $this->content[$key]; |
||
51 | } |
||
52 | |||
53 | protected function _getFirstPart(){ |
||
54 | if(isset($this->content["thead"])){ |
||
55 | return $this->content["thead"]; |
||
56 | } |
||
57 | return $this->content["tbody"]; |
||
58 | } |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Returns/create eventually the body of the table |
||
63 | * @return HtmlTableContent |
||
64 | */ |
||
65 | public function getBody() { |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Returns the number of rows (TR) |
||
71 | * @return int |
||
72 | */ |
||
73 | public function getRowCount() { |
||
74 | return $this->getPart("tbody")->count(); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns/create eventually the header of the table |
||
79 | * @return HtmlTableContent |
||
80 | */ |
||
81 | public function getHeader() { |
||
82 | return $this->getPart("thead"); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Returns/create eventually the footer of the table |
||
87 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
88 | */ |
||
89 | public function getFooter() { |
||
90 | return $this->getPart("tfoot"); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Checks if the part corresponding to $key exists |
||
95 | * @param string $key |
||
96 | * @return boolean |
||
97 | */ |
||
98 | public function hasPart($key) { |
||
99 | return \array_key_exists($key, $this->content) === true; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * |
||
104 | * @param int $rowCount |
||
105 | * @param int $colCount |
||
106 | * @return HtmlTableContent |
||
107 | */ |
||
108 | public function setRowCount($rowCount, $colCount) { |
||
109 | $this->_colCount=$colCount; |
||
110 | return $this->getBody()->setRowCount($rowCount, $colCount); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns the cell (HtmlTD) at position $row,$col |
||
115 | * @param int $row |
||
116 | * @param int $col |
||
117 | * @return HtmlTD |
||
118 | */ |
||
119 | public function getCell($row, $col) { |
||
120 | return $this->getBody()->getCell($row, $col); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Retuns the row at $rowIndex |
||
125 | * @param int $rowIndex |
||
126 | * @return HtmlTR |
||
127 | */ |
||
128 | public function getRow($rowIndex) { |
||
129 | return $this->getBody()->getRow($rowIndex); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Adds a new row and sets $values to his cols |
||
134 | * @param array $values |
||
135 | * @return HtmlTR |
||
136 | */ |
||
137 | public function addRow($values=array()) { |
||
138 | $row=$this->getBody()->addRow($this->_colCount); |
||
139 | $row->setValues(\array_values($values)); |
||
140 | return $row; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * adds and returns a new row |
||
145 | * @return HtmlTR |
||
146 | */ |
||
147 | public function newRow() { |
||
148 | return $this->getBody()->newRow($this->_colCount); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Sets the tbody values |
||
153 | * @param array $values values in an array of array |
||
154 | * @return HtmlTable |
||
155 | */ |
||
156 | public function setValues($values=array()) { |
||
157 | $this->getBody()->setValues($values); |
||
158 | return $this; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Sets the header values |
||
163 | * @param array $values |
||
164 | * @return HtmlTableContent |
||
165 | */ |
||
166 | public function setHeaderValues($values=array()) { |
||
167 | return $this->getHeader()->setValues($values); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Sets the footer values |
||
172 | * @param array $values |
||
173 | * @return HtmlTableContent |
||
174 | */ |
||
175 | public function setFooterValues($values=array()) { |
||
176 | return $this->getFooter()->setValues($values); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Sets values to the col at index $colIndex |
||
181 | * @param int $colIndex |
||
182 | * @param array $values |
||
183 | * @return HtmlTable |
||
184 | */ |
||
185 | public function setColValues($colIndex, $values=array()) { |
||
186 | $this->getBody()->setColValues($colIndex, $values); |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Sets values to the row at index $rowIndex |
||
192 | * @param int $rowIndex |
||
193 | * @param array $values |
||
194 | * @return HtmlTable |
||
195 | */ |
||
196 | public function setRowValues($rowIndex, $values=array()) { |
||
197 | $this->getBody()->setRowValues($rowIndex, $values); |
||
198 | return $this; |
||
199 | } |
||
200 | |||
201 | public function addColVariations($colIndex, $variations=array()) { |
||
202 | return $this->getBody()->addColVariations($colIndex, $variations); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Sets the col alignment to center |
||
207 | * @param int $colIndex |
||
208 | * @return HtmlTable |
||
209 | */ |
||
210 | public function colCenter($colIndex) { |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Sets the col alignment to right |
||
216 | * @param int $colIndex |
||
217 | * @return HtmlTable |
||
218 | */ |
||
219 | public function colRight($colIndex) { |
||
220 | return $this->colAlign($colIndex, "colRight"); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Sets col alignment to left |
||
225 | * @param int $colIndex |
||
226 | * @return HtmlTable |
||
227 | */ |
||
228 | public function colLeft($colIndex) { |
||
229 | return $this->colAlign($colIndex, "colLeft"); |
||
230 | } |
||
231 | |||
232 | public function setColAlignment($colIndex,$alignment){ |
||
233 | switch ($alignment){ |
||
234 | case TextAlignment::LEFT: |
||
235 | $function="colLeft"; |
||
236 | break; |
||
237 | case TextAlignment::RIGHT: |
||
238 | $function="colRight"; |
||
239 | break; |
||
240 | case TextAlignment::CENTER: |
||
241 | $function="colCenter"; |
||
242 | break; |
||
243 | default:$function="colLeft"; |
||
|
|||
244 | } |
||
245 | $this->colAlign($colIndex, $function); |
||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | private function colAlign($colIndex, $function) { |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Applies a format on each cell when $callback returns true |
||
265 | * @param callable $callback function with the cell as parameter, must return a boolean |
||
266 | * @param string $format css class to apply |
||
267 | * @return HtmlTable |
||
268 | */ |
||
269 | public function conditionalCellFormat($callback, $format) { |
||
270 | $this->getBody()->conditionalCellFormat($callback, $format); |
||
271 | return $this; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Applies a format on each row when $callback returns true |
||
276 | * @param callable $callback function with the row as parameter, must return a boolean |
||
277 | * @param string $format css class to apply |
||
278 | * @return HtmlTable |
||
279 | */ |
||
280 | public function conditionalRowFormat($callback, $format) { |
||
281 | $this->getBody()->conditionalRowFormat($callback, $format); |
||
282 | return $this; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Applies a callback function on each cell |
||
287 | * @param callable $callback |
||
288 | * @return HtmlTable |
||
289 | */ |
||
290 | public function applyCells($callback) { |
||
291 | $this->getBody()->applyCells($callback); |
||
292 | return $this; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Applies a callback function on each row |
||
297 | * @param callable $callback |
||
298 | * @return HtmlTable |
||
299 | */ |
||
300 | public function applyRows($callback) { |
||
301 | $this->getBody()->applyRows($callback); |
||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * |
||
307 | * {@inheritDoc} |
||
308 | * |
||
309 | * @see HtmlSemDoubleElement::compile() |
||
310 | */ |
||
311 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
312 | if(\sizeof($this->_compileParts)<3){ |
||
313 | $this->_template="%content%"; |
||
314 | $this->refresh(); |
||
315 | } |
||
316 | $this->content=JArray::sortAssociative($this->content, $this->_compileParts); |
||
317 | return parent::compile($js, $view); |
||
318 | } |
||
319 | |||
320 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
321 | parent::compile_once($js,$view); |
||
322 | if ($this->propertyContains("class", "sortable")) { |
||
323 | $this->addEvent("execute", "$('#" . $this->identifier . "').tablesort().data('tablesort').sort($('th.default-sort'));"); |
||
324 | } |
||
325 | if(isset($this->_activeRowSelector)){ |
||
326 | $this->_activeRowSelector->compile(); |
||
327 | } |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * |
||
332 | * {@inheritDoc} |
||
333 | * |
||
334 | * @see BaseHtml::fromDatabaseObject() |
||
335 | */ |
||
336 | public function fromDatabaseObject($object, $function) { |
||
337 | $result=$function($object); |
||
338 | if (\is_array($result)) { |
||
339 | $result= $this->addRow($function($object)); |
||
340 | } else { |
||
341 | $result= $this->getBody()->_addRow($result); |
||
342 | } |
||
343 | if(isset($this->_afterCompileEvents["onNewRow"])){ |
||
344 | if(\is_callable($this->_afterCompileEvents["onNewRow"])) |
||
345 | $this->_afterCompileEvents["onNewRow"]($result,$object); |
||
346 | } |
||
347 | return $result; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Sets the parts of the Table to compile |
||
352 | * @param array $parts array of thead,tbody,tfoot |
||
353 | * @return HtmlTable |
||
354 | */ |
||
355 | public function setCompileParts($parts=["tbody"]) { |
||
356 | $this->_compileParts=$parts; |
||
357 | return $this; |
||
358 | } |
||
359 | |||
360 | public function refresh(){ |
||
361 | $this->_footer=$this->getFooter(); |
||
362 | $this->addEvent("execute", '$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");'); |
||
363 | } |
||
364 | |||
365 | public function run(JsUtils $js){ |
||
366 | $result= parent::run($js); |
||
367 | if(isset($this->_footer)) |
||
368 | $this->_footer->run($js); |
||
369 | return $result; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
374 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
375 | * @param callable $callback |
||
376 | * @return HtmlTable |
||
377 | */ |
||
378 | public function onNewRow($callback) { |
||
379 | $this->_afterCompileEvents["onNewRow"]=$callback; |
||
380 | return $this; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Defines how a row is selectable |
||
385 | * @param string $class |
||
386 | * @param string $event |
||
387 | * @param boolean $multiple |
||
388 | * @return HtmlTable |
||
389 | */ |
||
390 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
391 | $this->_activeRowSelector=new ActiveRow($this,$class,$event,$multiple); |
||
392 | return $this; |
||
393 | } |
||
394 | |||
395 | public function hideColumn($colIndex){ |
||
396 | if(isset($this->content["thead"])){ |
||
397 | $this->content["thead"]->hideColumn($colIndex); |
||
398 | } |
||
399 | $this->content["tbody"]->hideColumn($colIndex); |
||
400 | if(isset($this->content["tfoot"])){ |
||
401 | $this->content["tfoot"]->hideColumn($colIndex); |
||
402 | } |
||
403 | return $this; |
||
404 | } |
||
405 | |||
406 | public function setColWidth($colIndex,$width){ |
||
411 | } |
||
412 | |||
413 | public function setColWidths($widths){ |
||
414 | $part=$this->_getFirstPart(); |
||
415 | if($part!==null && $part->count()>0){ |
||
416 | $count=$part->getColCount(); |
||
417 | if(!\is_array($widths)){ |
||
418 | $widths=\array_fill(0, $count, $widths); |
||
419 | } |
||
420 | $max=\min(\sizeof($widths),$count); |
||
421 | for($i=0;$i<$max;$i++){ |
||
426 | } |
||
427 | |||
428 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
429 | $body=$this->getBody(); |
||
430 | $body->mergeIdentiqualValues($colIndex,$function); |
||
431 | return $this; |
||
432 | } |
||
433 | } |
||
434 |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.