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 DataTablesColumnInterface, 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 | * Title. |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | private $title; |
||
122 | |||
123 | /** |
||
124 | * Type. |
||
125 | * |
||
126 | * @var string |
||
127 | */ |
||
128 | private $type; |
||
129 | |||
130 | /** |
||
131 | * Visible. |
||
132 | * |
||
133 | * @var boolean |
||
134 | */ |
||
135 | private $visible; |
||
136 | |||
137 | /** |
||
138 | * Width. |
||
139 | * |
||
140 | * @var string |
||
141 | */ |
||
142 | private $width; |
||
143 | |||
144 | /** |
||
145 | * Constructor. |
||
146 | */ |
||
147 | protected function __construct() { |
||
154 | |||
155 | /** |
||
156 | * DataTables cell types. |
||
157 | * |
||
158 | * @return array Returns the cell types. |
||
159 | */ |
||
160 | public static function dtCellTypes() { |
||
166 | |||
167 | /** |
||
168 | * DataTables order sequences. |
||
169 | * |
||
170 | * @return array Returns the order sequences. |
||
171 | */ |
||
172 | public static function dtOrderSequences() { |
||
178 | |||
179 | /** |
||
180 | * DataTables types. |
||
181 | * |
||
182 | * @return array Returns the types. |
||
183 | */ |
||
184 | public static function dtTypes() { |
||
194 | |||
195 | /** |
||
196 | * Get the cell type. |
||
197 | * |
||
198 | * @return string Returns the cell type. |
||
199 | */ |
||
200 | public function getCellType() { |
||
203 | |||
204 | /** |
||
205 | * Get the class name. |
||
206 | * |
||
207 | * @return string Returns the class name. |
||
208 | */ |
||
209 | public function getClassname() { |
||
212 | |||
213 | /** |
||
214 | * Get the content padding. |
||
215 | * |
||
216 | * @return string Returns the content padding. |
||
217 | */ |
||
218 | public function getContentPadding() { |
||
221 | |||
222 | /** |
||
223 | * Get the data. |
||
224 | * |
||
225 | * @return integer|string Returns the data. |
||
226 | */ |
||
227 | public function getData() { |
||
230 | |||
231 | /** |
||
232 | * Get the default content |
||
233 | * |
||
234 | * @return string Returns the default content. |
||
235 | */ |
||
236 | public function getDefaultContent() { |
||
239 | |||
240 | /** |
||
241 | * Get the mapping. |
||
242 | * |
||
243 | * @return DataTablesMapping The mapping. |
||
244 | */ |
||
245 | public function getMapping() { |
||
248 | |||
249 | /** |
||
250 | * Get the name. |
||
251 | * |
||
252 | * @return string Returns the name. |
||
253 | */ |
||
254 | public function getName() { |
||
257 | |||
258 | /** |
||
259 | * Get the order data. |
||
260 | * |
||
261 | * @return integer|array Returns the order data. |
||
262 | */ |
||
263 | public function getOrderData() { |
||
266 | |||
267 | /** |
||
268 | * Get the order data type. |
||
269 | * |
||
270 | * @return string Returns the order data type. |
||
271 | */ |
||
272 | public function getOrderDataType() { |
||
275 | |||
276 | /** |
||
277 | * Get the order sequence. |
||
278 | * |
||
279 | * @return string Returns the order sequence. |
||
280 | */ |
||
281 | public function getOrderSequence() { |
||
284 | |||
285 | /** |
||
286 | * Get the orderable. |
||
287 | * |
||
288 | * @return boolean Returns the orderable. |
||
289 | */ |
||
290 | public function getOrderable() { |
||
293 | |||
294 | /** |
||
295 | * Get the search. |
||
296 | * |
||
297 | * @return DataTablesSearch Returns the search. |
||
298 | */ |
||
299 | public function getSearch() { |
||
302 | |||
303 | /** |
||
304 | * Get the searchable. |
||
305 | * |
||
306 | * @return boolean Returns the searchable. |
||
307 | */ |
||
308 | public function getSearchable() { |
||
311 | |||
312 | /** |
||
313 | * Get the title. |
||
314 | * |
||
315 | * @return string Returns the title. |
||
316 | */ |
||
317 | public function getTitle() { |
||
320 | |||
321 | /** |
||
322 | * Get the type. |
||
323 | * |
||
324 | * @return string Returns the type. |
||
325 | */ |
||
326 | public function getType() { |
||
329 | |||
330 | /** |
||
331 | * Get the visible. |
||
332 | * |
||
333 | * @return boolean Returns the visible. |
||
334 | */ |
||
335 | public function getVisible() { |
||
338 | |||
339 | /** |
||
340 | * Get the width. |
||
341 | * |
||
342 | * @return string Returns the width. |
||
343 | */ |
||
344 | public function getWidth() { |
||
347 | |||
348 | /** |
||
349 | * {@inheritdoc} |
||
350 | */ |
||
351 | public function jsonSerialize() { |
||
354 | |||
355 | /** |
||
356 | * Create a column instance. |
||
357 | * |
||
358 | * @param string $data The column data. |
||
359 | * @param string $name The column name. |
||
360 | * @param string $cellType The column cell type. |
||
361 | * @return DataTablesColumn Returns a column. |
||
362 | */ |
||
363 | public static function newInstance($data, $name, $cellType = self::DATATABLES_CELL_TYPE_TD) { |
||
376 | |||
377 | /** |
||
378 | * Parse a raw columns array. |
||
379 | * |
||
380 | * @param array $rawColumns The raw columns array. |
||
381 | * @param DataTablesWrapper $wrapper The wrapper. |
||
382 | * @return DataTablesColumn[] Returns the columns. |
||
383 | */ |
||
384 | public static function parse(array $rawColumns, DataTablesWrapper $wrapper) { |
||
417 | |||
418 | /** |
||
419 | * Set the cell type. |
||
420 | * |
||
421 | * @param string $cellType The cell type. |
||
422 | * @return DataTablesColumn Returns this column. |
||
423 | */ |
||
424 | public function setCellType($cellType) { |
||
431 | |||
432 | /** |
||
433 | * Set the class name. |
||
434 | * |
||
435 | * @param string $classname The class name. |
||
436 | * @return DataTablesColumn Returns this column. |
||
437 | */ |
||
438 | public function setClassname($classname) { |
||
442 | |||
443 | /** |
||
444 | * Set the content padding. |
||
445 | * |
||
446 | * @param string $contentPadding The content padding. |
||
447 | * @return DataTablesColumn Returns this column. |
||
448 | */ |
||
449 | public function setContentPadding($contentPadding) { |
||
453 | |||
454 | /** |
||
455 | * Set the data. |
||
456 | * |
||
457 | * @param integer|string $data The data. |
||
458 | * @return DataTablesColumn Returns this column. |
||
459 | */ |
||
460 | protected function setData($data) { |
||
464 | |||
465 | /** |
||
466 | * Set the default content. |
||
467 | * |
||
468 | * @param string $defaultContent The default content. |
||
469 | * @return DataTablesColumn Returns this column. |
||
470 | */ |
||
471 | public function setDefaultContent($defaultContent) { |
||
475 | |||
476 | /** |
||
477 | * Set the mapping. |
||
478 | * |
||
479 | * @param DataTablesMapping $mapping The mapping. |
||
480 | * @return DataTablesColumn Returns this column. |
||
481 | */ |
||
482 | protected function setMapping(DataTablesMapping $mapping) { |
||
486 | |||
487 | /** |
||
488 | * Set the name. |
||
489 | * |
||
490 | * @param string $name The name. |
||
491 | * @return DataTablesColumn Returns this column. |
||
492 | */ |
||
493 | public function setName($name) { |
||
497 | |||
498 | /** |
||
499 | * Set the order data. |
||
500 | * |
||
501 | * @param integer|array $orderData The order data. |
||
502 | * @return DataTablesColumn Returns this column. |
||
503 | */ |
||
504 | public function setOrderData($orderData) { |
||
508 | |||
509 | /** |
||
510 | * Set the order data type. |
||
511 | * |
||
512 | * @param string $orderDataType The order data type. |
||
513 | * @return DataTablesColumn Returns this column. |
||
514 | */ |
||
515 | public function setOrderDataType($orderDataType) { |
||
519 | |||
520 | /** |
||
521 | * Set the order sequence. |
||
522 | * |
||
523 | * @param string $orderSequence The order sequence. |
||
524 | * @return DataTablesColumn Returns this column. |
||
525 | */ |
||
526 | public function setOrderSequence($orderSequence) { |
||
533 | |||
534 | /** |
||
535 | * Set the orderable. |
||
536 | * |
||
537 | * @param boolean $orderable The orderable. |
||
538 | * @return DataTablesColumn Returns this column. |
||
539 | */ |
||
540 | public function setOrderable($orderable) { |
||
544 | |||
545 | /** |
||
546 | * Set the search. |
||
547 | * |
||
548 | * @param DataTablesSearch $search The search. |
||
549 | * @return DataTablesColumn Returns this column. |
||
550 | */ |
||
551 | protected function setSearch(DataTablesSearch $search) { |
||
555 | |||
556 | /** |
||
557 | * Set the searchable. |
||
558 | * |
||
559 | * @param boolean $searchable The searchable. |
||
560 | * @return DataTablesColumn Returns this column. |
||
561 | */ |
||
562 | public function setSearchable($searchable) { |
||
566 | |||
567 | /** |
||
568 | * Set the title. |
||
569 | * |
||
570 | * @param string $title The title. |
||
571 | * @return DataTablesColumn Returns this column. |
||
572 | */ |
||
573 | public function setTitle($title) { |
||
577 | |||
578 | /** |
||
579 | * Set the type. |
||
580 | * |
||
581 | * @param string $type The type. |
||
582 | * @return DataTablesColumn Returns this column. |
||
583 | */ |
||
584 | public function setType($type) { |
||
591 | |||
592 | /** |
||
593 | * Set the visible. |
||
594 | * |
||
595 | * @param boolean $visible The visible. |
||
596 | * @return DataTablesColumn Returns this column. |
||
597 | */ |
||
598 | public function setVisible($visible) { |
||
602 | |||
603 | /** |
||
604 | * Set the width. |
||
605 | * |
||
606 | * @param string $width The width. |
||
607 | * @return DataTablesColumn Returns this column. |
||
608 | */ |
||
609 | public function setWidth($width) { |
||
613 | |||
614 | /** |
||
615 | * Convert into an array representing this instance. |
||
616 | * |
||
617 | * @return array Returns an array representing this instance. |
||
618 | */ |
||
619 | public function toArray() { |
||
642 | |||
643 | } |
||
644 |