Complex classes like DataTablesColumn 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 DataTablesColumn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DataTablesColumn implements DataTablesColumnInterface, JsonSerializable { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Cell type. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $cellType; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Class name. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $classname; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Content padding. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $contentPadding; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Data. |
||
| 49 | * |
||
| 50 | * @var integer|string |
||
| 51 | */ |
||
| 52 | private $data; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Default content. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $defaultContent; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * mapping. |
||
| 63 | * |
||
| 64 | * @var DataTablesMappingInterface |
||
| 65 | */ |
||
| 66 | private $mapping; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Name. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $name; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Order data. |
||
| 77 | * |
||
| 78 | * @var integer|array |
||
| 79 | */ |
||
| 80 | private $orderData; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Order data type. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $orderDataType; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Order sequence. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | private $orderSequence; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Orderable. |
||
| 98 | * |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | private $orderable; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Search. |
||
| 105 | * |
||
| 106 | * @var DataTablesSearchInterface |
||
| 107 | */ |
||
| 108 | private $search; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Searchable. |
||
| 112 | * |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | private $searchable; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Title. |
||
| 119 | * |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | private $title; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Type. |
||
| 126 | * |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | private $type; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Visible. |
||
| 133 | * |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | private $visible; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Width. |
||
| 140 | * |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | private $width; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Constructor. |
||
| 147 | */ |
||
| 148 | protected function __construct() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the cell type. |
||
| 158 | * |
||
| 159 | * @return string Returns the cell type. |
||
| 160 | */ |
||
| 161 | public function getCellType() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the class name. |
||
| 167 | * |
||
| 168 | * @return string Returns the class name. |
||
| 169 | */ |
||
| 170 | public function getClassname() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the content padding. |
||
| 176 | * |
||
| 177 | * @return string Returns the content padding. |
||
| 178 | */ |
||
| 179 | public function getContentPadding() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the data. |
||
| 185 | * |
||
| 186 | * @return integer|string Returns the data. |
||
| 187 | */ |
||
| 188 | public function getData() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the default content |
||
| 194 | * |
||
| 195 | * @return string Returns the default content. |
||
| 196 | */ |
||
| 197 | public function getDefaultContent() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the mapping. |
||
| 203 | * |
||
| 204 | * @return DataTablesMappingInterface The mapping. |
||
| 205 | */ |
||
| 206 | public function getMapping() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the name. |
||
| 212 | * |
||
| 213 | * @return string Returns the name. |
||
| 214 | */ |
||
| 215 | public function getName() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the order data. |
||
| 221 | * |
||
| 222 | * @return integer|array Returns the order data. |
||
| 223 | */ |
||
| 224 | public function getOrderData() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the order data type. |
||
| 230 | * |
||
| 231 | * @return string Returns the order data type. |
||
| 232 | */ |
||
| 233 | public function getOrderDataType() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the order sequence. |
||
| 239 | * |
||
| 240 | * @return string Returns the order sequence. |
||
| 241 | */ |
||
| 242 | public function getOrderSequence() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get the orderable. |
||
| 248 | * |
||
| 249 | * @return bool Returns the orderable. |
||
| 250 | */ |
||
| 251 | public function getOrderable() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the search. |
||
| 257 | * |
||
| 258 | * @return DataTablesSearchInterface Returns the search. |
||
| 259 | */ |
||
| 260 | public function getSearch() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the searchable. |
||
| 266 | * |
||
| 267 | * @return bool Returns the searchable. |
||
| 268 | */ |
||
| 269 | public function getSearchable() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the title. |
||
| 275 | * |
||
| 276 | * @return string Returns the title. |
||
| 277 | */ |
||
| 278 | public function getTitle() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the type. |
||
| 284 | * |
||
| 285 | * @return string Returns the type. |
||
| 286 | */ |
||
| 287 | public function getType() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the visible. |
||
| 293 | * |
||
| 294 | * @return bool Returns the visible. |
||
| 295 | */ |
||
| 296 | public function getVisible() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the width. |
||
| 302 | * |
||
| 303 | * @return string Returns the width. |
||
| 304 | */ |
||
| 305 | public function getWidth() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function jsonSerialize() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Create a column instance. |
||
| 318 | * |
||
| 319 | * @param string $data The column data. |
||
| 320 | * @param string $name The column name. |
||
| 321 | * @param string $cellType The column cell type. |
||
| 322 | * @return DataTablesColumn Returns a column. |
||
| 323 | */ |
||
| 324 | public static function newInstance($data, $name, $cellType = self::DATATABLES_CELL_TYPE_TD) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Parse a raw columns array. |
||
| 340 | * |
||
| 341 | * @param array $rawColumns The raw columns array. |
||
| 342 | * @param DataTablesWrapper $wrapper The wrapper. |
||
| 343 | * @return DataTablesColumn[] Returns the columns. |
||
| 344 | */ |
||
| 345 | public static function parse(array $rawColumns, DataTablesWrapper $wrapper) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Set the cell type. |
||
| 381 | * |
||
| 382 | * @param string $cellType The cell type. |
||
| 383 | * @return DataTablesColumn Returns this column. |
||
| 384 | */ |
||
| 385 | public function setCellType($cellType) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set the class name. |
||
| 395 | * |
||
| 396 | * @param string $classname The class name. |
||
| 397 | * @return DataTablesColumn Returns this column. |
||
| 398 | */ |
||
| 399 | public function setClassname($classname) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set the content padding. |
||
| 406 | * |
||
| 407 | * @param string $contentPadding The content padding. |
||
| 408 | * @return DataTablesColumn Returns this column. |
||
| 409 | */ |
||
| 410 | public function setContentPadding($contentPadding) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Set the data. |
||
| 417 | * |
||
| 418 | * @param integer|string $data The data. |
||
| 419 | * @return DataTablesColumn Returns this column. |
||
| 420 | */ |
||
| 421 | protected function setData($data) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Set the default content. |
||
| 428 | * |
||
| 429 | * @param string $defaultContent The default content. |
||
| 430 | * @return DataTablesColumn Returns this column. |
||
| 431 | */ |
||
| 432 | public function setDefaultContent($defaultContent) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set the mapping. |
||
| 439 | * |
||
| 440 | * @param DataTablesMappingInterface $mapping The mapping. |
||
| 441 | * @return DataTablesColumn Returns this column. |
||
| 442 | */ |
||
| 443 | protected function setMapping(DataTablesMappingInterface $mapping) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Set the name. |
||
| 450 | * |
||
| 451 | * @param string $name The name. |
||
| 452 | * @return DataTablesColumn Returns this column. |
||
| 453 | */ |
||
| 454 | public function setName($name) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set the order data. |
||
| 461 | * |
||
| 462 | * @param integer|array $orderData The order data. |
||
| 463 | * @return DataTablesColumn Returns this column. |
||
| 464 | */ |
||
| 465 | public function setOrderData($orderData) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set the order data type. |
||
| 472 | * |
||
| 473 | * @param string $orderDataType The order data type. |
||
| 474 | * @return DataTablesColumn Returns this column. |
||
| 475 | */ |
||
| 476 | public function setOrderDataType($orderDataType) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Set the order sequence. |
||
| 483 | * |
||
| 484 | * @param string $orderSequence The order sequence. |
||
| 485 | * @return DataTablesColumn Returns this column. |
||
| 486 | */ |
||
| 487 | public function setOrderSequence($orderSequence) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set the orderable. |
||
| 497 | * |
||
| 498 | * @param bool $orderable The orderable. |
||
| 499 | * @return DataTablesColumn Returns this column. |
||
| 500 | */ |
||
| 501 | public function setOrderable($orderable) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Set the search. |
||
| 508 | * |
||
| 509 | * @param DataTablesSearchInterface $search The search. |
||
| 510 | * @return DataTablesColumn Returns this column. |
||
| 511 | */ |
||
| 512 | protected function setSearch(DataTablesSearchInterface $search) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Set the searchable. |
||
| 519 | * |
||
| 520 | * @param bool $searchable The searchable. |
||
| 521 | * @return DataTablesColumn Returns this column. |
||
| 522 | */ |
||
| 523 | public function setSearchable($searchable) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Set the title. |
||
| 530 | * |
||
| 531 | * @param string $title The title. |
||
| 532 | * @return DataTablesColumn Returns this column. |
||
| 533 | */ |
||
| 534 | public function setTitle($title) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Set the type. |
||
| 541 | * |
||
| 542 | * @param string $type The type. |
||
| 543 | * @return DataTablesColumn Returns this column. |
||
| 544 | */ |
||
| 545 | public function setType($type) { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Set the visible. |
||
| 555 | * |
||
| 556 | * @param bool $visible The visible. |
||
| 557 | * @return DataTablesColumn Returns this column. |
||
| 558 | */ |
||
| 559 | public function setVisible($visible) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Set the width. |
||
| 566 | * |
||
| 567 | * @param string $width The width. |
||
| 568 | * @return DataTablesColumn Returns this column. |
||
| 569 | */ |
||
| 570 | public function setWidth($width) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Convert into an array representing this instance. |
||
| 577 | * |
||
| 578 | * @return array Returns an array representing this instance. |
||
| 579 | */ |
||
| 580 | public function toArray() { |
||
| 603 | |||
| 604 | } |
||
| 605 |