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 | /** |
||
12 | * Get the CRUD columns. |
||
13 | * |
||
14 | * @return array CRUD columns. |
||
15 | */ |
||
16 | public function getColumns() |
||
20 | |||
21 | /** |
||
22 | * Add a bunch of column names and their details to the CRUD object. |
||
23 | * |
||
24 | * @param [array or multi-dimensional array] |
||
25 | */ |
||
26 | public function setColumns($columns) |
||
58 | 6 | ||
59 | /** |
||
60 | * Add a column at the end of to the CRUD object's "columns" array. |
||
61 | * |
||
62 | 14 | * @param [string or array] |
|
63 | */ |
||
64 | public function addColumn($column) |
||
108 | |||
109 | 2 | /** |
|
110 | * Add multiple columns at the end of the CRUD object's "columns" array. |
||
111 | 2 | * |
|
112 | 2 | * @param [array of columns] |
|
113 | */ |
||
114 | public function addColumns($columns) |
||
122 | |||
123 | /** |
||
124 | 4 | * Move the most recently added column after the given target column. |
|
125 | * |
||
126 | 4 | * @param string|array $targetColumn The target column name or array. |
|
127 | 2 | */ |
|
128 | 2 | public function afterColumn($targetColumn) |
|
132 | 2 | ||
133 | /** |
||
134 | 2 | * Move the most recently added column before the given target column. |
|
135 | * |
||
136 | 4 | * @param string|array $targetColumn The target column name or array. |
|
137 | */ |
||
138 | public function beforeColumn($targetColumn) |
||
142 | |||
143 | /** |
||
144 | * Move the most recently added column before or after the given target column. Default is before. |
||
145 | * |
||
146 | * @param string|array $targetColumn The target column name or array. |
||
147 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
||
148 | */ |
||
149 | private function moveColumn($targetColumn, $before = true) |
||
166 | |||
167 | /** |
||
168 | 7 | * Add the default column type to the given Column, inferring the type from the database column type. |
|
169 | * |
||
170 | * @param [column array] |
||
171 | */ |
||
172 | public function addDefaultTypeToColumn($column) |
||
182 | |||
183 | /** |
||
184 | * If a field or column array is missing the "label" attribute, an ugly error would be show. |
||
185 | * So we add the field Name as a label - it's better than nothing. |
||
186 | 2 | * |
|
187 | * @param [field or column] |
||
188 | 2 | */ |
|
189 | 2 | public function addDefaultLabel($array) |
|
199 | |||
200 | /** |
||
201 | * Remove a column from the CRUD panel by name. |
||
202 | * |
||
203 | * @param string $column The column name. |
||
204 | */ |
||
205 | public function removeColumn($column) |
||
209 | |||
210 | /** |
||
211 | * Remove multiple columns from the CRUD panel by name. |
||
212 | * |
||
213 | * @param array $columns Array of column names. |
||
214 | */ |
||
215 | public function removeColumns($columns) |
||
223 | |||
224 | /** |
||
225 | * Remove an entry from an array. |
||
226 | * |
||
227 | * @param string $entity |
||
228 | * @param array $fields |
||
229 | * @return array values |
||
230 | * |
||
231 | * @deprecated This method is no longer used by internal code and is not recommended as it does not preserve the |
||
232 | * target array keys. |
||
233 | * @see Columns::removeColumn() to remove a column from the CRUD panel by name. |
||
234 | * @see Columns::removeColumns() to remove multiple columns from the CRUD panel by name. |
||
235 | */ |
||
236 | public function remove($entity, $fields) |
||
242 | |||
243 | /** |
||
244 | * Change attributes for multiple columns. |
||
245 | * |
||
246 | * @param [columns arrays] |
||
247 | * @param [attributes and values array] |
||
248 | */ |
||
249 | public function setColumnsDetails($columns, $attributes) |
||
253 | |||
254 | /** |
||
255 | * Change attributes for a certain column. |
||
256 | * |
||
257 | * @param [string] Column name. |
||
258 | * @param [attributes and values array] |
||
259 | */ |
||
260 | public function setColumnDetails($column, $attributes) |
||
264 | |||
265 | /** |
||
266 | * Set label for a specific column. |
||
267 | * |
||
268 | 3 | * @param string $column |
|
269 | * @param string $label |
||
270 | 3 | */ |
|
271 | public function setColumnLabel($column, $label) |
||
275 | |||
276 | /** |
||
277 | * Get the relationships used in the CRUD columns. |
||
278 | * @return [array] Relationship names |
||
279 | */ |
||
280 | public function getColumnsRelationships() |
||
288 | |||
289 | /** |
||
290 | * Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the |
||
291 | * new columns array in the original order. |
||
292 | * |
||
293 | * @param array $order An array of column names in the desired order. |
||
294 | */ |
||
295 | public function orderColumns($order) |
||
311 | |||
312 | /** |
||
313 | * Set the order of the CRUD columns. |
||
314 | * |
||
315 | * @param array $columns Column order. |
||
316 | * |
||
317 | * @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
||
318 | * @see Columns::orderColumns() to order the CRUD columns. |
||
319 | */ |
||
320 | public function setColumnOrder($columns) |
||
324 | |||
325 | /** |
||
326 | * Set the order of the CRUD columns. |
||
327 | * |
||
328 | * @param array $columns Column order. |
||
329 | * |
||
330 | * @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
||
331 | * @see Columns::orderColumns() to order the CRUD columns. |
||
332 | */ |
||
333 | public function setColumnsOrder($columns) |
||
337 | |||
338 | /** |
||
339 | * Get a column by the id, from the associative array. |
||
340 | * @param [integer] $column_number Placement inside the columns array. |
||
341 | * @return [array] Column details. |
||
342 | */ |
||
343 | public function findColumnById($column_number) |
||
349 | } |
||
350 |
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: