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 | /** |
||
| 40 | * @var string|null |
||
| 41 | */ |
||
| 42 | protected $sortable_column; |
||
| 43 | |||
| 44 | protected $sorting; |
||
| 45 | |||
| 46 | /** @var Collection|FilterConfig[] */ |
||
| 47 | protected $filters; |
||
| 48 | |||
| 49 | /** @var callable */ |
||
| 50 | protected $callback; |
||
| 51 | |||
| 52 | protected $is_hidden = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor. |
||
| 56 | * |
||
| 57 | * @param string|null $name column unique name for internal usage |
||
| 58 | * @param string|null $label column label |
||
| 59 | */ |
||
| 60 | public function __construct($name = null, $label = null) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns column order. |
||
| 72 | * |
||
| 73 | * This property used to to identify column position in grid. |
||
| 74 | * |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | public function getOrder() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Sets column order. |
||
| 84 | * |
||
| 85 | * This property used to to identify column position in grid. |
||
| 86 | * |
||
| 87 | * @param $order |
||
| 88 | * @return $this |
||
| 89 | */ |
||
| 90 | public function setOrder($order) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns field name. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getName() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets field name. |
||
| 109 | * |
||
| 110 | * @param string $name |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function setName($name) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the sortable name. |
||
| 122 | * |
||
| 123 | * @returns string |
||
| 124 | */ |
||
| 125 | public function getSortableName() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Sets field name. |
||
| 136 | * |
||
| 137 | * @param string|null $name |
||
| 138 | * @return $this|string |
||
| 139 | */ |
||
| 140 | public function name($name = null) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns true if column is hidden. |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function isHidden() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Makes column hidden. |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function hide() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Makes column visible. |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function show() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns text label that will be rendered in table header. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getLabel() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets text label that will be rendered in table header. |
||
| 197 | * |
||
| 198 | * @param string $label |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setLabel($label) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Sets text label that will be rendered in table header. |
||
| 210 | * |
||
| 211 | * @param string|null $label |
||
| 212 | * @return $this|string |
||
| 213 | */ |
||
| 214 | public function label($label = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns true if column is sortable (sorting controls must be rendered). |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function isSortable() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Allows to enable or disable sorting controls for column. |
||
| 237 | * |
||
| 238 | * @param boolean $isSortable |
||
| 239 | * @param string|null $sortableColumn |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function setSortable($isSortable, $sortableColumn = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Allows to enable or disable sorting controls for column. |
||
| 252 | * |
||
| 253 | * @param boolean|null $isSortable |
||
| 254 | * @param string|null $sortableColumn by default we use the field name to sort, but in some cases may be different |
||
| 255 | * name, for example, the relation column is 'profile.nickname' and inner join is 'profiles.nickname'. |
||
| 256 | * @return $this|boolean |
||
| 257 | */ |
||
| 258 | public function sortable($isSortable, $sortableColumn = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns current sorting order |
||
| 272 | * or null if table rows are not sorted using this column. |
||
| 273 | * |
||
| 274 | * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC |
||
| 275 | */ |
||
| 276 | public function getSorting() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Allows to specify sorting by this column for data rows. |
||
| 283 | * |
||
| 284 | * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | public function setSorting($sortOrder) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Allows to specify sorting by this column for data rows. |
||
| 296 | * |
||
| 297 | * @param null|string|boolean $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC|false |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function sorting($sortOrder = false) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Returns true if data rows are sorted ascending using this column. |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function isSortedAsc() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns true if data rows are sorted descending using this column. |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function isSortedDesc() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Allows to set callback function that will render |
||
| 333 | * content of table cells for this column. |
||
| 334 | * |
||
| 335 | * @param callable $callback |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function setCallback($callback) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns function that will render |
||
| 347 | * content of table cells for this column if specified. |
||
| 348 | * |
||
| 349 | * @return callable|null |
||
| 350 | */ |
||
| 351 | public function getCallback() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Allows to set callback function that will render |
||
| 358 | * content of table cells for this column. |
||
| 359 | * |
||
| 360 | * @param string|null $callback |
||
| 361 | * @return $this|callable|null |
||
| 362 | */ |
||
| 363 | public function callback($callback = null) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Allows to specify filtering controls for column. |
||
| 376 | * |
||
| 377 | * @param Collection|FilterConfig[] $filters |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | public function setFilters($filters) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Allows to specify filtering controls for column. |
||
| 392 | * |
||
| 393 | * @param Collection|FilterConfig[]|null $filters |
||
| 394 | * @return $this|Collection|FilterConfig[] |
||
| 395 | */ |
||
| 396 | public function filters($filters = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Allows to add filtering control to column. |
||
| 412 | * |
||
| 413 | * @param FilterConfig $filter |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | public function addFilter(FilterConfig $filter) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Allows to add filtering control to column. |
||
| 426 | * |
||
| 427 | * @param FilterConfig $filter |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function filter(FilterConfig $filter) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Creates instance of filtering control configuration |
||
| 439 | * and binds it to the column. |
||
| 440 | * |
||
| 441 | * @param string $class |
||
| 442 | * @return FilterConfig |
||
| 443 | */ |
||
| 444 | public function makeFilter($class = '\Nayjest\Grids\FilterConfig') |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Returns true if any filtering controls specified for the column. |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function hasFilters() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns list of filtering controls specified for the column. |
||
| 464 | * |
||
| 465 | * @return Collection|FilterConfig[] |
||
| 466 | */ |
||
| 467 | public function getFilters() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @todo move to Field instance |
||
| 478 | * @param DataRowInterface $row |
||
| 479 | * @return mixed |
||
| 480 | */ |
||
| 481 | public function getValue(DataRowInterface $row) |
||
| 489 | } |
||
| 490 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: