HtmlTable   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 46
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 262
rs 8.3999

36 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPart() 0 9 3
A getBody() 0 3 1
A getHeader() 0 3 1
A getFooter() 0 3 1
A hasPart() 0 3 1
A setRowCount() 0 4 1
A getCell() 0 3 1
A getRow() 0 3 1
A addRow() 0 5 1
A newRow() 0 3 1
A setValues() 0 4 1
A setHeaderValues() 0 3 1
A setFooterValues() 0 3 1
A setColValues() 0 4 1
A setRowValues() 0 4 1
A addColVariations() 0 3 1
A colCenter() 0 3 1
A colRight() 0 3 1
A colLeft() 0 3 1
A colAlign() 0 13 4
A setCelled() 0 3 1
A setBasic() 0 5 2
A setCollapsing() 0 3 1
A setDefinition() 0 3 1
A setStructured() 0 3 1
A setSortable() 0 6 3
A setSingleLine() 0 3 1
A setFixed() 0 3 1
A conditionalCellFormat() 0 4 1
A conditionalRowFormat() 0 4 1
A applyCells() 0 4 1
A applyRows() 0 4 1
A setSelectable() 0 3 1
A compile() 0 7 2
A fromDatabaseObject() 0 8 2

How to fix   Complexity   

Complex Class

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\content\table\HtmlTableContent;
7
use Ajax\semantic\html\base\constants\Variation;
8
use Ajax\JsUtils;
9
10
use Ajax\service\JArray;
11
12
/**
13
 * Semantic HTML Table component
14
 * @author jc
15
 *
16
 */
17
class HtmlTable extends HtmlSemDoubleElement {
18
	private $_colCount;
19
20
	public function __construct($identifier, $rowCount, $colCount) {
21
		parent::__construct($identifier, "table", "ui table");
22
		$this->content=array ();
23
		$this->setRowCount($rowCount, $colCount);
24
		$this->_variations=[ Variation::CELLED,Variation::PADDED,Variation::COMPACT ];
25
	}
26
27
	/**
28
	 * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot
29
	 * @param string $key
30
	 * @return HtmlTableContent
31
	 */
32
	private function getPart($key) {
33
		if (\array_key_exists($key, $this->content) === false) {
34
			$this->content[$key]=new HtmlTableContent("", $key);
35
			if ($key !== "tbody") {
36
				$this->content[$key]->setRowCount(1, $this->_colCount);
37
			}
38
		}
39
		return $this->content[$key];
40
	}
41
42
	/**
43
	 * Returns/create eventually the body of the table
44
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
45
	 */
46
	public function getBody() {
47
		return $this->getPart("tbody");
48
	}
49
50
	/**
51
	 * Returns/create eventually the header of the table
52
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
53
	 */
54
	public function getHeader() {
55
		return $this->getPart("thead");
56
	}
57
58
	/**
59
	 * Returns/create eventually the footer of the table
60
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
61
	 */
62
	public function getFooter() {
63
		return $this->getPart("tfoot");
64
	}
65
66
	/**
67
	 * Checks if the part corresponding to $key exists
68
	 * @param string $key
69
	 * @return boolean
70
	 */
71
	public function hasPart($key) {
72
		return \array_key_exists($key, $this->content) === true;
73
	}
74
75
	/**
76
	 *
77
	 * @param int $rowCount
78
	 * @param int $colCount
79
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
80
	 */
81
	public function setRowCount($rowCount, $colCount) {
82
		$this->_colCount=$colCount;
83
		return $this->getBody()->setRowCount($rowCount, $colCount);
84
	}
85
86
	/**
87
	 * Returns the cell (HtmlTD) at position $row,$col
88
	 * @param int $row
89
	 * @param int $col
90
	 * @return \Ajax\semantic\html\content\HtmlTD
91
	 */
92
	public function getCell($row, $col) {
93
		return $this->getBody()->getCell($row, $col);
94
	}
95
96
	/**
97
	 * Retuns the row at $rowIndex
98
	 * @param int $rowIndex
99
	 * @return \Ajax\semantic\html\content\HtmlTR
100
	 */
101
	public function getRow($rowIndex) {
102
		return $this->getBody()->getRow($rowIndex);
103
	}
104
105
	/**
106
	 * Adds a new row and sets $values to his cols
107
	 * @param array $values
108
	 * @return \Ajax\semantic\html\collections\HtmlTable
109
	 */
110
	public function addRow($values=array()) {
111
		$row=$this->getBody()->addRow($this->_colCount);
112
		$row->setValues(\array_values($values));
0 ignored issues
show
Bug introduced by
The method setValues() does not exist on Ajax\common\html\HtmlDoubleElement. Did you maybe mean setValue()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
113
		return $this;
114
	}
115
116
	/**
117
	 * adds and returns a new row
118
	 * @return \Ajax\semantic\html\content\table\HtmlTR
119
	 */
120
	public function newRow() {
121
		return $this->getBody()->newRow($this->_colCount);
122
	}
123
124
	public function setValues($values=array()) {
125
		$this->getBody()->setValues($values);
126
		return $this;
127
	}
128
129
	public function setHeaderValues($values=array()) {
130
		return $this->getHeader()->setValues($values);
131
	}
132
133
	public function setFooterValues($values=array()) {
134
		return $this->getFooter()->setValues($values);
135
	}
136
137
	/**
138
	 * Sets values to the col at index $colIndex
139
	 * @param int $colIndex
140
	 * @param array $values
141
	 * @return \Ajax\semantic\html\collections\HtmlTable
142
	 */
143
	public function setColValues($colIndex, $values=array()) {
144
		$this->getBody()->setColValues($colIndex, $values);
145
		return $this;
146
	}
147
148
	/**
149
	 * Sets values to the row at index $rowIndex
150
	 * @param int $rowIndex
151
	 * @param array $values
152
	 * @return \Ajax\semantic\html\collections\HtmlTable
153
	 */
154
	public function setRowValues($rowIndex, $values=array()) {
155
		$this->getBody()->setRowValues($rowIndex, $values);
156
		return $this;
157
	}
158
159
	public function addColVariations($colIndex, $variations=array()) {
160
		return $this->getBody()->addColVariations($colIndex, $variations);
161
	}
162
163
	public function colCenter($colIndex) {
164
		return $this->colAlign($colIndex, "colCenter");
165
	}
166
167
	public function colRight($colIndex) {
168
		return $this->colAlign($colIndex, "colRight");
169
	}
170
171
	public function colLeft($colIndex) {
172
		return $this->colAlign($colIndex, "colLeft");
173
	}
174
175
	private function colAlign($colIndex, $function) {
176
		if (\is_array($colIndex)) {
177
			foreach ( $colIndex as $cIndex ) {
178
				$this->colAlign($cIndex, $function);
179
			}
180
		} else {
181
			if ($this->hasPart("thead")) {
182
				$this->getHeader()->$function($colIndex);
183
			}
184
			$this->getBody()->$function($colIndex);
185
		}
186
		return $this;
187
	}
188
189
	public function setCelled() {
190
		return $this->addToProperty("class", "celled");
191
	}
192
193
	public function setBasic($very=false) {
194
		if ($very)
195
			$this->addToPropertyCtrl("class", "very", array ("very" ));
196
		return $this->addToPropertyCtrl("class", "basic", array ("basic" ));
197
	}
198
199
	public function setCollapsing() {
200
		return $this->addToProperty("class", "collapsing");
201
	}
202
203
	public function setDefinition() {
204
		return $this->addToProperty("class", "definition");
205
	}
206
207
	public function setStructured() {
208
		return $this->addToProperty("class", "structured");
209
	}
210
211
	public function setSortable($colIndex=NULL) {
212
		if (isset($colIndex) && $this->hasPart("thead")) {
213
			$this->getHeader()->sort($colIndex);
214
		}
215
		return $this->addToProperty("class", "sortable");
216
	}
217
218
	public function setSingleLine() {
219
		return $this->addToProperty("class", "single line");
220
	}
221
222
	public function setFixed() {
223
		return $this->addToProperty("class", "fixed");
224
	}
225
226
	public function conditionalCellFormat($callback, $format) {
227
		$this->getBody()->conditionalCellFormat($callback, $format);
228
		return $this;
229
	}
230
231
	public function conditionalRowFormat($callback, $format) {
232
		$this->getBody()->conditionalRowFormat($callback, $format);
233
		return $this;
234
	}
235
236
	public function applyCells($callback) {
237
		$this->getBody()->applyCells($callback);
238
		return $this;
239
	}
240
241
	public function applyRows($callback) {
242
		$this->getBody()->applyRows($callback);
243
		return $this;
244
	}
245
246
	public function setSelectable() {
247
		return $this->addToProperty("class", "selectable");
248
	}
249
250
	/**
251
	 *
252
	 * {@inheritDoc}
253
	 *
254
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
255
	 */
256
	public function compile(JsUtils $js=NULL, &$view=NULL) {
257
		$this->content=JArray::sortAssociative($this->content, [ "thead","tbody","tfoot" ]);
258
		if ($this->propertyContains("class", "sortable")) {
259
			$this->addEvent("execute", "$('#" . $this->identifier . "').tablesort();");
260
		}
261
		return parent::compile($js, $view);
262
	}
263
264
	/**
265
	 *
266
	 * {@inheritDoc}
267
	 *
268
	 * @see \Ajax\common\html\BaseHtml::fromDatabaseObject()
269
	 */
270
	public function fromDatabaseObject($object, $function) {
271
		$result=$function($object);
272
		if (\is_array($result)) {
273
			return $this->addRow($function($object));
274
		} else {
275
			return $this->getBody()->_addRow($result);
276
		}
277
	}
278
}