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 |
||
| 23 | class DataTablesColumn implements JsonSerializable { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Cell type. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $cellType; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Class name. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $classname; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Content padding. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $contentPadding; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Data. |
||
| 48 | * |
||
| 49 | * @var integer|string |
||
| 50 | */ |
||
| 51 | private $data; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Default content. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $defaultContent; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * mapping. |
||
| 62 | * |
||
| 63 | * @var DataTablesMapping |
||
| 64 | */ |
||
| 65 | private $mapping; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Name. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $name; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Order data. |
||
| 76 | * |
||
| 77 | * @var integer|array |
||
| 78 | */ |
||
| 79 | private $orderData; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Order data type. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $orderDataType; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Order sequence. |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private $orderSequence; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Orderable. |
||
| 97 | * |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | private $orderable; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Search. |
||
| 104 | * |
||
| 105 | * @var DataTablesSearch |
||
| 106 | */ |
||
| 107 | private $search; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Searchable. |
||
| 111 | * |
||
| 112 | * @var boolean |
||
| 113 | */ |
||
| 114 | private $searchable; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Type. |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | private $type; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Visible. |
||
| 125 | * |
||
| 126 | * @var boolean |
||
| 127 | */ |
||
| 128 | private $visible; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Width. |
||
| 132 | * |
||
| 133 | * @var string |
||
| 134 | */ |
||
| 135 | private $width; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Constructor. |
||
| 139 | */ |
||
| 140 | protected function __construct() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the cell type. |
||
| 150 | * |
||
| 151 | * @return string Returns the cell type. |
||
| 152 | */ |
||
| 153 | public function getCellType() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get the class name. |
||
| 159 | * |
||
| 160 | * @return string Returns the class name. |
||
| 161 | */ |
||
| 162 | public function getClassname() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the content padding. |
||
| 168 | * |
||
| 169 | * @return string Returns the content padding. |
||
| 170 | */ |
||
| 171 | public function getContentPadding() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the data. |
||
| 177 | * |
||
| 178 | * @return integer|string Returns the data. |
||
| 179 | */ |
||
| 180 | public function getData() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the default content |
||
| 186 | * |
||
| 187 | * @return string Returns the default content. |
||
| 188 | */ |
||
| 189 | public function getDefaultContent() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the mapping. |
||
| 195 | * |
||
| 196 | * @return DataTablesMapping The mapping. |
||
| 197 | */ |
||
| 198 | public function getMapping() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the name. |
||
| 204 | * |
||
| 205 | * @return string Returns the name. |
||
| 206 | */ |
||
| 207 | public function getName() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the order data. |
||
| 213 | * |
||
| 214 | * @return integer|array Returns the order data. |
||
| 215 | */ |
||
| 216 | public function getOrderData() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the order data type. |
||
| 222 | * |
||
| 223 | * @return string Returns the order data type. |
||
| 224 | */ |
||
| 225 | public function getOrderDataType() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the order sequence. |
||
| 231 | * |
||
| 232 | * @return string Returns the order sequence. |
||
| 233 | */ |
||
| 234 | public function getOrderSequence() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the orderable. |
||
| 240 | * |
||
| 241 | * @return boolean Returns the orderable. |
||
| 242 | */ |
||
| 243 | public function getOrderable() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get the search. |
||
| 249 | * |
||
| 250 | * @return DataTablesSearch Returns the search. |
||
| 251 | */ |
||
| 252 | public function getSearch() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the searchable. |
||
| 258 | * |
||
| 259 | * @return boolean Returns the searchable. |
||
| 260 | */ |
||
| 261 | public function getSearchable() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the type. |
||
| 267 | * |
||
| 268 | * @return string Returns the type. |
||
| 269 | */ |
||
| 270 | public function getType() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the visible. |
||
| 276 | * |
||
| 277 | * @return boolean Returns the visible. |
||
| 278 | */ |
||
| 279 | public function getVisible() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the width. |
||
| 285 | * |
||
| 286 | * @return string Returns the width. |
||
| 287 | */ |
||
| 288 | public function getWidth() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function jsonSerialize() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Create a DataTables column instance. |
||
| 301 | * |
||
| 302 | * @param string $data The column data. |
||
| 303 | * @param string $name The column name. |
||
| 304 | * @param string $cellType The column cell type. |
||
| 305 | * @return DataTablesColumn Returns a DataTables column. |
||
| 306 | */ |
||
| 307 | public static function newInstance($data, $name, $cellType = "td") { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Parse a raw columns array. |
||
| 322 | * |
||
| 323 | * @param array $rawColumns The raw columns array. |
||
| 324 | * @param DataTablesWrapper $wrapper The wrapper. |
||
| 325 | * @return DataTablesColumn[] Returns the DataTables columns. |
||
| 326 | */ |
||
| 327 | public static function parse(array $rawColumns, DataTablesWrapper $wrapper) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set the cell type. |
||
| 363 | * |
||
| 364 | * @param string $cellType The cell type. |
||
| 365 | * @return DataTablesColumn Returns this DataTables column. |
||
| 366 | */ |
||
| 367 | public function setCellType($cellType) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set the class name. |
||
| 374 | * |
||
| 375 | * @param string $classname The class name. |
||
| 376 | * @return DataTablesColumn Returns this DataTables column. |
||
| 377 | */ |
||
| 378 | public function setClassname($classname) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set the content padding. |
||
| 385 | * |
||
| 386 | * @param string $contentPadding The content padding. |
||
| 387 | * @return DataTablesColumn Returns this DataTables column. |
||
| 388 | */ |
||
| 389 | public function setContentPadding($contentPadding) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set the data. |
||
| 396 | * |
||
| 397 | * @param integer|string $data The data. |
||
| 398 | * @return DataTablesColumn Returns this DataTables column. |
||
| 399 | */ |
||
| 400 | protected function setData($data) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set the default content. |
||
| 407 | * |
||
| 408 | * @param string $defaultContent The default content. |
||
| 409 | * @return DataTablesColumn Returns this DataTables column. |
||
| 410 | */ |
||
| 411 | public function setDefaultContent($defaultContent) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set the mapping. |
||
| 418 | * |
||
| 419 | * @param DataTablesMapping $mapping The mapping. |
||
| 420 | * @return DataTablesColumn Returns this DataTables column. |
||
| 421 | */ |
||
| 422 | protected function setMapping(DataTablesMapping $mapping) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Set the name. |
||
| 429 | * |
||
| 430 | * @param string $name The name. |
||
| 431 | * @return DataTablesColumn Returns this DataTables column. |
||
| 432 | */ |
||
| 433 | public function setName($name) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Set the order data. |
||
| 440 | * |
||
| 441 | * @param integer|array $orderData The order data. |
||
| 442 | * @return DataTablesColumn Returns this DataTables column. |
||
| 443 | */ |
||
| 444 | public function setOrderData($orderData) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Set the order data type. |
||
| 451 | * |
||
| 452 | * @param string $orderDataType The order data type. |
||
| 453 | * @return DataTablesColumn Returns this DataTables column. |
||
| 454 | */ |
||
| 455 | public function setOrderDataType($orderDataType) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Set the order sequence. |
||
| 462 | * |
||
| 463 | * @param string $orderSequence The order sequence. |
||
| 464 | * @return DataTablesColumn Returns this DataTables column. |
||
| 465 | */ |
||
| 466 | public function setOrderSequence($orderSequence) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set the orderable. |
||
| 473 | * |
||
| 474 | * @param boolean $orderable The orderable. |
||
| 475 | * @return DataTablesColumn Returns this DataTables column. |
||
| 476 | */ |
||
| 477 | public function setOrderable($orderable) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set the search. |
||
| 484 | * |
||
| 485 | * @param DataTablesSearch $search The search. |
||
| 486 | * @return DataTablesColumn Returns this DataTables column. |
||
| 487 | */ |
||
| 488 | protected function setSearch(DataTablesSearch $search) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Set the searchable. |
||
| 495 | * |
||
| 496 | * @param boolean $searchable The searchable. |
||
| 497 | * @return DataTablesColumn Returns this DataTables column. |
||
| 498 | */ |
||
| 499 | public function setSearchable($searchable) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Set the type. |
||
| 506 | * |
||
| 507 | * @param string $type The type. |
||
| 508 | * @return DataTablesColumn Returns this DataTables column. |
||
| 509 | */ |
||
| 510 | public function setType($type) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Set the visible. |
||
| 517 | * |
||
| 518 | * @param boolean $visible The visible. |
||
| 519 | * @return DataTablesColumn Returns this DataTables column. |
||
| 520 | */ |
||
| 521 | public function setVisible($visible) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Set the width. |
||
| 528 | * |
||
| 529 | * @param string $width The width. |
||
| 530 | * @return DataTablesColumn Returns this DataTables column. |
||
| 531 | */ |
||
| 532 | public function setWidth($width) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Convert into an array representing this instance. |
||
| 539 | * |
||
| 540 | * @return array Returns an array representing this instance. |
||
| 541 | */ |
||
| 542 | public function toArray() { |
||
| 565 | |||
| 566 | } |
||
| 567 |