HtmlTableContent   F
last analyzed

Complexity

Total Complexity 91

Size/Duplication

Total Lines 484
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
eloc 184
c 9
b 0
f 1
dl 0
loc 484
rs 2
wmc 91

47 Methods

Rating   Name   Duplication   Size   Complexity  
A toDelete() 0 5 2
A addMergeRow() 0 9 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 12 4
A colCenter() 0 2 1
A colAlignFromRight() 0 11 4
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 colRightFromRight() 0 2 1
A toRowspanned() 0 5 2
A applyCells() 0 6 2
A _setMerged() 0 3 1
A addPropertyCol() 0 8 3
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 12 3
A colLeftFromRight() 0 2 1
A colCenterFromRight() 0 2 1
A __construct() 0 4 3
B mergeIdentiqualValues() 0 25 8
A delete() 0 10 3
A applyRows() 0 6 2
A conditionalColFormat() 0 7 2
A getItem() 0 2 1
A mergeCol() 0 2 1
A setValues() 0 3 1
A setFullWidth() 0 2 1
A refreshTR() 0 2 1
A _addOrSetValues() 0 19 5
A addColVariations() 0 8 3
A sort() 0 3 1
A _isMerged() 0 2 1
A addValues() 0 3 1
A setFocusable() 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
namespace Ajax\semantic\html\content\table;
3
4
use Ajax\semantic\html\base\HtmlSemCollection;
5
use Ajax\service\JArray;
6
use Ajax\common\html\HtmlCollection;
7
use Ajax\common\html\HtmlDoubleElement;
8
use Ajax\semantic\html\base\traits\BaseTrait;
9
10
/**
11
 * a table content (thead, tbody or tfoot)
12
 *
13
 * @author jc
14
 *
15
 */
16
class HtmlTableContent extends HtmlSemCollection {
17
18
	protected $_focusable = false;
19
20
	protected $_tdTagNames = [
21
		"thead" => "th",
22
		"tbody" => "td",
23
		"tfoot" => "th"
24
	];
25
26
	protected $_merged = false;
27
28
	/**
29
	 *
30
	 * @param string $identifier
31
	 * @param string $tagName
32
	 * @param int $rowCount
33
	 * @param int $colCount
34
	 */
35
	public function __construct($identifier, $tagName = "tbody", $rowCount = NULL, $colCount = NULL) {
36
		parent::__construct($identifier, $tagName, "");
37
		if (isset($rowCount) && isset($colCount))
38
			$this->setRowCount($rowCount, $colCount);
39
	}
40
41
	/**
42
	 *
43
	 * @param int $rowCount
44
	 * @param int $colCount
45
	 * @return HtmlTableContent
46
	 */
47
	public function setRowCount($rowCount, $colCount) {
48
		$count = $this->count();
49
		for ($i = $count; $i < $rowCount; $i ++) {
50
			$this->addItem($colCount);
51
		}
52
		return $this;
53
	}
54
55
	public function getTdTagName($tagName) {
56
		return $this->_tdTagNames[$this->tagName];
57
	}
58
59
	public function refreshTR() {
60
		$this->_template = "%wrapContentBefore%%content%%wrapContentAfter%";
61
	}
62
63
	/**
64
	 *
65
	 * {@inheritdoc}
66
	 *
67
	 * @see \Ajax\common\html\HtmlCollection::createItem()
68
	 * @return HtmlTR
69
	 */
70
	protected function createItem($value) {
71
		$count = $this->count();
72
		$tr = new HtmlTR("");
73
		$tr->setContainer($this, $count);
74
		if ($this->_focusable) {
75
			$tr->setProperty('tabindex', $count);
76
		}
77
		$tr->setTdTagName($this->_tdTagNames[$this->tagName]);
78
		if (isset($value) === true) {
79
			$tr->setColCount($value);
80
		}
81
		return $tr;
82
	}
83
84
	public function newRow($value) {
85
		return $this->createItem($value);
86
	}
87
88
	/**
89
	 *
90
	 * @param int $colCount
91
	 * @return HtmlTR
92
	 */
93
	public function addRow($colCount) {
94
		return $this->addItem($colCount);
95
	}
96
97
	/**
98
	 *
99
	 * @param mixed $row
100
	 * @return HtmlTR
101
	 */
102
	public function _addRow($row) {
103
		return $this->addItem($row);
104
	}
105
106
	public function addMergeRow($colCount, $value = null) {
107
		$row = $this->addRow($colCount);
108
		$row->mergeCol();
109
		if (isset($value)) {
110
			$row->setValues([
111
				$value
112
			]);
113
		}
114
		return $row;
115
	}
116
117
	/**
118
	 *
119
	 * @return HtmlTR
120
	 */
121
	public function getItem($index) {
122
		return parent::getItem($index);
123
	}
124
125
	/**
126
	 * Returns the cell (HtmlTD) at position $row,$col
127
	 *
128
	 * @param int $row
129
	 * @param int $col
130
	 * @return HtmlTD|HtmlDoubleElement
131
	 */
132
	public function getCell($row, $col) {
133
		$row = $this->getItem($row);
134
		if (isset($row) && $row instanceof HtmlCollection) {
135
			$col = $row->getItem($col);
136
		} else {
137
			$col = $row;
138
		}
139
		return $col;
140
	}
141
142
	/**
143
	 *
144
	 * @param int $index
145
	 * @return HtmlTR
146
	 */
147
	public function getRow($index) {
148
		return $this->getItem($index);
149
	}
150
151
	/**
152
	 *
153
	 * @param int $row
154
	 * @param int $col
155
	 * @param mixed $value
156
	 * @return HtmlTableContent
157
	 */
158
	public function setCellValue($row, $col, $value = "") {
159
		$cell = $this->getCell($row, $col);
160
		if (isset($cell) === true) {
161
			$cell->setValue($value);
162
		}
163
		return $this;
164
	}
165
166
	/**
167
	 * Sets the cells values
168
	 *
169
	 * @param mixed $values
170
	 */
171
	public function setValues($values = array()) {
172
		return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) {
173
			$row->setValues($_values);
174
		});
175
	}
176
177
	/**
178
	 * Adds the cells values
179
	 *
180
	 * @param mixed $values
181
	 */
182
	public function addValues($values = array()) {
183
		return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) {
184
			$row->addValues($_values);
185
		});
186
	}
187
188
	/**
189
	 * Adds or sets the cells values
190
	 *
191
	 * @param mixed $values
192
	 * @param callable $callback
193
	 */
194
	protected function _addOrSetValues($values, $callback) {
195
		$count = $this->count();
196
		$isArray = true;
197
		if (! \is_array($values)) {
198
			$values = \array_fill(0, $count, $values);
199
			$isArray = false;
200
		}
201
		if (JArray::dimension($values) == 1 && $isArray)
202
			$values = [
203
				$values
204
			];
205
206
		$count = \min(\sizeof($values), $count);
207
208
		for ($i = 0; $i < $count; $i ++) {
209
			$row = $this->content[$i];
210
			$callback($row, $values[$i]);
211
		}
212
		return $this;
213
	}
214
215
	public function setColValues($colIndex, $values = array()) {
216
		$count = $this->count();
217
		if (! \is_array($values)) {
218
			$values = \array_fill(0, $count, $values);
219
		}
220
		$count = \min(\sizeof($values), $count);
221
		for ($i = 0; $i < $count; $i ++) {
222
			$this->getCell($i, $colIndex)->setValue($values[$i]);
223
		}
224
		return $this;
225
	}
226
227
	public function addColVariations($colIndex, $variations = array()) {
228
		$count = $this->count();
229
		for ($i = 0; $i < $count; $i ++) {
230
			$cell = $this->getCell($i, $colIndex);
231
			if ($cell instanceof BaseTrait)
232
				$cell->addVariations($variations);
233
		}
234
		return $this;
235
	}
236
237
	public function addPropertyCol($colIndex, $name, $value) {
238
		$count = $this->count();
239
		for ($i = 0; $i < $count; $i ++) {
240
			$cell = $this->getCell($i, $colIndex);
241
			if (isset($cell))
242
				$cell->addToProperty($name, $value);
243
		}
244
		return $this;
245
	}
246
247
	public function setRowValues($rowIndex, $values = array()) {
248
		$count = $this->count();
249
		if (! \is_array($values)) {
250
			$values = \array_fill(0, $count, $values);
251
		}
252
		$this->getItem($rowIndex)->setValues($values);
253
		return $this;
254
	}
255
256
	private function colAlign($colIndex, $function) {
257
		$count = $this->count();
258
		for ($i = 0; $i < $count; $i ++) {
259
			$index = $this->content[$i]->getColPosition($colIndex);
260
			if ($index !== NULL) {
261
				$cell = $this->getCell($i, $index);
262
				if ($cell != NULL) {
263
					$cell->$function();
264
				}
265
			}
266
		}
267
		return $this;
268
	}
269
270
	private function colAlignFromRight($colIndex, $function) {
271
		$count = $this->count();
272
		for ($i = 0; $i < $count; $i ++) {
273
			$maxRow = $this->content[$i]->count();
274
			$index = $maxRow - $colIndex - 1;
275
			if (($cell = $this->getCell($i, $index)) !== NULL) {
276
				if ($cell->getColspan() == 1)
0 ignored issues
show
Bug introduced by
The method getColspan() 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\content\table\HtmlTD or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\semantic\html\base\HtmlSemNavElement. ( Ignorable by Annotation )

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

276
				if ($cell->/** @scrutinizer ignore-call */ getColspan() == 1)
Loading history...
277
					$cell->$function();
278
			}
279
		}
280
		return $this;
281
	}
282
283
	public function colCenter($colIndex) {
284
		return $this->colAlign($colIndex, "textCenterAligned");
285
	}
286
287
	public function colRight($colIndex) {
288
		return $this->colAlign($colIndex, "textRightAligned");
289
	}
290
291
	public function colLeft($colIndex) {
292
		return $this->colAlign($colIndex, "textLeftAligned");
293
	}
294
295
	public function colCenterFromRight($colIndex) {
296
		return $this->colAlignFromRight($colIndex, "textCenterAligned");
297
	}
298
299
	public function colRightFromRight($colIndex) {
300
		return $this->colAlignFromRight($colIndex, "textRightAligned");
301
	}
302
303
	public function colLeftFromRight($colIndex) {
304
		return $this->colAlignFromRight($colIndex, "textLeftAligned");
305
	}
306
307
	/**
308
	 * Returns the number of rows (TR)
309
	 *
310
	 * @return int
311
	 */
312
	public function getRowCount() {
313
		return $this->count();
314
	}
315
316
	/**
317
	 * Returns the number of columns (TD)
318
	 *
319
	 * @return int
320
	 */
321
	public function getColCount() {
322
		$result = 0;
323
		if ($this->count() > 0)
324
			$result = $this->getItem(0)->count();
325
		return $result;
326
	}
327
328
	/**
329
	 * Removes the cell at position $rowIndex,$colIndex
330
	 *
331
	 * @param int $rowIndex
332
	 * @param int $colIndex
333
	 * @return HtmlTableContent
334
	 */
335
	public function delete($rowIndex, $colIndex = NULL) {
336
		if (isset($colIndex)) {
337
			$row = $this->getItem($rowIndex);
338
			if (isset($row) === true) {
339
				$row->delete($colIndex);
340
			}
341
		} else {
342
			$this->removeItem($rowIndex);
343
		}
344
		return $this;
345
	}
346
347
	public function toDelete($rowIndex, $colIndex) {
348
		$row = $this->getItem($rowIndex);
349
		if (isset($row) === true)
350
			$row->toDelete($colIndex);
351
		return $this;
352
	}
353
354
	public function toRowspanned($rowIndex, $colIndex) {
355
		$row = $this->getItem($rowIndex);
356
		if (isset($row) === true)
357
			$row->toRowspanned($colIndex);
358
		return $this;
359
	}
360
361
	public function mergeCol($rowIndex = 0, $colIndex = 0) {
362
		return $this->getItem($rowIndex)->mergeCol($colIndex);
363
	}
364
365
	public function mergeRow($rowIndex = 0, $colIndex = 0) {
366
		return $this->getItem($rowIndex)->mergeRow($colIndex);
367
	}
368
369
	public function setFullWidth() {
370
		return $this->addToProperty("class", "full-width");
371
	}
372
373
	public function sort($colIndex) {
374
		$this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort");
375
		return $this;
376
	}
377
378
	/**
379
	 *
380
	 * @param mixed $callback
381
	 * @param string $format
382
	 * @return HtmlTableContent
383
	 */
384
	public function conditionalCellFormat($callback, $format) {
385
		$rows = $this->content;
386
		foreach ($rows as $row) {
387
			$row->conditionalCellFormat($callback, $format);
388
		}
389
		return $this;
390
	}
391
392
	public function conditionalColFormat($colIndex, $callback, $format) {
393
		$rows = $this->content;
394
		foreach ($rows as $row) {
395
			$cell = $row->getItem($colIndex);
396
			$cell->conditionnalCellFormat($callback, $format);
397
		}
398
		return $this;
399
	}
400
401
	/**
402
	 *
403
	 * @param mixed $callback
404
	 * @param string $format
405
	 * @return HtmlTableContent
406
	 */
407
	public function conditionalRowFormat($callback, $format) {
408
		$rows = $this->content;
409
		foreach ($rows as $row) {
410
			$row->conditionalRowFormat($callback, $format);
411
		}
412
		return $this;
413
	}
414
415
	public function hideColumn($colIndex) {
416
		$rows = $this->content;
417
		foreach ($rows as $row) {
418
			$cell = $row->getItem($colIndex);
419
			$cell->addToProperty("style", "display:none;");
420
		}
421
		return $this;
422
	}
423
424
	/**
425
	 *
426
	 * @param mixed $callback
427
	 * @return HtmlTableContent
428
	 */
429
	public function applyCells($callback) {
430
		$rows = $this->content;
431
		foreach ($rows as $row) {
432
			$row->applyCells($callback);
433
		}
434
		return $this;
435
	}
436
437
	/**
438
	 *
439
	 * @param mixed $callback
440
	 * @return HtmlTableContent
441
	 */
442
	public function applyRows($callback) {
443
		$rows = $this->content;
444
		foreach ($rows as $row) {
445
			$row->apply($callback);
446
		}
447
		return $this;
448
	}
449
450
	/**
451
	 *
452
	 * @param int $colIndex
453
	 * @param string $function
454
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
455
	 * @see https://fomantic-ui.com/collections/table.html#definition needs rowspanned class
456
	 * @since fomantic-ui 2.4.8
457
	 */
458
	public function mergeIdentiqualValues($colIndex, $function = "strip_tags") {
459
		$rows = $this->content;
460
		$identiqual = null;
461
		$counter = 0;
462
		$cellToMerge = null;
463
		$functionExists = \function_exists($function);
464
		foreach ($rows as $row) {
465
			$cell = $row->getItem($colIndex);
466
			$value = $cell->getContent();
467
			if ($functionExists)
468
				$value = \call_user_func($function, $value);
469
			if ($value !== $identiqual) {
470
				if ($counter > 0 && isset($cellToMerge)) {
471
					$cellToMerge->setRowspanned($counter);
472
				}
473
				$counter = 0;
474
				$cellToMerge = $cell;
475
				$identiqual = $value;
476
			}
477
			$counter ++;
478
		}
479
		if ($counter > 0 && isset($cellToMerge)) {
480
			$cellToMerge->setRowspanned($counter);
481
		}
482
		return $this;
483
	}
484
485
	public function _isMerged() {
486
		return $this->_merged;
487
	}
488
489
	public function _setMerged($value) {
490
		$this->_merged = $value;
491
		return $this;
492
	}
493
494
	/**
495
	 *
496
	 * @param bool $focusable
497
	 */
498
	public function setFocusable(bool $focusable): void {
499
		$this->_focusable = $focusable;
500
	}
501
}
502