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) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns field name. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getName() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets field name. |
||
| 103 | * |
||
| 104 | * @param string $name |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | public function setName($name) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets field name. |
||
| 115 | * |
||
| 116 | * @param string|null $name |
||
| 117 | * @return $this|string |
||
| 118 | */ |
||
| 119 | public function name($name = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns true if column is hidden. |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function isHidden() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Makes column hidden. |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function hide() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Makes column visible. |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function show() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns text label that will be rendered in table header. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getLabel() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets text label that will be rendered in table header. |
||
| 173 | * |
||
| 174 | * @param string $label |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function setLabel($label) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets text label that will be rendered in table header. |
||
| 185 | * |
||
| 186 | * @param string|null $label |
||
| 187 | * @return $this|string |
||
| 188 | */ |
||
| 189 | public function label($label = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns true if column is sortable (sorting controls must be rendered). |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function isSortable() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Allows to enable or disable sorting controls for column. |
||
| 211 | * |
||
| 212 | * @param boolean $isSortable |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function setSortable($isSortable) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns current sorting order |
||
| 223 | * or null if table rows are not sorted using this column. |
||
| 224 | * |
||
| 225 | * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC |
||
| 226 | */ |
||
| 227 | public function getSorting() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Allows to specify sorting by this column for data rows. |
||
| 234 | * |
||
| 235 | * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setSorting($sortOrder) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Allows to enable or disable sorting controls for column. |
||
| 246 | * |
||
| 247 | * @param string|null $sorting |
||
| 248 | * @return $this|string |
||
| 249 | */ |
||
| 250 | public function sorting($sorting = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns true if data rows are sorted ascending using this column. |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function isSortedAsc() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns true if data rows are sorted descending using this column. |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function isSortedDesc() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Allows to set callback function that will render |
||
| 282 | * content of table cells for this column. |
||
| 283 | * |
||
| 284 | * @param callable $callback |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | public function setCallback($callback) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns function that will render |
||
| 295 | * content of table cells for this column if specified. |
||
| 296 | * |
||
| 297 | * @return callable|null |
||
| 298 | */ |
||
| 299 | public function getCallback() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Allows to set callback function that will render |
||
| 306 | * content of table cells for this column. |
||
| 307 | * |
||
| 308 | * @param string|null $callback |
||
| 309 | * @return $this|callable|null |
||
| 310 | */ |
||
| 311 | public function callback($callback = null) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Allows to specify filtering controls for column. |
||
| 323 | * |
||
| 324 | * @param Collection|FilterConfig[] $filters |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | public function setFilters($filters) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Allows to specify filtering controls for column. |
||
| 339 | * |
||
| 340 | * @param Collection|FilterConfig[]|null $filters |
||
| 341 | * @return $this|Collection|FilterConfig[] |
||
| 342 | */ |
||
| 343 | public function filters($filters = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Allows to add filtering control to column. |
||
| 359 | * |
||
| 360 | * @param FilterConfig $filter |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function addFilter(FilterConfig $filter) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Allows to add filtering control to column. |
||
| 372 | * |
||
| 373 | * @param FilterConfig $filter |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function filter(FilterConfig $filter) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Creates instance of filtering control configuration |
||
| 384 | * and binds it to the column. |
||
| 385 | * |
||
| 386 | * @param string $class |
||
| 387 | * @return FilterConfig |
||
| 388 | */ |
||
| 389 | public function makeFilter($class = '\Nayjest\Grids\FilterConfig') |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns true if any filtering controls specified for the column. |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | public function hasFilters() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns list of filtering controls specified for the column. |
||
| 408 | * |
||
| 409 | * @return Collection|FilterConfig[] |
||
| 410 | */ |
||
| 411 | public function getFilters() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @todo move to Field instance |
||
| 421 | * @param DataRowInterface $row |
||
| 422 | * @return mixed |
||
| 423 | */ |
||
| 424 | public function getValue(DataRowInterface $row) |
||
| 432 | } |
||
| 433 |