Complex classes like Column 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 Column, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Column extends Fluent |
||
20 | { |
||
21 | /** |
||
22 | * @param array $attributes |
||
23 | */ |
||
24 | public function __construct($attributes = []) |
||
48 | |||
49 | /** |
||
50 | * Format string to title case. |
||
51 | * |
||
52 | * @param string $value |
||
53 | * @return string |
||
54 | */ |
||
55 | public static function titleFormat($value) |
||
56 | { |
||
57 | return Str::title(str_replace('_', ' ', $value)); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Create a computed column that is not searchable/orderable. |
||
62 | * |
||
63 | * @param string $data |
||
64 | * @param string $title |
||
65 | * @return Column |
||
66 | */ |
||
67 | public static function computed($data, $title = '') |
||
68 | { |
||
69 | if (empty($title)) { |
||
70 | $title = self::titleFormat($data); |
||
71 | } |
||
72 | |||
73 | return static::make($data)->title($title)->orderable(false)->searchable(false); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Set column searchable flag. |
||
78 | * |
||
79 | * @param bool $flag |
||
80 | * @return $this |
||
81 | * @see https://datatables.net/reference/option/columns.searchable |
||
82 | */ |
||
83 | public function searchable(bool $flag = true) |
||
84 | { |
||
85 | $this->attributes['searchable'] = $flag; |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Set column orderable flag. |
||
92 | * |
||
93 | * @param bool $flag |
||
94 | * @return $this |
||
95 | * @see https://datatables.net/reference/option/columns.orderable |
||
96 | */ |
||
97 | public function orderable(bool $flag = true) |
||
98 | { |
||
99 | $this->attributes['orderable'] = $flag; |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Set column title. |
||
106 | * |
||
107 | * @param string $value |
||
108 | * @return $this |
||
109 | * @see https://datatables.net/reference/option/columns.title |
||
110 | */ |
||
111 | public function title($value) |
||
112 | { |
||
113 | $this->attributes['title'] = $value; |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Make a new column instance. |
||
120 | * |
||
121 | * @param string $data |
||
122 | * @param string $name |
||
123 | * @return Column |
||
124 | */ |
||
125 | public static function make($data, $name = '') |
||
126 | { |
||
127 | $attr = [ |
||
128 | 'data' => $data, |
||
129 | 'name' => $name ?: $data, |
||
130 | ]; |
||
131 | |||
132 | return new static($attr); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Create a checkbox column. |
||
137 | * |
||
138 | * @param string $title |
||
139 | * @return Column |
||
140 | */ |
||
141 | public static function checkbox($title = '') |
||
142 | { |
||
143 | return static::make('') |
||
144 | ->content('') |
||
145 | ->title($title) |
||
146 | ->className('select-checkbox') |
||
147 | ->orderable(false) |
||
148 | ->searchable(false); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Set column class name. |
||
153 | * |
||
154 | * @param string $class |
||
155 | * @return $this |
||
156 | * @see https://datatables.net/reference/option/columns.className |
||
157 | */ |
||
158 | public function className($class) |
||
159 | { |
||
160 | $this->attributes['className'] = $class; |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Set column default content. |
||
167 | * |
||
168 | * @param string $value |
||
169 | * @return $this |
||
170 | * @see https://datatables.net/reference/option/columns.defaultContent |
||
171 | */ |
||
172 | public function content($value) |
||
178 | |||
179 | /** |
||
180 | * Set column visible flag. |
||
181 | * |
||
182 | * @param bool $flag |
||
183 | * @return $this |
||
184 | * @see https://datatables.net/reference/option/columns.visible |
||
185 | */ |
||
186 | public function visible(bool $flag = true) |
||
192 | |||
193 | /** |
||
194 | * Append a class name to field. |
||
195 | * |
||
196 | * @param string $class |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function addClass($class) |
||
209 | |||
210 | /** |
||
211 | * Set column exportable flag. |
||
212 | * |
||
213 | * @param bool $flag |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function exportable(bool $flag = true) |
||
222 | |||
223 | /** |
||
224 | * Set column printable flag. |
||
225 | * |
||
226 | * @param bool $flag |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function printable(bool $flag = true) |
||
235 | |||
236 | /** |
||
237 | * Set column width value. |
||
238 | * |
||
239 | * @param int|string $value |
||
240 | * @return $this |
||
241 | * @see https://datatables.net/reference/option/columns.width |
||
242 | */ |
||
243 | public function width($value) |
||
249 | |||
250 | /** |
||
251 | * Set column data option value. |
||
252 | * |
||
253 | * @param string $value |
||
254 | * @return $this |
||
255 | * @see https://datatables.net/reference/option/columns.data |
||
256 | */ |
||
257 | public function data($value) |
||
263 | |||
264 | /** |
||
265 | * Set column name option value. |
||
266 | * |
||
267 | * @param string $value |
||
268 | * @return $this |
||
269 | * @see https://datatables.net/reference/option/columns.name |
||
270 | */ |
||
271 | public function name($value) |
||
277 | |||
278 | /** |
||
279 | * Set column edit field option value. |
||
280 | * |
||
281 | * @param string $value |
||
282 | * @return $this |
||
283 | * @see https://datatables.net/reference/option/columns.editField |
||
284 | */ |
||
285 | public function editField($value) |
||
291 | |||
292 | /** |
||
293 | * Set column orderData option value. |
||
294 | * |
||
295 | * @param mixed $value |
||
296 | * @return $this |
||
297 | * @see https://datatables.net/reference/option/columns.orderData |
||
298 | */ |
||
299 | public function orderData($value) |
||
305 | |||
306 | /** |
||
307 | * Set column orderDataType option value. |
||
308 | * |
||
309 | * @param mixed $value |
||
310 | * @return $this |
||
311 | * @see https://datatables.net/reference/option/columns.orderDataType |
||
312 | */ |
||
313 | public function orderDataType($value) |
||
319 | |||
320 | /** |
||
321 | * Set column orderSequence option value. |
||
322 | * |
||
323 | * @param mixed $value |
||
324 | * @return $this |
||
325 | * @see https://datatables.net/reference/option/columns.orderSequence |
||
326 | */ |
||
327 | public function orderSequence($value) |
||
333 | |||
334 | /** |
||
335 | * Set column cellType option value. |
||
336 | * |
||
337 | * @param mixed $value |
||
338 | * @return $this |
||
339 | * @see https://datatables.net/reference/option/columns.cellType |
||
340 | */ |
||
341 | public function cellType($value) |
||
347 | |||
348 | /** |
||
349 | * Set column type option value. |
||
350 | * |
||
351 | * @param mixed $value |
||
352 | * @return $this |
||
353 | * @see https://datatables.net/reference/option/columns.type |
||
354 | */ |
||
355 | public function type($value) |
||
361 | |||
362 | /** |
||
363 | * Set column contentPadding option value. |
||
364 | * |
||
365 | * @param mixed $value |
||
366 | * @return $this |
||
367 | * @see https://datatables.net/reference/option/columns.contentPadding |
||
368 | */ |
||
369 | public function contentPadding($value) |
||
375 | |||
376 | /** |
||
377 | * Set column createdCell option value. |
||
378 | * |
||
379 | * @param mixed $value |
||
380 | * @return $this |
||
381 | * @see https://datatables.net/reference/option/columns.createdCell |
||
382 | */ |
||
383 | public function createdCell($value) |
||
389 | |||
390 | /** |
||
391 | * Set column renderer. |
||
392 | * |
||
393 | * @param mixed $value |
||
394 | * @return $this |
||
395 | * @see https://datatables.net/reference/option/columns.render |
||
396 | */ |
||
397 | public function render($value) |
||
403 | |||
404 | /** |
||
405 | * Parse render attribute. |
||
406 | * |
||
407 | * @param mixed $value |
||
408 | * @return string|null |
||
409 | */ |
||
410 | public function parseRender($value) |
||
431 | |||
432 | /** |
||
433 | * Check if given key & value is a valid datatables built-in renderer function. |
||
434 | * |
||
435 | * @param string $value |
||
436 | * @return bool |
||
437 | */ |
||
438 | private function isBuiltInRenderFunction($value) |
||
446 | |||
447 | /** |
||
448 | * Display render value as is. |
||
449 | * |
||
450 | * @param mixed $value |
||
451 | * @return string |
||
452 | */ |
||
453 | private function parseRenderAsString($value) |
||
457 | |||
458 | /** |
||
459 | * @return array |
||
460 | */ |
||
461 | public function toArray() |
||
465 | } |
||
466 |