Complex classes like Column 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 Column, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | abstract class Column extends \Grido\Components\Component |
||
| 37 | 1 | { |
|
| 38 | const ID = 'columns'; |
||
| 39 | |||
| 40 | const VALUE_IDENTIFIER = '%value'; |
||
| 41 | |||
| 42 | const ORDER_ASC = 'asc'; |
||
| 43 | const ORDER_DESC = 'desc'; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | protected $sort; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | protected $column; |
||
| 50 | |||
| 51 | /** @var \Nette\Utils\Html <td> html tag */ |
||
| 52 | protected $cellPrototype; |
||
| 53 | |||
| 54 | /** @var callback returns td html element; function($row, Html $td) */ |
||
| 55 | protected $cellCallback; |
||
| 56 | |||
| 57 | /** @var \Nette\Utils\Html <th> html tag */ |
||
| 58 | protected $headerPrototype; |
||
| 59 | |||
| 60 | /** @var mixed custom rendering */ |
||
| 61 | protected $customRender; |
||
| 62 | |||
| 63 | /** @var array custom rendering template variables */ |
||
| 64 | protected $customRenderVariables = []; |
||
| 65 | |||
| 66 | /** @var mixed custom export rendering */ |
||
| 67 | protected $customRenderExport; |
||
| 68 | |||
| 69 | /** @var bool */ |
||
| 70 | protected $sortable = FALSE; |
||
| 71 | |||
| 72 | /** @var array of arrays('pattern' => 'replacement') */ |
||
| 73 | protected $replacements = []; |
||
| 74 | 1 | ||
| 75 | /** |
||
| 76 | * @param \Grido\Grid $grid |
||
| 77 | * @param string $name |
||
| 78 | * @param string $label |
||
| 79 | */ |
||
| 80 | public function __construct($grid, $name, $label) |
||
| 81 | { |
||
| 82 | 1 | $this->addComponentToGrid($grid, Helpers::formatColumnName($name)); |
|
| 83 | |||
| 84 | 1 | $this->type = get_class($this); |
|
| 85 | 1 | $this->label = $label; |
|
| 86 | 1 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param bool $sortable |
||
| 90 | * @return Column |
||
| 91 | */ |
||
| 92 | public function setSortable($sortable = TRUE) |
||
| 93 | { |
||
| 94 | 1 | $this->sortable = (bool) $sortable; |
|
| 95 | 1 | return $this; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $replacement array('pattern' => 'replacement') |
||
| 100 | * @return Column |
||
| 101 | */ |
||
| 102 | public function setReplacement(array $replacement) |
||
| 103 | { |
||
| 104 | 1 | $this->replacements = $this->replacements + $replacement; |
|
| 105 | 1 | return $this; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param mixed $column |
||
| 110 | * @return Column |
||
| 111 | */ |
||
| 112 | public function setColumn($column) |
||
| 113 | { |
||
| 114 | 1 | $this->column = $column; |
|
| 115 | 1 | return $this; |
|
| 116 | } |
||
| 117 | 1 | ||
| 118 | /** |
||
| 119 | * @param string $dir |
||
| 120 | * @return Column |
||
| 121 | */ |
||
| 122 | public function setDefaultSort($dir) |
||
| 123 | { |
||
| 124 | 1 | $this->grid->setDefaultSort([$this->getName() => $dir]); |
|
| 125 | 1 | return $this; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param mixed $callback callback or string for name of template filename |
||
| 130 | * @param array $variables - template variables |
||
| 131 | * @return Column |
||
| 132 | */ |
||
| 133 | public function setCustomRender($callback, $variables = []) |
||
| 134 | { |
||
| 135 | 1 | $this->customRender = $callback; |
|
| 136 | 1 | $this->customRenderVariables = $variables; |
|
| 137 | |||
| 138 | 1 | return $this; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param mixed $callback | |
||
| 143 | * @return Column |
||
| 144 | */ |
||
| 145 | public function setCustomRenderExport($callback) |
||
| 146 | { |
||
| 147 | 1 | $this->customRenderExport = $callback; |
|
| 148 | 1 | return $this; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param callback $callback |
||
| 153 | * @return Column |
||
| 154 | */ |
||
| 155 | public function setCellCallback($callback) |
||
| 156 | { |
||
| 157 | 1 | $this->cellCallback = $callback; |
|
| 158 | 1 | return $this; |
|
| 159 | } |
||
| 160 | |||
| 161 | /**********************************************************************************************/ |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns cell prototype (<td> html tag). |
||
| 165 | * @param mixed $row |
||
| 166 | * @return \Nette\Utils\Html |
||
| 167 | */ |
||
| 168 | public function getCellPrototype($row = NULL) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns header cell prototype (<th> html tag). |
||
| 187 | * @return \Nette\Utils\Html |
||
| 188 | */ |
||
| 189 | public function getHeaderPrototype() |
||
| 190 | { |
||
| 191 | 1 | if ($this->headerPrototype === NULL) { |
|
| 192 | 1 | $this->headerPrototype = \Nette\Utils\Html::el('th') |
|
| 193 | 1 | ->setClass(['column', 'grid-header-' . $this->getName()]); |
|
| 194 | 1 | } |
|
| 195 | |||
| 196 | 1 | if ($this->isSortable() && $this->getSort()) { |
|
| 197 | 1 | $this->headerPrototype->class[] = $this->getSort() == self::ORDER_DESC |
|
| 198 | 1 | ? 'desc' |
|
| 199 | 1 | : 'asc'; |
|
| 200 | 1 | } |
|
| 201 | |||
| 202 | 1 | return $this->headerPrototype; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return mixed |
||
| 207 | * @internal |
||
| 208 | */ |
||
| 209 | public function getColumn() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | * @internal |
||
| 217 | */ |
||
| 218 | public function getSort() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return mixed |
||
| 235 | * @internal |
||
| 236 | */ |
||
| 237 | public function getCustomRender() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return array |
||
| 244 | * @internal |
||
| 245 | */ |
||
| 246 | public function getCustomRenderVariables() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return mixed |
||
| 253 | * @internal |
||
| 254 | */ |
||
| 255 | public function getLabel() |
||
| 256 | { |
||
| 257 | 1 | return is_string($this->label) |
|
| 258 | 1 | ? $this->translate($this->label) |
|
| 259 | 1 | : $this->label; |
|
| 260 | } |
||
| 261 | |||
| 262 | /**********************************************************************************************/ |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return bool |
||
| 266 | * @internal |
||
| 267 | */ |
||
| 268 | public function isSortable() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return bool |
||
| 275 | * @internal |
||
| 276 | */ |
||
| 277 | public function hasFilter() |
||
| 278 | { |
||
| 279 | 1 | return (bool) $this->grid->getFilter($this->getName(), FALSE); |
|
| 280 | } |
||
| 281 | |||
| 282 | /**********************************************************************************************/ |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param mixed $row |
||
| 286 | 1 | * @return string |
|
| 287 | * @internal |
||
| 288 | */ |
||
| 289 | public function render($row) |
||
| 290 | { |
||
| 291 | 1 | if (is_callable($this->customRender)) { |
|
| 292 | 1 | return call_user_func_array($this->customRender, [$row]); |
|
| 293 | } |
||
| 294 | |||
| 295 | 1 | $value = $this->getValue($row); |
|
| 296 | 1 | return $this->formatValue($value); |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param mixed $row |
||
| 301 | * @return string |
||
| 302 | * @internal |
||
| 303 | */ |
||
| 304 | public function renderExport($row) |
||
| 305 | { |
||
| 306 | 1 | if (is_callable($this->customRenderExport)) { |
|
| 307 | 1 | return call_user_func_array($this->customRenderExport, [$row]); |
|
| 308 | } |
||
| 309 | |||
| 310 | 1 | $value = $this->getValue($row); |
|
| 311 | 1 | return strip_tags($this->applyReplacement($value)); |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param mixed $row |
||
| 316 | 1 | * @throws Exception |
|
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | protected function getValue($row) |
||
| 320 | { |
||
| 321 | 1 | $column = $this->getColumn(); |
|
| 322 | 1 | if (is_string($column)) { |
|
| 323 | 1 | return $this->grid->getProperty($row, Helpers::unformatColumnName($column)); |
|
| 324 | |||
| 325 | } elseif (is_callable($column)) { |
||
| 326 | return call_user_func_array($column, [$row]); |
||
| 327 | 1 | ||
| 328 | } else { |
||
| 329 | throw new Exception('Column must be string or callback.'); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param mixed $value |
||
| 335 | * @return mixed |
||
| 336 | */ |
||
| 337 | 1 | protected function applyReplacement($value) |
|
| 338 | { |
||
| 339 | 1 | return (is_scalar($value) || $value === NULL) && isset($this->replacements[$value]) |
|
| 340 | 1 | ? is_string($value) |
|
| 341 | 1 | ? str_replace(static::VALUE_IDENTIFIER, $value, $this->replacements[$value]) |
|
| 342 | 1 | : $this->replacements[$value] |
|
| 343 | 1 | : $value; |
|
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | 1 | * @param mixed $value |
|
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | protected function formatValue($value) |
||
| 351 | { |
||
| 352 | 1 | $value = is_string($value) |
|
| 353 | 1 | ? \Latte\Runtime\Filters::escapeHtml($value) |
|
| 354 | 1 | : $value; |
|
| 355 | |||
| 356 | 1 | return $this->applyReplacement($value); |
|
| 357 | } |
||
| 358 | |||
| 359 | /******************************* Aliases for filters ******************************************/ |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return \Grido\Components\Filters\Text |
||
| 363 | */ |
||
| 364 | public function setFilterText() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return \Grido\Components\Filters\Date |
||
| 371 | */ |
||
| 372 | public function setFilterDate() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return \Grido\Components\Filters\DateRange |
||
| 379 | */ |
||
| 380 | public function setFilterDateRange() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return \Grido\Components\Filters\Check |
||
| 387 | */ |
||
| 388 | public function setFilterCheck() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param array $items |
||
| 395 | * @return \Grido\Components\Filters\Select |
||
| 396 | */ |
||
| 397 | public function setFilterSelect(array $items = NULL) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return \Grido\Components\Filters\Number |
||
| 404 | */ |
||
| 405 | public function setFilterNumber() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param \Nette\Forms\IControl $formControl |
||
| 412 | * @return \Grido\Components\Filters\Custom |
||
| 413 | */ |
||
| 414 | public function setFilterCustom(\Nette\Forms\IControl $formControl) |
||
| 418 | } |
||
| 419 |