Complex classes like FieldConfig 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 FieldConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class FieldConfig |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Field name. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $name; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Text label that will be rendered in table header. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $label; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $order = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $is_sortable = false; |
||
| 38 | |||
| 39 | protected $sorting; |
||
| 40 | |||
| 41 | /** @var Collection|FilterConfig[] */ |
||
| 42 | protected $filters; |
||
| 43 | |||
| 44 | /** @var callable */ |
||
| 45 | protected $callback; |
||
| 46 | |||
| 47 | protected $is_hidden = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor. |
||
| 51 | * |
||
| 52 | * @param string|null $name column unique name for internal usage |
||
| 53 | * @param string|null $label column label |
||
| 54 | */ |
||
| 55 | public function __construct($name = null, $label = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns column order. |
||
| 67 | * |
||
| 68 | * This property used to to identify column position in grid. |
||
| 69 | * |
||
| 70 | * @return int |
||
| 71 | */ |
||
| 72 | public function getOrder() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Sets column order. |
||
| 79 | * |
||
| 80 | * This property used to to identify column position in grid. |
||
| 81 | * |
||
| 82 | * @param $order |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function setOrder($order) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns field name. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getName() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Sets field name. |
||
| 104 | * |
||
| 105 | * @param string $name |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function setName($name) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets field name. |
||
| 117 | * |
||
| 118 | * @param string|null $name |
||
| 119 | * @return $this|string |
||
| 120 | */ |
||
| 121 | public function name($name = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns true if column is hidden. |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function isHidden() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Makes column hidden. |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function hide() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Makes column visible. |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function show() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns text label that will be rendered in table header. |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getLabel() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Sets text label that will be rendered in table header. |
||
| 178 | * |
||
| 179 | * @param string $label |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setLabel($label) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets text label that will be rendered in table header. |
||
| 191 | * |
||
| 192 | * @param string|null $label |
||
| 193 | * @return $this|string |
||
| 194 | */ |
||
| 195 | public function label($label = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns true if column is sortable (sorting controls must be rendered). |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function isSortable() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Allows to enable or disable sorting controls for column. |
||
| 218 | * |
||
| 219 | * @param boolean $isSortable |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function setSortable($isSortable) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Allows to enable or disable sorting controls for column. |
||
| 231 | * |
||
| 232 | * @param boolean|null $isSortable |
||
| 233 | * @return $this|boolean |
||
| 234 | */ |
||
| 235 | public function sortable($isSortable) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns current sorting order |
||
| 248 | * or null if table rows are not sorted using this column. |
||
| 249 | * |
||
| 250 | * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC |
||
| 251 | */ |
||
| 252 | public function getSorting() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Allows to specify sorting by this column for data rows. |
||
| 259 | * |
||
| 260 | * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function setSorting($sortOrder) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Allows to specify sorting by this column for data rows. |
||
| 272 | * |
||
| 273 | * @param null|string|boolean $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC|false |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | public function sorting($sortOrder = false) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns true if data rows are sorted ascending using this column. |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function isSortedAsc() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns true if data rows are sorted descending using this column. |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public function isSortedDesc() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Allows to set callback function that will render |
||
| 309 | * content of table cells for this column. |
||
| 310 | * |
||
| 311 | * @param callable $callback |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function setCallback($callback) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns function that will render |
||
| 323 | * content of table cells for this column if specified. |
||
| 324 | * |
||
| 325 | * @return callable|null |
||
| 326 | */ |
||
| 327 | public function getCallback() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Allows to set callback function that will render |
||
| 334 | * content of table cells for this column. |
||
| 335 | * |
||
| 336 | * @param string|null $callback |
||
| 337 | * @return $this|callable|null |
||
| 338 | */ |
||
| 339 | public function callback($callback = null) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Allows to specify filtering controls for column. |
||
| 352 | * |
||
| 353 | * @param Collection|FilterConfig[] $filters |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function setFilters($filters) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Allows to specify filtering controls for column. |
||
| 368 | * |
||
| 369 | * @param Collection|FilterConfig[]|null $filters |
||
| 370 | * @return $this|Collection|FilterConfig[] |
||
| 371 | */ |
||
| 372 | public function filters($filters = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Allows to add filtering control to column. |
||
| 388 | * |
||
| 389 | * @param FilterConfig $filter |
||
| 390 | * @return $this |
||
| 391 | */ |
||
| 392 | public function addFilter(FilterConfig $filter) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Allows to add filtering control to column. |
||
| 402 | * |
||
| 403 | * @param FilterConfig $filter |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | public function filter(FilterConfig $filter) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Creates instance of filtering control configuration |
||
| 415 | * and binds it to the column. |
||
| 416 | * |
||
| 417 | * @param string $class |
||
| 418 | * @return FilterConfig |
||
| 419 | */ |
||
| 420 | public function makeFilter($class = '\Nayjest\Grids\FilterConfig') |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Returns true if any filtering controls specified for the column. |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function hasFilters() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns list of filtering controls specified for the column. |
||
| 440 | * |
||
| 441 | * @return Collection|FilterConfig[] |
||
| 442 | */ |
||
| 443 | public function getFilters() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @todo move to Field instance |
||
| 454 | * @param DataRowInterface $row |
||
| 455 | * @return mixed |
||
| 456 | */ |
||
| 457 | public function getValue(DataRowInterface $row) |
||
| 465 | } |
||
| 466 |