Passed
Push — master ( ff68ab...5f63ee )
by Jean-Christophe
03:29
created

HtmlTableContent   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 374
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 374
rs 2.5423
c 0
b 0
f 0
wmc 72

38 Methods

Rating   Name   Duplication   Size   Complexity  
A toDelete() 0 5 2
A getRow() 0 2 1
A colLeft() 0 2 1
A setRowValues() 0 7 2
A hideColumn() 0 7 2
A newRow() 0 2 1
A colAlign() 0 8 3
A colCenter() 0 2 1
A getCell() 0 8 3
A setColValues() 0 10 3
A colRight() 0 2 1
A getTdTagName() 0 2 1
A _addRow() 0 2 1
A applyCells() 0 6 2
A _setMerged() 0 3 1
A setRowCount() 0 6 2
A addRow() 0 2 1
A setCellValue() 0 6 2
A mergeRow() 0 2 1
A conditionalRowFormat() 0 6 2
A conditionalCellFormat() 0 6 2
A getColCount() 0 5 2
A createItem() 0 9 2
C mergeIdentiqualValues() 0 25 8
A __construct() 0 4 3
A delete() 0 10 3
A applyRows() 0 6 2
A getItem() 0 2 1
A conditionalColFormat() 0 7 2
A mergeCol() 0 2 1
A setValues() 0 2 1
A setFullWidth() 0 2 1
B _addOrSetValues() 0 17 5
A addColVariations() 0 6 2
A sort() 0 3 1
A _isMerged() 0 2 1
A addValues() 0 2 1
A getRowCount() 0 2 1

How to fix   Complexity   

Complex Class

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
2
3
namespace Ajax\semantic\html\content\table;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\service\JArray;
7
use Ajax\common\html\HtmlCollection;
8
use Ajax\common\html\HtmlDoubleElement;
9
10
/**
11
 * a table content (thead, tbody or tfoot)
12
 * @author jc
13
 *
14
 */
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);
0 ignored issues
show
Bug introduced by
The method addVariations() does not exist on Ajax\common\html\HtmlDoubleElement. It seems like you code against a sub-type of Ajax\common\html\HtmlDoubleElement such as Ajax\semantic\html\base\HtmlSemDoubleElement or Ajax\semantic\widgets\dataelement\DataElement or Ajax\semantic\widgets\datatable\DataTable or Ajax\semantic\widgets\dataform\DataForm or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\semantic\html\base\HtmlSemCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
			$this->getCell($i, $colIndex)->/** @scrutinizer ignore-call */ addVariations($variations);
Loading history...
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){
264
		$row=$this->getItem($rowIndex);
265
		if (isset($row) === true)
266
			$row->toDelete($colIndex);
267
		return $this;
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){
323
		$rows=$this->content;
324
		foreach ( $rows as $row ) {
325
			$cell=$row->getItem($colIndex);
326
			$cell->addToProperty("style","display:none;");
327
		}
328
		return $this;
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 ) {
338
			$row->applyCells($callback);
339
		}
340
		return $this;
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){
387
		$this->_merged=$value;
388
		return $this;
389
	}
390
}
391