Completed
Push — master ( e9bdcb...b0560a )
by Jean-Christophe
04:52
created

HtmlTable   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 46
c 6
b 0
f 0
lcom 1
cbo 3
dl 0
loc 236
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
use Phalcon\Mvc\View;
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
	public function getRow($rowIndex) {
97
		return $this->getBody()->getRow($rowIndex);
98
	}
99
100
	public function addRow($values=array()) {
101
		$row=$this->getBody()->addRow($this->_colCount);
102
		$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...
103
		return $this;
104
	}
105
106
	public function newRow() {
107
		return $this->getBody()->newRow($this->_colCount);
108
	}
109
110
	public function setValues($values=array()) {
111
		$this->getBody()->setValues($values);
112
		return $this;
113
	}
114
115
	public function setHeaderValues($values=array()) {
116
		return $this->getHeader()->setValues($values);
117
	}
118
119
	public function setFooterValues($values=array()) {
120
		return $this->getFooter()->setValues($values);
121
	}
122
123
	public function setColValues($colIndex, $values=array()) {
124
		$this->getBody()->setColValues($colIndex, $values);
125
		return $this;
126
	}
127
128
	public function setRowValues($rowIndex, $values=array()) {
129
		$this->getBody()->setRowValues($rowIndex, $values);
130
		return $this;
131
	}
132
133
	public function addColVariations($colIndex, $variations=array()) {
134
		return $this->getBody()->addColVariations($colIndex, $variations);
135
	}
136
137
	public function colCenter($colIndex) {
138
		return $this->colAlign($colIndex, "colCenter");
139
	}
140
141
	public function colRight($colIndex) {
142
		return $this->colAlign($colIndex, "colRight");
143
	}
144
145
	public function colLeft($colIndex) {
146
		return $this->colAlign($colIndex, "colLeft");
147
	}
148
149
	private function colAlign($colIndex, $function) {
150
		if (\is_array($colIndex)) {
151
			foreach ( $colIndex as $cIndex ) {
152
				$this->colAlign($cIndex, $function);
153
			}
154
		} else {
155
			if ($this->hasPart("thead")) {
156
				$this->getHeader()->$function($colIndex);
157
			}
158
			$this->getBody()->$function($colIndex);
159
		}
160
		return $this;
161
	}
162
163
	public function setCelled() {
164
		return $this->addToProperty("class", "celled");
165
	}
166
167
	public function setBasic($very=false) {
168
		if ($very)
169
			$this->addToPropertyCtrl("class", "very", array ("very" ));
170
		return $this->addToPropertyCtrl("class", "basic", array ("basic" ));
171
	}
172
173
	public function setCollapsing() {
174
		return $this->addToProperty("class", "collapsing");
175
	}
176
177
	public function setDefinition() {
178
		return $this->addToProperty("class", "definition");
179
	}
180
181
	public function setStructured() {
182
		return $this->addToProperty("class", "structured");
183
	}
184
185
	public function setSortable($colIndex=NULL) {
186
		if (isset($colIndex) && $this->hasPart("thead")) {
187
			$this->getHeader()->sort($colIndex);
188
		}
189
		return $this->addToProperty("class", "sortable");
190
	}
191
192
	public function setSingleLine() {
193
		return $this->addToProperty("class", "single line");
194
	}
195
196
	public function setFixed() {
197
		return $this->addToProperty("class", "fixed");
198
	}
199
200
	public function conditionalCellFormat($callback, $format) {
201
		$this->getBody()->conditionalCellFormat($callback, $format);
202
		return $this;
203
	}
204
205
	public function conditionalRowFormat($callback, $format) {
206
		$this->getBody()->conditionalRowFormat($callback, $format);
207
		return $this;
208
	}
209
210
	public function applyCells($callback) {
211
		$this->getBody()->applyCells($callback);
212
		return $this;
213
	}
214
215
	public function applyRows($callback) {
216
		$this->getBody()->applyRows($callback);
217
		return $this;
218
	}
219
220
	public function setSelectable() {
221
		return $this->addToProperty("class", "selectable");
222
	}
223
224
	/**
225
	 *
226
	 * {@inheritDoc}
227
	 *
228
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
229
	 */
230
	public function compile(JsUtils $js=NULL, View $view=NULL) {
231
		$this->content=JArray::sortAssociative($this->content, [ "thead","tbody","tfoot" ]);
232
		if ($this->propertyContains("class", "sortable")) {
233
			$this->addEvent("execute", "$('#" . $this->identifier . "').tablesort();");
234
		}
235
		return parent::compile($js, $view);
236
	}
237
238
	/**
239
	 *
240
	 * {@inheritDoc}
241
	 *
242
	 * @see \Ajax\common\html\BaseHtml::fromDatabaseObject()
243
	 */
244
	public function fromDatabaseObject($object, $function) {
245
		$result=$function($object);
246
		if (\is_array($result)) {
247
			return $this->addRow($function($object));
248
		} else {
249
			return $this->getBody()->_addRow($result);
250
		}
251
	}
252
}