| Total Complexity | 61 |
| Total Lines | 307 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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() { |
||
| 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 rrow,$col |
||
| 201 | * @param int $row |
||
| 202 | * @param int $col |
||
| 203 | * @return HtmlGridCol |
||
| 204 | */ |
||
| 205 | public function getCell($row, $col) { |
||
| 206 | if ($row < 2 && $this->hasOnlyCols($this->count())) |
||
| 207 | return $this->getItem($col); |
||
|
|
|||
| 208 | $row=$this->getItem($row); |
||
| 209 | if (isset($row)) { |
||
| 210 | $col=$row->getItem($col); |
||
| 211 | } |
||
| 212 | return $col; |
||
| 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() { |
||
| 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) { |
||
| 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) { |
||
| 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 | public function addDivider($afterColIndex, $vertical=true, $content=NULL) { |
||
| 325 | } |
||
| 326 | } |
||
| 327 |