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) |
||
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 = '') |
||
75 | |||
76 | /** |
||
77 | * Set column searchable flag. |
||
78 | * |
||
79 | * @param bool $flag |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function searchable(bool $flag = true) |
||
88 | |||
89 | /** |
||
90 | * Set column orderable flag. |
||
91 | * |
||
92 | * @param bool $flag |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function orderable(bool $flag = true) |
||
101 | |||
102 | /** |
||
103 | * Set column title. |
||
104 | * |
||
105 | * @param string $value |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function title($value) |
||
114 | |||
115 | /** |
||
116 | * Make a new column instance. |
||
117 | * |
||
118 | * @param string $data |
||
119 | * @param string $name |
||
120 | * @return Column |
||
121 | */ |
||
122 | public static function make($data, $name = '') |
||
131 | |||
132 | /** |
||
133 | * Create a checkbox column. |
||
134 | * |
||
135 | * @param string $title |
||
136 | * @return Column |
||
137 | */ |
||
138 | public static function checkbox($title = '') |
||
147 | |||
148 | /** |
||
149 | * Set column class name. |
||
150 | * |
||
151 | * @param string $class |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function className($class) |
||
160 | |||
161 | /** |
||
162 | * Set column default content. |
||
163 | * |
||
164 | * @param string $value |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function content($value) |
||
173 | |||
174 | /** |
||
175 | * Set column visible flag. |
||
176 | * |
||
177 | * @param bool $flag |
||
178 | * @return $this |
||
179 | */ |
||
180 | public function visible(bool $flag = true) |
||
186 | |||
187 | /** |
||
188 | * Append a class name to field. |
||
189 | * |
||
190 | * @param string $class |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function addClass($class) |
||
203 | |||
204 | /** |
||
205 | * Set column exportable flag. |
||
206 | * |
||
207 | * @param bool $flag |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function exportable(bool $flag = true) |
||
216 | |||
217 | /** |
||
218 | * Set column printable flag. |
||
219 | * |
||
220 | * @param bool $flag |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function printable(bool $flag = true) |
||
229 | |||
230 | /** |
||
231 | * Set column width value. |
||
232 | * |
||
233 | * @param int|string $value |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function width($value) |
||
242 | |||
243 | /** |
||
244 | * Set column name. |
||
245 | * |
||
246 | * @param string $value |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function name($value) |
||
255 | |||
256 | /** |
||
257 | * Set column renderer. |
||
258 | * |
||
259 | * @param mixed $value |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function render($value) |
||
268 | |||
269 | /** |
||
270 | * Parse render attribute. |
||
271 | * |
||
272 | * @param mixed $value |
||
273 | * @return string|null |
||
274 | */ |
||
275 | public function parseRender($value) |
||
296 | |||
297 | /** |
||
298 | * Check if given key & value is a valid datatables built-in renderer function. |
||
299 | * |
||
300 | * @param string $value |
||
301 | * @return bool |
||
302 | */ |
||
303 | private function isBuiltInRenderFunction($value) |
||
311 | |||
312 | /** |
||
313 | * Display render value as is. |
||
314 | * |
||
315 | * @param mixed $value |
||
316 | * @return string |
||
317 | */ |
||
318 | private function parseRenderAsString($value) |
||
322 | |||
323 | /** |
||
324 | * @return array |
||
325 | */ |
||
326 | public function toArray() |
||
330 | } |
||
331 |