Total Complexity | 82 |
Total Lines | 410 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HtmlTableContent 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 HtmlTableContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class HtmlTableContent extends HtmlSemCollection { |
||
17 | protected $_tdTagNames=[ "thead" => "th","tbody" => "td","tfoot" => "th" ]; |
||
18 | protected $_merged=false; |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | * @param string $identifier |
||
23 | * @param string $tagName |
||
24 | * @param int $rowCount |
||
25 | * @param int $colCount |
||
26 | */ |
||
27 | public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) { |
||
28 | parent::__construct($identifier, $tagName, ""); |
||
29 | if (isset($rowCount) && isset($colCount)) |
||
30 | $this->setRowCount($rowCount, $colCount); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @param int $rowCount |
||
36 | * @param int $colCount |
||
37 | * @return HtmlTableContent |
||
38 | */ |
||
39 | public function setRowCount($rowCount, $colCount) { |
||
40 | $count=$this->count(); |
||
41 | for($i=$count; $i < $rowCount; $i++) { |
||
42 | $this->addItem($colCount); |
||
43 | } |
||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | public function getTdTagName($tagName) { |
||
48 | return $this->_tdTagNames[$this->tagName]; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * |
||
53 | * {@inheritDoc} |
||
54 | * |
||
55 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
56 | * @return HtmlTR |
||
57 | */ |
||
58 | protected function createItem($value) { |
||
59 | $count=$this->count(); |
||
60 | $tr=new HtmlTR(""); |
||
61 | $tr->setContainer($this, $count); |
||
62 | $tr->setTdTagName($this->_tdTagNames[$this->tagName]); |
||
63 | if (isset($value) === true) { |
||
64 | $tr->setColCount($value); |
||
65 | } |
||
66 | return $tr; |
||
67 | } |
||
68 | |||
69 | public function newRow($value) { |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param int $colCount |
||
75 | * @return HtmlTR |
||
76 | */ |
||
77 | public function addRow($colCount) { |
||
78 | return $this->addItem($colCount); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param mixed $row |
||
83 | * @return HtmlTR |
||
84 | */ |
||
85 | public function _addRow($row) { |
||
87 | } |
||
88 | |||
89 | public function addMergeRow($colCount,$value=null){ |
||
90 | $row=$this->addRow($colCount); |
||
91 | $row->mergeCol(); |
||
92 | if(isset($value)){ |
||
93 | $row->setValues([$value]); |
||
94 | } |
||
95 | return $row; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return HtmlTR |
||
100 | */ |
||
101 | public function getItem($index){ |
||
102 | return parent::getItem($index); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Returns the cell (HtmlTD) at position $row,$col |
||
107 | * @param int $row |
||
108 | * @param int $col |
||
109 | * @return HtmlTD|HtmlDoubleElement |
||
110 | */ |
||
111 | public function getCell($row, $col) { |
||
112 | $row=$this->getItem($row); |
||
113 | if (isset($row) && $row instanceof HtmlCollection) { |
||
114 | $col=$row->getItem($col); |
||
115 | }else{ |
||
116 | $col=$row; |
||
117 | } |
||
118 | return $col; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * |
||
123 | * @param int $index |
||
124 | * @return HtmlTR |
||
125 | */ |
||
126 | public function getRow($index) { |
||
127 | return $this->getItem($index); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * |
||
132 | * @param int $row |
||
133 | * @param int $col |
||
134 | * @param mixed $value |
||
135 | * @return HtmlTableContent |
||
136 | */ |
||
137 | public function setCellValue($row, $col, $value="") { |
||
138 | $cell=$this->getCell($row, $col); |
||
139 | if (isset($cell) === true) { |
||
140 | $cell->setValue($value); |
||
141 | } |
||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Sets the cells values |
||
147 | * @param mixed $values |
||
148 | */ |
||
149 | public function setValues($values=array()) { |
||
150 | return $this->_addOrSetValues($values, function(HtmlTR $row,$_values){$row->setValues($_values);}); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Adds the cells values |
||
155 | * @param mixed $values |
||
156 | */ |
||
157 | public function addValues($values=array()) { |
||
158 | return $this->_addOrSetValues($values, function(HtmlTR $row,$_values){$row->addValues($_values);}); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Adds or sets the cells values |
||
163 | * @param mixed $values |
||
164 | * @param callable $callback |
||
165 | */ |
||
166 | protected function _addOrSetValues($values,$callback) { |
||
167 | $count=$this->count(); |
||
168 | $isArray=true; |
||
169 | if (!\is_array($values)) { |
||
170 | $values=\array_fill(0, $count, $values); |
||
171 | $isArray=false; |
||
172 | } |
||
173 | if (JArray::dimension($values) == 1 && $isArray) |
||
174 | $values=[ $values ]; |
||
175 | |||
176 | $count=\min(\sizeof($values), $count); |
||
177 | |||
178 | for($i=0; $i < $count; $i++) { |
||
179 | $row=$this->content[$i]; |
||
180 | $callback($row,$values[$i]); |
||
181 | } |
||
182 | return $this; |
||
183 | } |
||
184 | |||
185 | public function setColValues($colIndex, $values=array()) { |
||
186 | $count=$this->count(); |
||
187 | if (!\is_array($values)) { |
||
188 | $values=\array_fill(0, $count, $values); |
||
189 | } |
||
190 | $count=\min(\sizeof($values), $count); |
||
191 | for($i=0; $i < $count; $i++) { |
||
192 | $this->getCell($i, $colIndex)->setValue($values[$i]); |
||
193 | } |
||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | public function addColVariations($colIndex, $variations=array()) { |
||
198 | $count=$this->count(); |
||
199 | for($i=0; $i < $count; $i++) { |
||
200 | $cell=$this->getCell($i, $colIndex); |
||
201 | if($cell instanceof BaseTrait) |
||
202 | $cell->addVariations($variations); |
||
203 | } |
||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | public function setRowValues($rowIndex, $values=array()) { |
||
208 | $count=$this->count(); |
||
209 | if (!\is_array($values)) { |
||
210 | $values=\array_fill(0, $count, $values); |
||
211 | } |
||
212 | $this->getItem($rowIndex)->setValues($values); |
||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | private function colAlign($colIndex, $function) { |
||
217 | $count=$this->count(); |
||
218 | for($i=0; $i < $count; $i++) { |
||
219 | $index=$this->content[$i]->getColPosition($colIndex); |
||
220 | if ($index !== NULL) |
||
221 | $this->getCell($i, $index)->$function(); |
||
222 | } |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | private function colAlignFromRight($colIndex, $function) { |
||
227 | $count=$this->count(); |
||
228 | for($i=0; $i < $count; $i++) { |
||
229 | $maxRow=$this->content[$i]->count(); |
||
230 | $index=$maxRow-$colIndex-1; |
||
231 | if (($cell=$this->getCell($i, $index))!== NULL){ |
||
232 | if($cell->getColspan()==1) |
||
|
|||
233 | $cell->$function(); |
||
234 | } |
||
235 | } |
||
236 | return $this; |
||
237 | } |
||
238 | |||
239 | public function colCenter($colIndex) { |
||
240 | return $this->colAlign($colIndex, "textCenterAligned"); |
||
241 | } |
||
242 | |||
243 | public function colRight($colIndex) { |
||
244 | return $this->colAlign($colIndex, "textRightAligned"); |
||
245 | } |
||
246 | |||
247 | public function colLeft($colIndex) { |
||
248 | return $this->colAlign($colIndex, "textLeftAligned"); |
||
249 | } |
||
250 | |||
251 | public function colCenterFromRight($colIndex) { |
||
252 | return $this->colAlignFromRight($colIndex, "textCenterAligned"); |
||
253 | } |
||
254 | |||
255 | public function colRightFromRight($colIndex) { |
||
256 | return $this->colAlignFromRight($colIndex, "textRightAligned"); |
||
257 | } |
||
258 | |||
259 | public function colLeftFromRight($colIndex) { |
||
260 | return $this->colAlignFromRight($colIndex, "textLeftAligned"); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns the number of rows (TR) |
||
265 | * @return int |
||
266 | */ |
||
267 | public function getRowCount() { |
||
268 | return $this->count(); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Returns the number of columns (TD) |
||
273 | * @return int |
||
274 | */ |
||
275 | public function getColCount() { |
||
276 | $result=0; |
||
277 | if ($this->count() > 0) |
||
278 | $result=$this->getItem(0)->count(); |
||
279 | return $result; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Removes the cell at position $rowIndex,$colIndex |
||
284 | * @param int $rowIndex |
||
285 | * @param int $colIndex |
||
286 | * @return HtmlTableContent |
||
287 | */ |
||
288 | public function delete($rowIndex, $colIndex=NULL) { |
||
289 | if (isset($colIndex)) { |
||
290 | $row=$this->getItem($rowIndex); |
||
291 | if (isset($row) === true) { |
||
292 | $row->delete($colIndex); |
||
293 | } |
||
294 | } else { |
||
295 | $this->removeItem($rowIndex); |
||
296 | } |
||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | public function toDelete($rowIndex, $colIndex){ |
||
305 | } |
||
306 | |||
307 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
308 | return $this->getItem($rowIndex)->mergeCol($colIndex); |
||
309 | } |
||
310 | |||
311 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
312 | return $this->getItem($rowIndex)->mergeRow($colIndex); |
||
313 | } |
||
314 | |||
315 | public function setFullWidth() { |
||
316 | return $this->addToProperty("class", "full-width"); |
||
317 | } |
||
318 | |||
319 | public function sort($colIndex) { |
||
320 | $this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort"); |
||
321 | return $this; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param mixed $callback |
||
326 | * @param string $format |
||
327 | * @return HtmlTableContent |
||
328 | */ |
||
329 | public function conditionalCellFormat($callback, $format) { |
||
330 | $rows=$this->content; |
||
331 | foreach ( $rows as $row ) { |
||
332 | $row->conditionalCellFormat($callback, $format); |
||
333 | } |
||
334 | return $this; |
||
335 | } |
||
336 | |||
337 | public function conditionalColFormat($colIndex,$callback,$format){ |
||
338 | $rows=$this->content; |
||
339 | foreach ( $rows as $row ) { |
||
340 | $cell=$row->getItem($colIndex); |
||
341 | $cell->conditionnalCellFormat($callback,$format); |
||
342 | } |
||
343 | return $this; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param mixed $callback |
||
348 | * @param string $format |
||
349 | * @return HtmlTableContent |
||
350 | */ |
||
351 | public function conditionalRowFormat($callback, $format) { |
||
352 | $rows=$this->content; |
||
353 | foreach ( $rows as $row ) { |
||
354 | $row->conditionalRowFormat($callback, $format); |
||
355 | } |
||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | public function hideColumn($colIndex){ |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param mixed $callback |
||
370 | * @return HtmlTableContent |
||
371 | */ |
||
372 | public function applyCells($callback) { |
||
373 | $rows=$this->content; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @param mixed $callback |
||
382 | * @return HtmlTableContent |
||
383 | */ |
||
384 | public function applyRows($callback) { |
||
385 | $rows=$this->content; |
||
386 | foreach ( $rows as $row ) { |
||
387 | $row->apply($callback); |
||
388 | } |
||
389 | return $this; |
||
390 | } |
||
391 | |||
392 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
393 | $rows=$this->content; |
||
394 | $identiqual=null; |
||
395 | $counter=0; |
||
396 | $cellToMerge=null; |
||
397 | $functionExists=\function_exists($function); |
||
398 | foreach ( $rows as $row ) { |
||
399 | $cell=$row->getItem($colIndex); |
||
400 | $value=$cell->getContent(); |
||
401 | if($functionExists) |
||
402 | $value=\call_user_func($function,$value); |
||
403 | if($value!==$identiqual){ |
||
404 | if($counter>0 && isset($cellToMerge)){ |
||
405 | $cellToMerge->setRowspan($counter); |
||
406 | } |
||
407 | $counter=0; |
||
408 | $cellToMerge=$cell; |
||
409 | $identiqual=$value; |
||
410 | } |
||
411 | $counter++; |
||
412 | } |
||
413 | if($counter>0 && isset($cellToMerge)){ |
||
414 | $cellToMerge->setRowspan($counter); |
||
415 | } |
||
416 | return $this; |
||
417 | } |
||
418 | |||
419 | public function _isMerged(){ |
||
420 | return $this->_merged; |
||
421 | } |
||
422 | |||
423 | public function _setMerged($value){ |
||
426 | } |
||
427 | } |
||
428 |