We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 48 |
Total Lines | 415 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
8 | trait Columns |
||
9 | { |
||
10 | use ColumnsProtectedMethods; |
||
11 | |||
12 | // ------------ |
||
13 | // COLUMNS |
||
14 | // ------------ |
||
15 | |||
16 | /** |
||
17 | * Get the CRUD columns for the current operation. |
||
18 | * |
||
19 | * @return array CRUD columns. |
||
20 | */ |
||
21 | public function columns() |
||
22 | { |
||
23 | return $this->getOperationSetting('columns') ?? []; |
||
|
|||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Add a bunch of column names and their details to the CRUD object. |
||
28 | * |
||
29 | * @param array|string $columns |
||
30 | */ |
||
31 | public function setColumns($columns) |
||
32 | { |
||
33 | // clear any columns already set |
||
34 | $this->removeAllColumns(); |
||
35 | |||
36 | // if array, add a column for each of the items |
||
37 | if (is_array($columns) && count($columns)) { |
||
38 | foreach ($columns as $key => $column) { |
||
39 | // if label and other details have been defined in the array |
||
40 | if (is_array($column)) { |
||
41 | $this->addColumn($column); |
||
42 | } else { |
||
43 | $this->addColumn([ |
||
44 | 'name' => $column, |
||
45 | 'label' => mb_ucfirst($column), |
||
46 | 'type' => 'text', |
||
47 | ]); |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | if (is_string($columns)) { |
||
53 | $this->addColumn([ |
||
54 | 'name' => $columns, |
||
55 | 'label' => mb_ucfirst($columns), |
||
56 | 'type' => 'text', |
||
57 | ]); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Add a column at the end of to the CRUD object's "columns" array. |
||
63 | * |
||
64 | * @param array|string $column |
||
65 | * |
||
66 | * @return self |
||
67 | */ |
||
68 | public function addColumn($column) |
||
69 | { |
||
70 | $column = $this->makeSureColumnHasNeededAttributes($column); |
||
71 | $this->addColumnToOperationSettings($column); |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Add multiple columns at the end of the CRUD object's "columns" array. |
||
78 | * |
||
79 | * @param array $columns |
||
80 | */ |
||
81 | public function addColumns($columns) |
||
82 | { |
||
83 | if (count($columns)) { |
||
84 | foreach ($columns as $key => $column) { |
||
85 | $this->addColumn($column); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Move the most recently added column after the given target column. |
||
92 | * |
||
93 | * @param string|array $targetColumn The target column name or array. |
||
94 | */ |
||
95 | public function afterColumn($targetColumn) |
||
96 | { |
||
97 | $this->moveColumn($targetColumn, false); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Move the most recently added column before the given target column. |
||
102 | * |
||
103 | * @param string|array $targetColumn The target column name or array. |
||
104 | */ |
||
105 | public function beforeColumn($targetColumn) |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Move this column to be first in the columns list. |
||
112 | * |
||
113 | * @return bool|null |
||
114 | */ |
||
115 | public function makeFirstColumn() |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Add the default column type to the given Column, inferring the type from the database column type. |
||
127 | * |
||
128 | * @param array $column |
||
129 | * |
||
130 | * @return array|bool |
||
131 | */ |
||
132 | public function addDefaultTypeToColumn($column) |
||
133 | { |
||
134 | if (array_key_exists('name', (array) $column)) { |
||
135 | $default_type = $this->inferFieldTypeFromDbColumnType($column['name']); |
||
136 | |||
137 | return array_merge(['type' => $default_type], $column); |
||
138 | } |
||
139 | |||
140 | return false; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Remove a column from the CRUD panel by name. |
||
145 | * |
||
146 | * @param string $columnKey The column key. |
||
147 | */ |
||
148 | public function removeColumn($columnKey) |
||
149 | { |
||
150 | $columnsArray = $this->columns(); |
||
151 | Arr::forget($columnsArray, $columnKey); |
||
152 | $this->setOperationSetting('columns', $columnsArray); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Remove multiple columns from the CRUD panel by name. |
||
157 | * |
||
158 | * @param array $columns Array of column names. |
||
159 | */ |
||
160 | public function removeColumns($columns) |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Remove all columns from the CRUD panel. |
||
171 | */ |
||
172 | public function removeAllColumns() |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Remove an attribute from one column's definition array. |
||
179 | * @param string $column The name of the column. |
||
180 | * @param string $attribute The name of the attribute being removed. |
||
181 | */ |
||
182 | public function removeColumnAttribute($column, $attribute) |
||
183 | { |
||
184 | $columns = $this->columns(); |
||
185 | |||
186 | unset($columns[$column][$attribute]); |
||
187 | |||
188 | $this->setOperationSetting('columns', $columns); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Change attributes for multiple columns. |
||
193 | * |
||
194 | * @param array $columns |
||
195 | * @param array $attributes |
||
196 | */ |
||
197 | public function setColumnsDetails($columns, $attributes) |
||
198 | { |
||
199 | foreach ($columns as $columnKey) { |
||
200 | $this->setColumnDetails($columnKey, $attributes); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Change attributes for a certain column. |
||
206 | * |
||
207 | * @param string $columnKey Column key. |
||
208 | * @param array $attributesAndValues |
||
209 | */ |
||
210 | public function setColumnDetails($columnKey, $attributesAndValues) |
||
211 | { |
||
212 | $columnsArray = $this->columns(); |
||
213 | |||
214 | if (isset($columnsArray[$columnKey])) { |
||
215 | foreach ($attributesAndValues as $attributeName => $attributeValue) { |
||
216 | $columnsArray[$columnKey][$attributeName] = $attributeValue; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | $this->setOperationSetting('columns', $columnsArray); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Alias for setColumnDetails(). |
||
225 | * Provides a consistent syntax with Fields, Buttons, Filters modify functionality. |
||
226 | * |
||
227 | * @param string $column Column name. |
||
228 | * @param array $attributes |
||
229 | */ |
||
230 | public function modifyColumn($column, $attributes) |
||
231 | { |
||
232 | $this->setColumnDetails($column, $attributes); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Set label for a specific column. |
||
237 | * |
||
238 | * @param string $column |
||
239 | * @param string $label |
||
240 | */ |
||
241 | public function setColumnLabel($column, $label) |
||
242 | { |
||
243 | $this->setColumnDetails($column, ['label' => $label]); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Get the relationships used in the CRUD columns. |
||
248 | * |
||
249 | * @return array Relationship names |
||
250 | */ |
||
251 | public function getColumnsRelationships() |
||
252 | { |
||
253 | $columns = $this->columns(); |
||
254 | |||
255 | return collect($columns)->pluck('entity')->reject(function ($value, $key) { |
||
256 | return $value == null; |
||
257 | })->toArray(); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the |
||
262 | * new columns array in the original order. |
||
263 | * |
||
264 | * @param array $order An array of column names in the desired order. |
||
265 | */ |
||
266 | public function orderColumns($order) |
||
267 | { |
||
268 | $orderedColumns = []; |
||
269 | foreach ($order as $columnName) { |
||
270 | if (array_key_exists($columnName, $this->columns())) { |
||
271 | $orderedColumns[$columnName] = $this->columns()[$columnName]; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | if (empty($orderedColumns)) { |
||
276 | return; |
||
277 | } |
||
278 | |||
279 | $remaining = array_diff_key($this->columns(), $orderedColumns); |
||
280 | $this->setOperationSetting('columns', array_merge($orderedColumns, $remaining)); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Get a column by the id, from the associative array. |
||
285 | * |
||
286 | * @param int $column_number Placement inside the columns array. |
||
287 | * |
||
288 | * @return array Column details. |
||
289 | */ |
||
290 | public function findColumnById($column_number) |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get the visibility priority for the actions column |
||
299 | * in the CRUD table view. |
||
300 | * |
||
301 | * @return int The priority, from 1 to infinity. Lower is better. |
||
302 | */ |
||
303 | public function getActionsColumnPriority() |
||
304 | { |
||
305 | return (int) $this->getOperationSetting('actionsColumnPriority') ?? 1; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Set a certain priority for the actions column |
||
310 | * in the CRUD table view. Usually set to 10000 in order to hide it. |
||
311 | * |
||
312 | * @param int $number The priority, from 1 to infinity. Lower is better. |
||
313 | * |
||
314 | * @return self |
||
315 | */ |
||
316 | public function setActionsColumnPriority($number) |
||
317 | { |
||
318 | $this->setOperationSetting('actionsColumnPriority', (int) $number); |
||
319 | |||
320 | return $this; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Check if a column exists, by any given attribute. |
||
325 | * |
||
326 | * @param string $attribute Attribute name on that column definition array. |
||
327 | * @param string $value Value of that attribute on that column definition array. |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function hasColumnWhere($attribute, $value) |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Get the first column where a given attribute has the given value. |
||
341 | * |
||
342 | * @param string $attribute Attribute name on that column definition array. |
||
343 | * @param string $value Value of that attribute on that column definition array. |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function firstColumnWhere($attribute, $value) |
||
350 | }); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * The only REALLY MANDATORY attribute for a column is the 'name'. |
||
355 | * Everything else, Backpack can probably guess. |
||
356 | * |
||
357 | * This method checks that all necessary attributes are set. |
||
358 | * If not, it tries to guess them. |
||
359 | * |
||
360 | * @param string|array $column The column definition array OR column name as string. |
||
361 | * @return array Proper column definition array. |
||
362 | */ |
||
363 | public function makeSureColumnHasNeededAttributes($column) |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Count the number of columns added so far. |
||
393 | * |
||
394 | * It will not take into account the action |
||
395 | * columns (columns with buttons, checkbox). |
||
396 | * |
||
397 | * @return int |
||
398 | */ |
||
399 | public function countColumnsWithoutActions() |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Create and return a CrudColumn object for that column name. |
||
408 | * |
||
409 | * Enables developers to use a fluent syntax to declare their columns, |
||
410 | * in addition to the existing options: |
||
411 | * - CRUD::addColumn(['name' => 'price', 'type' => 'number']); |
||
412 | * - CRUD::column('price')->type('number'); |
||
413 | * |
||
414 | * And if the developer uses the CrudColumn object as Column in his CrudController: |
||
415 | * - Column::name('price')->type('number'); |
||
416 | * |
||
417 | * @param string $name The name of the column in the db, or model attribute. |
||
418 | * @return CrudColumn |
||
419 | */ |
||
420 | public function column($name) |
||
425 |