We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Complex classes like Columns 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 Columns, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | trait Columns |
||
6 | { |
||
7 | // ------------ |
||
8 | // COLUMNS |
||
9 | // ------------ |
||
10 | |||
11 | public $actions_column_priority = 1; |
||
12 | |||
13 | /** |
||
14 | * Get the CRUD columns. |
||
15 | * |
||
16 | 3 | * @return array CRUD columns. |
|
17 | */ |
||
18 | 3 | public function getColumns() |
|
22 | |||
23 | /** |
||
24 | * Add a bunch of column names and their details to the CRUD object. |
||
25 | * |
||
26 | * @param [array or multi-dimensional array] |
||
27 | */ |
||
28 | public function setColumns($columns) |
||
60 | |||
61 | /** |
||
62 | * Add a column at the end of to the CRUD object's "columns" array. |
||
63 | * |
||
64 | 21 | * @param [string or array] |
|
65 | */ |
||
66 | public function addColumn($column) |
||
126 | 16 | ||
127 | 15 | /** |
|
128 | * Add multiple columns at the end of the CRUD object's "columns" array. |
||
129 | * |
||
130 | 15 | * @param [array of columns] |
|
131 | */ |
||
132 | public function addColumns($columns) |
||
140 | 2 | ||
141 | /** |
||
142 | * Move the most recently added column after the given target column. |
||
143 | * |
||
144 | * @param string|array $targetColumn The target column name or array. |
||
145 | */ |
||
146 | public function afterColumn($targetColumn) |
||
150 | 2 | ||
151 | /** |
||
152 | * Move the most recently added column before the given target column. |
||
153 | * |
||
154 | * @param string|array $targetColumn The target column name or array. |
||
155 | */ |
||
156 | public function beforeColumn($targetColumn) |
||
160 | |||
161 | /** |
||
162 | * Move this column to be first in the columns list. |
||
163 | */ |
||
164 | public function makeFirstColumn() |
||
173 | |||
174 | /** |
||
175 | 4 | * Move the most recently added column before or after the given target column. Default is before. |
|
176 | * |
||
177 | 4 | * @param string|array $targetColumn The target column name or array. |
|
178 | 2 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
|
179 | 2 | */ |
|
180 | private function moveColumn($targetColumn, $before = true) |
||
197 | |||
198 | /** |
||
199 | * Add the default column type to the given Column, inferring the type from the database column type. |
||
200 | * |
||
201 | * @param [column array] |
||
202 | */ |
||
203 | public function addDefaultTypeToColumn($column) |
||
213 | 21 | ||
214 | 7 | /** |
|
215 | * If a field or column array is missing the "label" attribute, an ugly error would be show. |
||
216 | 7 | * So we add the field Name as a label - it's better than nothing. |
|
217 | * |
||
218 | * @param [field or column] |
||
219 | 14 | */ |
|
220 | public function addDefaultLabel($array) |
||
230 | 4 | ||
231 | /** |
||
232 | * Remove a column from the CRUD panel by name. |
||
233 | * |
||
234 | * @param string $column The column name. |
||
235 | */ |
||
236 | public function removeColumn($column) |
||
240 | 2 | ||
241 | 2 | /** |
|
242 | * Remove multiple columns from the CRUD panel by name. |
||
243 | * |
||
244 | 2 | * @param array $columns Array of column names. |
|
245 | */ |
||
246 | public function removeColumns($columns) |
||
254 | |||
255 | /** |
||
256 | * Remove an entry from an array. |
||
257 | * |
||
258 | * @param string $entity |
||
259 | * @param array $fields |
||
260 | * @return array values |
||
261 | * |
||
262 | * @deprecated This method is no longer used by internal code and is not recommended as it does not preserve the |
||
263 | * target array keys. |
||
264 | * @see Columns::removeColumn() to remove a column from the CRUD panel by name. |
||
265 | * @see Columns::removeColumns() to remove multiple columns from the CRUD panel by name. |
||
266 | */ |
||
267 | public function remove($entity, $fields) |
||
273 | |||
274 | /** |
||
275 | * Change attributes for multiple columns. |
||
276 | * |
||
277 | * @param [columns arrays] |
||
278 | * @param [attributes and values array] |
||
279 | */ |
||
280 | public function setColumnsDetails($columns, $attributes) |
||
284 | |||
285 | /** |
||
286 | * Change attributes for a certain column. |
||
287 | * |
||
288 | * @param [string] Column name. |
||
289 | * @param [attributes and values array] |
||
290 | */ |
||
291 | public function setColumnDetails($column, $attributes) |
||
295 | |||
296 | /** |
||
297 | * Alias for setColumnDetails(). |
||
298 | * Provides a consistent syntax with Fields, Buttons, Filters modify functionality. |
||
299 | * |
||
300 | * @param [string] Column name. |
||
301 | * @param [attributes and values array] |
||
302 | 3 | */ |
|
303 | public function modifyColumn($column, $attributes) |
||
307 | 2 | ||
308 | 3 | /** |
|
309 | * Set label for a specific column. |
||
310 | * |
||
311 | * @param string $column |
||
312 | * @param string $label |
||
313 | */ |
||
314 | public function setColumnLabel($column, $label) |
||
318 | |||
319 | 5 | /** |
|
320 | 5 | * Get the relationships used in the CRUD columns. |
|
321 | 4 | * @return [array] Relationship names |
|
322 | 4 | */ |
|
323 | public function getColumnsRelationships() |
||
331 | 3 | ||
332 | 3 | /** |
|
333 | * Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the |
||
334 | * new columns array in the original order. |
||
335 | * |
||
336 | * @param array $order An array of column names in the desired order. |
||
337 | */ |
||
338 | public function orderColumns($order) |
||
354 | |||
355 | /** |
||
356 | * Set the order of the CRUD columns. |
||
357 | * |
||
358 | * @param array $columns Column order. |
||
359 | * |
||
360 | * @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
||
361 | * @see Columns::orderColumns() to order the CRUD columns. |
||
362 | */ |
||
363 | public function setColumnOrder($columns) |
||
367 | |||
368 | /** |
||
369 | * Set the order of the CRUD columns. |
||
370 | * |
||
371 | * @param array $columns Column order. |
||
372 | 21 | * |
|
373 | * @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
||
374 | 21 | * @see Columns::orderColumns() to order the CRUD columns. |
|
375 | */ |
||
376 | 21 | public function setColumnsOrder($columns) |
|
380 | |||
381 | /** |
||
382 | 21 | * Get a column by the id, from the associative array. |
|
383 | * @param [integer] $column_number Placement inside the columns array. |
||
384 | * @return [array] Column details. |
||
385 | */ |
||
386 | public function findColumnById($column_number) |
||
392 | |||
393 | protected function hasColumn($table, $name) |
||
405 | |||
406 | /** |
||
407 | * Get the visibility priority for the actions column |
||
408 | * in the CRUD table view. |
||
409 | * |
||
410 | * @return int The priority, from 1 to infinity. Lower is better. |
||
411 | */ |
||
412 | public function getActionsColumnPriority() |
||
416 | |||
417 | /** |
||
418 | * Set a certain priority for the actions column |
||
419 | * in the CRUD table view. Usually set to 10000 in order to hide it. |
||
420 | * |
||
421 | * @param int $number The priority, from 1 to infinity. Lower is better. |
||
422 | */ |
||
423 | public function setActionsColumnPriority($number) |
||
429 | } |
||
430 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: