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 |
||
20 | class Column extends Fluent |
||
21 | { |
||
22 | /** |
||
23 | * @param array $attributes |
||
24 | */ |
||
25 | public function __construct($attributes = []) |
||
49 | |||
50 | /** |
||
51 | * Format string to title case. |
||
52 | * |
||
53 | * @param string $value |
||
54 | * @return string |
||
55 | */ |
||
56 | public static function titleFormat($value) |
||
60 | |||
61 | /** |
||
62 | * Create a computed column that is not searchable/orderable. |
||
63 | * |
||
64 | * @param string $data |
||
65 | * @param string|null $title |
||
66 | * @return Column |
||
67 | */ |
||
68 | public static function computed($data, $title = null) |
||
76 | |||
77 | /** |
||
78 | * Set column searchable flag. |
||
79 | * |
||
80 | * @param bool $flag |
||
81 | * @return $this |
||
82 | * @see https://datatables.net/reference/option/columns.searchable |
||
83 | */ |
||
84 | public function searchable(bool $flag = true) |
||
90 | |||
91 | /** |
||
92 | * Set column orderable flag. |
||
93 | * |
||
94 | * @param bool $flag |
||
95 | * @return $this |
||
96 | * @see https://datatables.net/reference/option/columns.orderable |
||
97 | */ |
||
98 | public function orderable(bool $flag = true) |
||
104 | |||
105 | /** |
||
106 | * Set column title. |
||
107 | * |
||
108 | * @param string $value |
||
109 | * @return $this |
||
110 | * @see https://datatables.net/reference/option/columns.title |
||
111 | */ |
||
112 | public function title($value) |
||
118 | |||
119 | /** |
||
120 | * Make a new column instance. |
||
121 | * |
||
122 | * @param string $data |
||
123 | * @param string $name |
||
124 | * @return Column |
||
125 | */ |
||
126 | public static function make($data, $name = '') |
||
135 | |||
136 | /** |
||
137 | * Create a checkbox column. |
||
138 | * |
||
139 | * @param string $title |
||
140 | * @return Column |
||
141 | */ |
||
142 | public static function checkbox($title = '') |
||
151 | |||
152 | /** |
||
153 | * Set column class name. |
||
154 | * |
||
155 | * @param string $class |
||
156 | * @return $this |
||
157 | * @see https://datatables.net/reference/option/columns.className |
||
158 | */ |
||
159 | public function className($class) |
||
165 | |||
166 | /** |
||
167 | * Set column default content. |
||
168 | * |
||
169 | * @param string $value |
||
170 | * @return $this |
||
171 | * @see https://datatables.net/reference/option/columns.defaultContent |
||
172 | */ |
||
173 | public function content($value) |
||
179 | |||
180 | /** |
||
181 | * Set column visible flag. |
||
182 | * |
||
183 | * @param bool $flag |
||
184 | * @return $this |
||
185 | * @see https://datatables.net/reference/option/columns.visible |
||
186 | */ |
||
187 | public function visible(bool $flag = true) |
||
193 | |||
194 | /** |
||
195 | * Set column hidden state. |
||
196 | * |
||
197 | * @return $this |
||
198 | * @see https://datatables.net/reference/option/columns.visible |
||
199 | */ |
||
200 | public function hidden() |
||
204 | |||
205 | /** |
||
206 | * Append a class name to field. |
||
207 | * |
||
208 | * @param string $class |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function addClass($class) |
||
221 | |||
222 | /** |
||
223 | * Set column exportable flag. |
||
224 | * |
||
225 | * @param bool $flag |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function exportable(bool $flag = true) |
||
234 | |||
235 | /** |
||
236 | * Set column printable flag. |
||
237 | * |
||
238 | * @param bool $flag |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function printable(bool $flag = true) |
||
247 | |||
248 | /** |
||
249 | * Set column width value. |
||
250 | * |
||
251 | * @param int|string $value |
||
252 | * @return $this |
||
253 | * @see https://datatables.net/reference/option/columns.width |
||
254 | */ |
||
255 | public function width($value) |
||
261 | |||
262 | /** |
||
263 | * Set column data option value. |
||
264 | * |
||
265 | * @param string $value |
||
266 | * @return $this |
||
267 | * @see https://datatables.net/reference/option/columns.data |
||
268 | */ |
||
269 | public function data($value) |
||
275 | |||
276 | /** |
||
277 | * Set column name option value. |
||
278 | * |
||
279 | * @param string $value |
||
280 | * @return $this |
||
281 | * @see https://datatables.net/reference/option/columns.name |
||
282 | */ |
||
283 | public function name($value) |
||
289 | |||
290 | /** |
||
291 | * Set column edit field option value. |
||
292 | * |
||
293 | * @param string $value |
||
294 | * @return $this |
||
295 | * @see https://datatables.net/reference/option/columns.editField |
||
296 | */ |
||
297 | public function editField($value) |
||
303 | |||
304 | /** |
||
305 | * Set column orderData option value. |
||
306 | * |
||
307 | * @param mixed $value |
||
308 | * @return $this |
||
309 | * @see https://datatables.net/reference/option/columns.orderData |
||
310 | */ |
||
311 | public function orderData($value) |
||
317 | |||
318 | /** |
||
319 | * Set column orderDataType option value. |
||
320 | * |
||
321 | * @param mixed $value |
||
322 | * @return $this |
||
323 | * @see https://datatables.net/reference/option/columns.orderDataType |
||
324 | */ |
||
325 | public function orderDataType($value) |
||
331 | |||
332 | /** |
||
333 | * Set column orderSequence option value. |
||
334 | * |
||
335 | * @param mixed $value |
||
336 | * @return $this |
||
337 | * @see https://datatables.net/reference/option/columns.orderSequence |
||
338 | */ |
||
339 | public function orderSequence($value) |
||
345 | |||
346 | /** |
||
347 | * Set column cellType option value. |
||
348 | * |
||
349 | * @param mixed $value |
||
350 | * @return $this |
||
351 | * @see https://datatables.net/reference/option/columns.cellType |
||
352 | */ |
||
353 | public function cellType($value) |
||
359 | |||
360 | /** |
||
361 | * Set column type option value. |
||
362 | * |
||
363 | * @param mixed $value |
||
364 | * @return $this |
||
365 | * @see https://datatables.net/reference/option/columns.type |
||
366 | */ |
||
367 | public function type($value) |
||
373 | |||
374 | /** |
||
375 | * Set column contentPadding option value. |
||
376 | * |
||
377 | * @param mixed $value |
||
378 | * @return $this |
||
379 | * @see https://datatables.net/reference/option/columns.contentPadding |
||
380 | */ |
||
381 | public function contentPadding($value) |
||
387 | |||
388 | /** |
||
389 | * Set column createdCell option value. |
||
390 | * |
||
391 | * @param mixed $value |
||
392 | * @return $this |
||
393 | * @see https://datatables.net/reference/option/columns.createdCell |
||
394 | */ |
||
395 | public function createdCell($value) |
||
401 | |||
402 | /** |
||
403 | * Use the js renderer "$.fn.dataTable.render.". |
||
404 | * |
||
405 | * @param mixed $value |
||
406 | * @param mixed ...$params |
||
407 | * @return $this |
||
408 | * @see https://datatables.net/reference/option/columns.render |
||
409 | */ |
||
410 | public function renderJs($value, ...$params) |
||
425 | |||
426 | /** |
||
427 | * Set column renderer. |
||
428 | * |
||
429 | * @param mixed $value |
||
430 | * @return $this |
||
431 | * @see https://datatables.net/reference/option/columns.render |
||
432 | */ |
||
433 | public function render($value) |
||
439 | |||
440 | /** |
||
441 | * Parse render attribute. |
||
442 | * |
||
443 | * @param mixed $value |
||
444 | * @return string|null |
||
445 | */ |
||
446 | public function parseRender($value) |
||
467 | |||
468 | /** |
||
469 | * Check if given key & value is a valid datatables built-in renderer function. |
||
470 | * |
||
471 | * @param string $value |
||
472 | * @return bool |
||
473 | */ |
||
474 | private function isBuiltInRenderFunction($value) |
||
482 | |||
483 | /** |
||
484 | * Display render value as is. |
||
485 | * |
||
486 | * @param mixed $value |
||
487 | * @return string |
||
488 | */ |
||
489 | private function parseRenderAsString($value) |
||
493 | |||
494 | /** |
||
495 | * Set column footer. |
||
496 | * |
||
497 | * @param mixed $value |
||
498 | * @return $this |
||
499 | */ |
||
500 | public function footer($value) |
||
506 | |||
507 | /** |
||
508 | * @return array |
||
509 | */ |
||
510 | public function toArray() |
||
518 | } |
||
519 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.