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 | 3 | 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 | |||
59 | /** |
||
60 | * Add a column at the end of to the CRUD object's "columns" array. |
||
61 | * |
||
62 | * @param [string or array] |
||
63 | */ |
||
64 | 21 | public function addColumn($column) |
|
117 | |||
118 | /** |
||
119 | * Add multiple columns at the end of the CRUD object's "columns" array. |
||
120 | * |
||
121 | * @param [array of columns] |
||
122 | */ |
||
123 | 16 | public function addColumns($columns) |
|
131 | |||
132 | /** |
||
133 | * Move the most recently added column after the given target column. |
||
134 | * |
||
135 | * @param string|array $targetColumn The target column name or array. |
||
136 | */ |
||
137 | 2 | public function afterColumn($targetColumn) |
|
141 | |||
142 | /** |
||
143 | * Move the most recently added column before the given target column. |
||
144 | * |
||
145 | * @param string|array $targetColumn The target column name or array. |
||
146 | */ |
||
147 | 2 | public function beforeColumn($targetColumn) |
|
151 | |||
152 | /** |
||
153 | * Move the most recently added column before or after the given target column. Default is before. |
||
154 | * |
||
155 | * @param string|array $targetColumn The target column name or array. |
||
156 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
||
157 | */ |
||
158 | 4 | private function moveColumn($targetColumn, $before = true) |
|
175 | |||
176 | /** |
||
177 | * Add the default column type to the given Column, inferring the type from the database column type. |
||
178 | * |
||
179 | * @param [column array] |
||
180 | */ |
||
181 | public function addDefaultTypeToColumn($column) |
||
191 | |||
192 | /** |
||
193 | * If a field or column array is missing the "label" attribute, an ugly error would be show. |
||
194 | * So we add the field Name as a label - it's better than nothing. |
||
195 | * |
||
196 | * @param [field or column] |
||
197 | */ |
||
198 | 21 | public function addDefaultLabel($array) |
|
208 | |||
209 | /** |
||
210 | * Remove a column from the CRUD panel by name. |
||
211 | * |
||
212 | * @param string $column The column name. |
||
213 | */ |
||
214 | 4 | public function removeColumn($column) |
|
218 | |||
219 | /** |
||
220 | * Remove multiple columns from the CRUD panel by name. |
||
221 | * |
||
222 | * @param array $columns Array of column names. |
||
223 | */ |
||
224 | 2 | public function removeColumns($columns) |
|
232 | |||
233 | /** |
||
234 | * Remove an entry from an array. |
||
235 | * |
||
236 | * @param string $entity |
||
237 | * @param array $fields |
||
238 | * @return array values |
||
239 | * |
||
240 | * @deprecated This method is no longer used by internal code and is not recommended as it does not preserve the |
||
241 | * target array keys. |
||
242 | * @see Columns::removeColumn() to remove a column from the CRUD panel by name. |
||
243 | * @see Columns::removeColumns() to remove multiple columns from the CRUD panel by name. |
||
244 | */ |
||
245 | public function remove($entity, $fields) |
||
251 | |||
252 | /** |
||
253 | * Change attributes for multiple columns. |
||
254 | * |
||
255 | * @param [columns arrays] |
||
256 | * @param [attributes and values array] |
||
257 | */ |
||
258 | public function setColumnsDetails($columns, $attributes) |
||
262 | |||
263 | /** |
||
264 | * Change attributes for a certain column. |
||
265 | * |
||
266 | * @param [string] Column name. |
||
267 | * @param [attributes and values array] |
||
268 | */ |
||
269 | public function setColumnDetails($column, $attributes) |
||
273 | |||
274 | /** |
||
275 | * Set label for a specific column. |
||
276 | * |
||
277 | * @param string $column |
||
278 | * @param string $label |
||
279 | */ |
||
280 | public function setColumnLabel($column, $label) |
||
284 | |||
285 | /** |
||
286 | * Get the relationships used in the CRUD columns. |
||
287 | * @return [array] Relationship names |
||
288 | */ |
||
289 | 3 | public function getColumnsRelationships() |
|
297 | |||
298 | /** |
||
299 | * Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the |
||
300 | * new columns array in the original order. |
||
301 | * |
||
302 | * @param array $order An array of column names in the desired order. |
||
303 | */ |
||
304 | 5 | public function orderColumns($order) |
|
320 | |||
321 | /** |
||
322 | * Set the order of the CRUD columns. |
||
323 | * |
||
324 | * @param array $columns Column order. |
||
325 | * |
||
326 | * @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
||
327 | * @see Columns::orderColumns() to order the CRUD columns. |
||
328 | */ |
||
329 | public function setColumnOrder($columns) |
||
333 | |||
334 | /** |
||
335 | * Set the order of the CRUD columns. |
||
336 | * |
||
337 | * @param array $columns Column order. |
||
338 | * |
||
339 | * @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
||
340 | * @see Columns::orderColumns() to order the CRUD columns. |
||
341 | */ |
||
342 | public function setColumnsOrder($columns) |
||
346 | |||
347 | /** |
||
348 | * Get a column by the id, from the associative array. |
||
349 | * @param [integer] $column_number Placement inside the columns array. |
||
350 | * @return [array] Column details. |
||
351 | */ |
||
352 | public function findColumnById($column_number) |
||
358 | |||
359 | 21 | protected function hasColumn($table, $name) |
|
371 | } |
||
372 |
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: