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 | public 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 | * Set the cell type. |
||
| 340 | * |
||
| 341 | * @param string $cellType The cell type. |
||
| 342 | * @return DataTablesColumn Returns this column. |
||
| 343 | */ |
||
| 344 | public function setCellType($cellType) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Set the class name. |
||
| 354 | * |
||
| 355 | * @param string $classname The class name. |
||
| 356 | * @return DataTablesColumn Returns this column. |
||
| 357 | */ |
||
| 358 | public function setClassname($classname) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set the content padding. |
||
| 365 | * |
||
| 366 | * @param string $contentPadding The content padding. |
||
| 367 | * @return DataTablesColumn Returns this column. |
||
| 368 | */ |
||
| 369 | public function setContentPadding($contentPadding) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set the data. |
||
| 376 | * |
||
| 377 | * @param integer|string $data The data. |
||
| 378 | * @return DataTablesColumn Returns this column. |
||
| 379 | */ |
||
| 380 | public function setData($data) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Set the default content. |
||
| 387 | * |
||
| 388 | * @param string $defaultContent The default content. |
||
| 389 | * @return DataTablesColumn Returns this column. |
||
| 390 | */ |
||
| 391 | public function setDefaultContent($defaultContent) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set the mapping. |
||
| 398 | * |
||
| 399 | * @param DataTablesMappingInterface $mapping The mapping. |
||
| 400 | * @return DataTablesColumn Returns this column. |
||
| 401 | */ |
||
| 402 | protected function setMapping(DataTablesMappingInterface $mapping) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set the name. |
||
| 409 | * |
||
| 410 | * @param string $name The name. |
||
| 411 | * @return DataTablesColumn Returns this column. |
||
| 412 | */ |
||
| 413 | public function setName($name) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Set the order data. |
||
| 420 | * |
||
| 421 | * @param integer|array $orderData The order data. |
||
| 422 | * @return DataTablesColumn Returns this column. |
||
| 423 | */ |
||
| 424 | public function setOrderData($orderData) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set the order data type. |
||
| 431 | * |
||
| 432 | * @param string $orderDataType The order data type. |
||
| 433 | * @return DataTablesColumn Returns this column. |
||
| 434 | */ |
||
| 435 | public function setOrderDataType($orderDataType) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Set the order sequence. |
||
| 442 | * |
||
| 443 | * @param string $orderSequence The order sequence. |
||
| 444 | * @return DataTablesColumn Returns this column. |
||
| 445 | */ |
||
| 446 | public function setOrderSequence($orderSequence) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Set the orderable. |
||
| 456 | * |
||
| 457 | * @param bool $orderable The orderable. |
||
| 458 | * @return DataTablesColumn Returns this column. |
||
| 459 | */ |
||
| 460 | public function setOrderable($orderable) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Set the search. |
||
| 467 | * |
||
| 468 | * @param DataTablesSearchInterface $search The search. |
||
| 469 | * @return DataTablesColumn Returns this column. |
||
| 470 | */ |
||
| 471 | public function setSearch(DataTablesSearchInterface $search) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Set the searchable. |
||
| 478 | * |
||
| 479 | * @param bool $searchable The searchable. |
||
| 480 | * @return DataTablesColumn Returns this column. |
||
| 481 | */ |
||
| 482 | public function setSearchable($searchable) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set the title. |
||
| 489 | * |
||
| 490 | * @param string $title The title. |
||
| 491 | * @return DataTablesColumn Returns this column. |
||
| 492 | */ |
||
| 493 | public function setTitle($title) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Set the type. |
||
| 500 | * |
||
| 501 | * @param string $type The type. |
||
| 502 | * @return DataTablesColumn Returns this column. |
||
| 503 | */ |
||
| 504 | public function setType($type) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Set the visible. |
||
| 514 | * |
||
| 515 | * @param bool $visible The visible. |
||
| 516 | * @return DataTablesColumn Returns this column. |
||
| 517 | */ |
||
| 518 | public function setVisible($visible) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set the width. |
||
| 525 | * |
||
| 526 | * @param string $width The width. |
||
| 527 | * @return DataTablesColumn Returns this column. |
||
| 528 | */ |
||
| 529 | public function setWidth($width) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Convert into an array representing this instance. |
||
| 536 | * |
||
| 537 | * @return array Returns an array representing this instance. |
||
| 538 | */ |
||
| 539 | public function toArray() { |
||
| 562 | |||
| 563 | } |
||
| 564 |