HtmlGrid   F
last analyzed

Complexity

Total Complexity 62

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 109
c 5
b 1
f 0
dl 0
loc 316
rs 3.44
wmc 62

28 Methods

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 2 1
A asContainer() 0 2 1
A setEqualWidth() 0 2 1
A setVerticalAlignment() 0 2 1
A setWidth() 0 2 1
A getRow() 0 2 1
A setValues() 0 12 5
A addDivider() 0 5 2
A setCelled() 0 3 2
B setColsCount() 0 17 7
A colCount() 0 7 3
A setRowsCount() 0 19 6
A createItem() 0 5 2
A setCentered() 0 2 1
A setDivided() 0 3 2
A setPadded() 0 4 2
A asSegment() 0 2 1
A addRow() 0 4 1
A setColWidth() 0 5 2
A hasOnlyCols() 0 2 2
A __construct() 0 10 3
A rowCount() 0 5 2
A addCols() 0 5 2
A setStretched() 0 2 1
A getCell() 0 8 4
A setWide() 0 7 2
A setRelaxed() 0 3 2
A addCol() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like HtmlGrid 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 HtmlGrid, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\semantic\html\content\HtmlGridRow;
6
use Ajax\semantic\html\base\constants\Wide;
7
use Ajax\semantic\html\base\constants\VerticalAlignment;
8
use Ajax\semantic\html\base\HtmlSemCollection;
9
use Ajax\semantic\html\base\traits\TextAlignmentTrait;
10
use Ajax\semantic\html\content\HtmlGridCol;
11
12
/**
13
 * Semantic Grid component
14
 * @see http://semantic-ui.com/collections/grid.html
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlGrid extends HtmlSemCollection {
19
	use TextAlignmentTrait;
20
	private $_createCols;
21
	private $_colSizing=true;
22
	private $_implicitRows=false;
23
24
	public function __construct($identifier, $numRows=1, $numCols=NULL, $createCols=true, $implicitRows=false) {
25
		parent::__construct($identifier, "div", "ui grid");
26
		$this->_implicitRows=$implicitRows;
27
		$this->_createCols=$createCols;
28
		if (isset($numCols)) {
29
			$this->_colSizing=false;
30
			$this->setWide($numCols);
31
		}
32
		if($createCols)
33
			$this->setRowsCount($numRows, $numCols);
34
	}
35
36
	public function asSegment() {
37
		return $this->addToPropertyCtrl("class", "segment", array ("segment" ));
38
	}
39
40
	public function asContainer() {
41
		return $this->addToPropertyCtrl("class", "container", array ("container" ));
42
	}
43
44
	/**
45
	 * Defines the grid width (alias for setWidth)
46
	 * @param int $wide
47
	 */
48
	public function setWide($wide) {
49
		if(isset(Wide::getConstants()["W" . $wide])){
50
			$wide=Wide::getConstants()["W" . $wide];
51
			$this->addToPropertyCtrl("class", $wide, Wide::getConstants());
52
			return $this->addToPropertyCtrl("class", "column", array ("column" ));
53
		}
54
		return $this;
55
	}
56
57
	/**
58
	 * Defines the grid width
59
	 * @param int $width
60
	 * @return \Ajax\semantic\html\collections\HtmlGrid
61
	 */
62
	public function setWidth($width) {
63
		return $this->setWide($width);
64
	}
65
66
	/**
67
	 * Adds a row with $colsCount columns
68
	 * @param int $colsCount number of columns to create
69
	 * @return mixed
70
	 */
71
	public function addRow($colsCount=NULL) {
72
		$rowCount=$this->rowCount() + 1;
73
		$this->setRowsCount($rowCount, $colsCount, true);
74
		return $this->content[$rowCount - 1];
75
	}
76
77
	/**
78
	 * Adds a col
79
	 * @param int $width with of the column to add
80
	 * @return mixed|\Ajax\semantic\html\collections\HtmlGrid
81
	 */
82
	public function addCol($width=NULL) {
83
		$colCount=$this->colCount() + 1;
84
		$this->setColsCount($colCount, true, $width);
85
		if ($this->hasOnlyCols($this->count()))
86
			return $this->content[$colCount - 1];
87
		return $this;
88
	}
89
90
	/**
91
	 *
92
	 * @param array $sizes array of width of the columns to create
93
	 * @return \Ajax\semantic\html\collections\HtmlGrid
94
	 */
95
	public function addCols($sizes=array()) {
96
		foreach ( $sizes as $size ) {
97
			$this->addCol($size);
98
		}
99
		return $this;
100
	}
101
102
	/**
103
	 * Create $rowsCount rows
104
	 * @param int $rowsCount
105
	 * @param int $colsCount
106
	 * @return \Ajax\semantic\html\collections\HtmlGrid
107
	 */
108
	public function setRowsCount($rowsCount, $colsCount=NULL, $force=false) {
109
		$count=$this->count();
110
		if ($rowsCount < 2 && $force === false) {
111
			for($i=$count; $i < $colsCount; $i++) {
112
				$this->addItem(new HtmlGridCol("col-" . $this->identifier . "-" . $i));
113
			}
114
		} else {
115
			if ($this->hasOnlyCols($count)) {
116
				$tmpContent=$this->content;
117
				$item=$this->addItem($colsCount);
118
				$item->setContent($tmpContent);
119
				$this->content=array ();
120
				$count=1;
121
			}
122
			for($i=$count; $i < $rowsCount; $i++) {
123
				$this->addItem($colsCount);
124
			}
125
		}
126
		return $this;
127
	}
128
129
	protected function hasOnlyCols($count) {
130
		return $count > 0 && $this->content[0] instanceof HtmlGridCol;
131
	}
132
133
	/**
134
	 * Defines the number of columns in the grid
135
	 * @param int $numCols
136
	 * @param boolean $toCreate
137
	 * @param int $width
138
	 * @return \Ajax\semantic\html\collections\HtmlGrid
139
	 */
140
	public function setColsCount($numCols, $toCreate=true, $width=NULL) {
141
		if (isset($width)===false) {
142
			$this->setWide($numCols);
143
		}
144
		if ($toCreate === true) {
145
			$count=$this->count();
146
			if ($count == 0 || $this->hasOnlyCols($count)) {
147
				for($i=$count; $i < $numCols; $i++) {
148
					$this->addItem(new HtmlGridCol("col-" . $this->identifier . "-" . $i, $width));
149
				}
150
			} else {
151
				for($i=0; $i < $count; $i++) {
152
					$this->getItem($i)->setColsCount($numCols);
153
				}
154
			}
155
		}
156
		return $this;
157
	}
158
159
	/**
160
	 * return the row at $index
161
	 * @param int $index
162
	 * @return HtmlGridRow
163
	 */
164
	public function getRow($index) {
165
		return $this->getItem($index);
166
	}
167
	
168
	/**
169
	 * @return HtmlGridRow
170
	 */
171
	public function getItem($index){
172
		return parent::getItem($index);
173
	}
174
175
	/**
176
	 * Returns the row count
177
	 * @return int
178
	 */
179
	public function rowCount() {
180
		$count=$this->count();
181
		if ($this->hasOnlyCols($count))
182
			return 0;
183
		return $count;
184
	}
185
186
	/**
187
	 * Returns the column count
188
	 * @return int
189
	 */
190
	public function colCount() {
191
		$count=$this->count();
192
		if ($this->hasOnlyCols($count))
193
			return $count;
194
		if ($count > 0)
195
			return $this->getItem(0)->count();
196
		return 0;
197
	}
198
199
	/**
200
	 * Returns the cell (HtmlGridCol) at position $row,$col
201
	 * @param int $row
202
	 * @param int $col
203
	 * @return HtmlGridCol|HtmlGridRow
204
	 */
205
	public function getCell($row, $col) {
206
		if ($row < 2 && $this->hasOnlyCols($this->count()))
207
			return $this->getItem($col);
208
		$rowO=$this->getItem($row);
209
		if (isset($rowO)) {
210
			$colO=$rowO->getItem($col);
211
		}
212
		return $colO;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $colO does not seem to be defined for all execution paths leading up to this point.
Loading history...
213
	}
214
215
	/**
216
	 * Adds dividers between columns ($vertically=false) or between rows ($vertically=true)
217
	 * @param boolean $vertically
218
	 * @return \Ajax\semantic\html\collections\HtmlGrid
219
	 */
220
	public function setDivided($vertically=false) {
221
		$value=($vertically === true) ? "vertically divided" : "divided";
222
		return $this->addToPropertyCtrl("class", $value, array ("divided" ));
223
	}
224
225
	/**
226
	 * Divides rows into cells
227
	 * @param boolean $internally true for internal cells
228
	 * @return \Ajax\semantic\html\collections\HtmlGrid
229
	 */
230
	public function setCelled($internally=false) {
231
		$value=($internally === true) ? "internally celled" : "celled";
232
		return $this->addToPropertyCtrl("class", $value, array ("celled","internally celled" ));
233
	}
234
235
	/**
236
	 * A grid can have its columns centered
237
	 */
238
	public function setCentered() {
239
		return $this->addToPropertyCtrl("class", "centered", array ("centered" ));
240
	}
241
242
	/**
243
	 * automatically resize all elements to split the available width evenly
244
	 * @return \Ajax\semantic\html\collections\HtmlGrid
245
	 */
246
	public function setEqualWidth() {
247
		return $this->addToProperty("class", "equal width");
248
	}
249
250
	/**
251
	 * Adds vertical or/and horizontal gutters
252
	 * @param string $value
253
	 * @return \Ajax\semantic\html\collections\HtmlGrid
254
	 */
255
	public function setPadded($value=NULL) {
256
		if (isset($value))
257
			$this->addToPropertyCtrl("class", $value, array ("vertically","horizontally" ));
258
		return $this->addToProperty("class", "padded");
259
	}
260
261
	/**
262
	 *
263
	 * @param boolean $very
264
	 * @return \Ajax\semantic\html\collections\HtmlGrid
265
	 */
266
	public function setRelaxed($very=false) {
267
		$value=($very === true) ? "very relaxed" : "relaxed";
268
		return $this->addToPropertyCtrl("class", $value, array ("relaxed","very relaxed" ));
269
	}
270
271
	public function setVerticalAlignment($value=VerticalAlignment::MIDDLE) {
272
		return $this->addToPropertyCtrl("class", $value . " aligned", VerticalAlignment::getConstantValues("aligned"));
273
	}
274
275
	/**
276
	 *
277
	 * {@inheritDoc}
278
	 *
279
	 * @see \Ajax\common\html\HtmlCollection::createItem()
280
	 */
281
	protected function createItem($value) {
282
		if ($this->_createCols === false)
283
			$value=null;
284
		$item=new HtmlGridRow($this->identifier . "-row-" . ($this->count() + 1), $value, $this->_colSizing, $this->_implicitRows);
285
		return $item;
286
	}
287
288
	/**
289
	 * Sets $values to the grid
290
	 * @param array $values
291
	 */
292
	public function setValues($values, $force=true) {
293
		$count=$this->count();
294
		$valuesSize=\sizeof($values);
295
		if ($this->_createCols === false || $force === true) {
296
			for($i=$count; $i < $valuesSize; $i++) {
297
				$colSize=\sizeof($values[$i]);
298
				$this->addItem(new HtmlGridRow($this->identifier . "-row-" . ($this->count() + 1), $colSize, $this->_colSizing, $this->_implicitRows));
299
			}
300
		}
301
		$count=\min(array ($this->count(),$valuesSize ));
302
		for($i=0; $i < $count; $i++) {
303
			$this->content[$i]->setValues($values[$i], $this->_createCols === false);
304
		}
305
	}
306
	
307
	public function setColWidth($numCol,$width){
308
		foreach ($this->content as $row){
309
			$row->getCol($numCol)->setWidth($width);
310
		}
311
		return $this;
312
	}
313
314
	/**
315
	 * stretch the row contents to take up the entire column height
316
	 * @return \Ajax\semantic\html\content\HtmlGridRow
317
	 */
318
	public function setStretched() {
319
		return $this->addToProperty("class", "stretched");
320
	}
321
322
	/**
323
	 * Adds a divider after the specified col
324
	 * @param integer $afterColIndex
325
	 * @param boolean $vertical
326
	 * @param mixed $content
327
	 * @return \Ajax\semantic\html\collections\HtmlGrid
328
	 */
329
	public function addDivider($afterColIndex, $vertical=true, $content=NULL) {
330
		$col=$this->getCell(0, $afterColIndex);
331
		if($col instanceof HtmlGridCol)
0 ignored issues
show
introduced by
$col is never a sub-type of Ajax\semantic\html\content\HtmlGridCol.
Loading history...
332
			$col->addDivider($vertical, $content);
333
		return $this;
334
	}
335
}
336