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) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param bool $sortable |
||
| 90 | * @return Column |
||
| 91 | */ |
||
| 92 | public function setSortable($sortable = TRUE) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $replacement array('pattern' => 'replacement') |
||
| 100 | * @return Column |
||
| 101 | */ |
||
| 102 | public function setReplacement(array $replacement) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param mixed $column |
||
| 110 | * @return Column |
||
| 111 | */ |
||
| 112 | public function setColumn($column) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $dir |
||
| 120 | * @return Column |
||
| 121 | */ |
||
| 122 | public function setDefaultSort($dir) |
||
| 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 = []) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param mixed $callback | |
||
| 143 | * @return Column |
||
| 144 | */ |
||
| 145 | public function setCustomRenderExport($callback) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param callback $callback |
||
| 153 | * @return Column |
||
| 154 | */ |
||
| 155 | public function setCellCallback($callback) |
||
| 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() |
||
| 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() |
||
| 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() |
||
| 281 | |||
| 282 | /**********************************************************************************************/ |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param mixed $row |
||
| 286 | * @return string |
||
| 287 | * @internal |
||
| 288 | */ |
||
| 289 | public function render($row) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param mixed $row |
||
| 301 | * @return string |
||
| 302 | 1 | * @internal |
|
| 303 | */ |
||
| 304 | public function renderExport($row) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param mixed $row |
||
| 316 | * @throws Exception |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | protected function getValue($row) |
||
| 332 | |||
| 333 | 1 | /** |
|
| 334 | * @param mixed $value |
||
| 335 | * @return mixed |
||
| 336 | */ |
||
| 337 | protected function applyReplacement($value) |
||
| 352 | |||
| 353 | 1 | /** |
|
| 354 | * @param mixed $value |
||
| 355 | * @return mixed |
||
| 356 | */ |
||
| 357 | protected function formatValue($value) |
||
| 365 | |||
| 366 | /******************************* Aliases for filters ******************************************/ |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return \Grido\Components\Filters\Text |
||
| 370 | */ |
||
| 371 | public function setFilterText() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return \Grido\Components\Filters\Date |
||
| 378 | */ |
||
| 379 | public function setFilterDate() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return \Grido\Components\Filters\DateRange |
||
| 386 | */ |
||
| 387 | public function setFilterDateRange() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return \Grido\Components\Filters\Check |
||
| 394 | */ |
||
| 395 | public function setFilterCheck() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param array $items |
||
| 402 | * @return \Grido\Components\Filters\Select |
||
| 403 | */ |
||
| 404 | public function setFilterSelect(array $items = NULL) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return \Grido\Components\Filters\Number |
||
| 411 | */ |
||
| 412 | public function setFilterNumber() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param \Nette\Forms\IControl $formControl |
||
| 419 | * @return \Grido\Components\Filters\Custom |
||
| 420 | */ |
||
| 421 | public function setFilterCustom(\Nette\Forms\IControl $formControl) |
||
| 425 | } |
||
| 426 |