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) |
|
| 65 | { |
||
| 66 | // if a string was passed, not an array, change it to an array |
||
| 67 | 21 | if (! is_array($column)) { |
|
| 68 | 6 | $column = ['name' => $column]; |
|
| 69 | } |
||
| 70 | |||
| 71 | // make sure the column has a label |
||
| 72 | 21 | $column_with_details = $this->addDefaultLabel($column); |
|
| 73 | |||
| 74 | // make sure the column has a name |
||
| 75 | 21 | if (! array_key_exists('name', $column_with_details)) { |
|
| 76 | $column_with_details['name'] = 'anonymous_column_'.str_random(5); |
||
| 77 | } |
||
| 78 | |||
| 79 | // make sure the column has a type |
||
| 80 | 21 | if (! array_key_exists('type', $column_with_details)) { |
|
| 81 | 18 | $column_with_details['type'] = 'text'; |
|
| 82 | } |
||
| 83 | |||
| 84 | // make sure the column has a key |
||
| 85 | 21 | if (! array_key_exists('key', $column_with_details)) { |
|
| 86 | 21 | $column_with_details['key'] = $column_with_details['name']; |
|
| 87 | } |
||
| 88 | |||
| 89 | // check if the column exists in the DB table |
||
| 90 | 21 | if ($this->hasColumn($this->model->getTable(), $column_with_details['name'])) { |
|
| 91 | 3 | $column_with_details['tableColumn'] = true; |
|
| 92 | } else { |
||
| 93 | 18 | $column_with_details['tableColumn'] = false; |
|
| 94 | 18 | $column_with_details['orderable'] = false; |
|
| 95 | 18 | $column_with_details['searchLogic'] = false; |
|
| 96 | } |
||
| 97 | |||
| 98 | 21 | array_filter($this->columns[$column_with_details['key']] = $column_with_details); |
|
| 99 | |||
| 100 | // if this is a relation type field and no corresponding model was specified, get it from the relation method |
||
| 101 | // defined in the main model |
||
| 102 | 21 | if (isset($column_with_details['entity']) && ! isset($column_with_details['model'])) { |
|
| 103 | 1 | $column_with_details['model'] = $this->getRelationModel($column_with_details['entity']); |
|
| 104 | } |
||
| 105 | |||
| 106 | 21 | return $this; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add multiple columns at the end of the CRUD object's "columns" array. |
||
| 111 | * |
||
| 112 | * @param [array of columns] |
||
| 113 | */ |
||
| 114 | 16 | public function addColumns($columns) |
|
| 115 | { |
||
| 116 | 16 | if (count($columns)) { |
|
| 117 | 16 | foreach ($columns as $key => $column) { |
|
| 118 | 15 | $this->addColumn($column); |
|
| 119 | } |
||
| 120 | } |
||
| 121 | 15 | } |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Move the most recently added column after the given target column. |
||
| 125 | * |
||
| 126 | * @param string|array $targetColumn The target column name or array. |
||
| 127 | */ |
||
| 128 | 2 | public function afterColumn($targetColumn) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Move the most recently added column before the given target column. |
||
| 135 | * |
||
| 136 | * @param string|array $targetColumn The target column name or array. |
||
| 137 | */ |
||
| 138 | 2 | 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 | 4 | private function moveColumn($targetColumn, $before = true) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * 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 | * |
||
| 187 | * @param [field or column] |
||
| 188 | */ |
||
| 189 | 21 | 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 | 4 | 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 | 2 | 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 | * @param string $column |
||
| 269 | * @param string $label |
||
| 270 | */ |
||
| 271 | public function setColumnLabel($column, $label) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the relationships used in the CRUD columns. |
||
| 278 | * @return [array] Relationship names |
||
| 279 | */ |
||
| 280 | 3 | 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 | 5 | 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 | 21 | protected function hasColumn($table, $name) |
|
| 362 | |||
| 363 | } |
||
| 364 |
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: