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