We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 64 |
Total Lines | 332 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ColumnsProtectedMethods 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 ColumnsProtectedMethods, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait ColumnsProtectedMethods |
||
10 | { |
||
11 | /** |
||
12 | * Add a column to the current operation, using the Setting API. |
||
13 | * |
||
14 | * @param array $column Column definition array. |
||
15 | */ |
||
16 | protected function addColumnToOperationSettings($column) |
||
17 | { |
||
18 | $allColumns = $this->columns(); |
||
|
|||
19 | $allColumns = Arr::add($allColumns, $column['key'], $column); |
||
20 | |||
21 | $this->setOperationSetting('columns', $allColumns); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * If a column priority has not been defined, provide a default one. |
||
26 | * |
||
27 | * @param array $column Column definition array. |
||
28 | * @return array Proper array defining the column. |
||
29 | */ |
||
30 | protected function makeSureColumnHasPriority($column) |
||
31 | { |
||
32 | $columns_count = $this->countColumnsWithoutActions(); |
||
33 | $assumed_priority = $columns_count ? $columns_count : 0; |
||
34 | |||
35 | $column['priority'] = $column['priority'] ?? $assumed_priority; |
||
36 | |||
37 | return $column; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * If the field definition array is actually a string, it means the programmer was lazy |
||
42 | * and has only passed the name of the column. Turn that into a proper array. |
||
43 | * |
||
44 | * @param array $column Column definition array. |
||
45 | * @return array Proper array defining the column. |
||
46 | */ |
||
47 | protected function makeSureColumnHasName($column) |
||
48 | { |
||
49 | if (is_string($column)) { |
||
50 | $column = ['name' => $column]; |
||
51 | } |
||
52 | |||
53 | if (is_array($column) && ! isset($column['name'])) { |
||
54 | $column['name'] = 'anonymous_column_'.Str::random(5); |
||
55 | } |
||
56 | |||
57 | return $column; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * If a column array is missing the "label" attribute, an ugly error would be show. |
||
62 | * So we add the field Name as a label - it's better than nothing. |
||
63 | * |
||
64 | * @param array $column Column definition array. |
||
65 | * @return array Proper array defining the column. |
||
66 | */ |
||
67 | protected function makeSureColumnHasLabel($column) |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * If a column definition is missing the type, set a default. |
||
78 | * |
||
79 | * @param array $column Column definition array. |
||
80 | * @return array Column definition array with type. |
||
81 | */ |
||
82 | protected function makeSureColumnHasType($column) |
||
83 | { |
||
84 | // Do not alter type if it has been set by developer |
||
85 | if (isset($column['type'])) { |
||
86 | return $column; |
||
87 | } |
||
88 | |||
89 | // Set text as default column type |
||
90 | $column['type'] = 'text'; |
||
91 | |||
92 | if (method_exists($this->model, 'translationEnabledForModel') && $this->model->translationEnabledForModel() && array_key_exists($column['name'], $this->model->getTranslations())) { |
||
93 | return $column; |
||
94 | } |
||
95 | |||
96 | $could_be_relation = Arr::get($column, 'entity', false) !== false; |
||
97 | |||
98 | if ($could_be_relation) { |
||
99 | $column['type'] = $this->inferFieldTypeFromRelationType($column['relation_type']); |
||
100 | } |
||
101 | |||
102 | if (in_array($column['name'], $this->model->getDates())) { |
||
103 | $column['type'] = 'datetime'; |
||
104 | } |
||
105 | |||
106 | if ($this->model->hasCast($column['name'])) { |
||
107 | $attributeType = $this->model->getCasts()[$column['name']]; |
||
108 | |||
109 | switch ($attributeType) { |
||
110 | case 'array': |
||
111 | case 'encrypted:array': |
||
112 | case 'collection': |
||
113 | case 'encrypted:collection': |
||
114 | $column['type'] = 'array'; |
||
115 | break; |
||
116 | case 'json': |
||
117 | case 'object': |
||
118 | $column['type'] = 'json'; |
||
119 | break; |
||
120 | case 'bool': |
||
121 | case 'boolean': |
||
122 | $column['type'] = 'check'; |
||
123 | break; |
||
124 | case 'date': |
||
125 | $column['type'] = 'date'; |
||
126 | break; |
||
127 | case 'datetime': |
||
128 | $column['type'] = 'datetime'; |
||
129 | break; |
||
130 | case 'double': |
||
131 | case 'float': |
||
132 | case 'int': |
||
133 | case 'integer': |
||
134 | case 'real': |
||
135 | case 'timestamp': |
||
136 | $column['type'] = 'number'; |
||
137 | break; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return $column; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * If a column definition is missing the key, set the default. |
||
146 | * The key is used when storing all columns using the Settings API, |
||
147 | * it is used as the "key" of the associative array that holds all columns. |
||
148 | * |
||
149 | * @param array $column Column definition array. |
||
150 | * @return array Column definition array with key. |
||
151 | */ |
||
152 | protected function makeSureColumnHasKey($column) |
||
153 | { |
||
154 | if (! isset($column['key'])) { |
||
155 | $column['key'] = str_replace('.', '__', $column['name']); |
||
156 | } |
||
157 | |||
158 | return $column; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * If a column definition is missing the wrapper element, set the default (empty). |
||
163 | * The wrapper is the HTML element that wrappes around the column text. |
||
164 | * By defining this array a developer can wrap the text into an anchor (link), |
||
165 | * span, div or whatever they want. |
||
166 | * |
||
167 | * @param array $column Column definition array. |
||
168 | * @return array Column definition array with wrapper. |
||
169 | */ |
||
170 | protected function makeSureColumnHasWrapper($column) |
||
171 | { |
||
172 | if (! isset($column['wrapper'])) { |
||
173 | $column['wrapper'] = []; |
||
174 | } |
||
175 | |||
176 | return $column; |
||
177 | } |
||
178 | |||
179 | protected function makeSureColumnHasEntity($column) |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Infer the attribute for the column when needed. |
||
237 | * |
||
238 | * @param array $column |
||
239 | * @return void |
||
240 | */ |
||
241 | protected function makeSureColumnHasAttribute(array $column) |
||
242 | { |
||
243 | return $this->makeSureFieldHasAttribute($column); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * If an entity has been defined for the column, but no model, |
||
248 | * determine the model from that relationship. |
||
249 | * |
||
250 | * @param array $column Column definition array. |
||
251 | * @return array Column definition array with model. |
||
252 | */ |
||
253 | protected function makeSureColumnHasModel($column) |
||
254 | { |
||
255 | // if this is a relation type field and no corresponding model was specified, |
||
256 | // get it from the relation method defined in the main model |
||
257 | if (isset($column['entity']) && $column['entity'] !== false && ! isset($column['model'])) { |
||
258 | $column['model'] = $this->getRelationModel($column['entity']); |
||
259 | } |
||
260 | |||
261 | return $column; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * If an entity has been defined for the column, but no relation type, |
||
266 | * determine the relation type from that relationship. |
||
267 | * |
||
268 | * @param array $column Column definition array. |
||
269 | * @return array Column definition array with model. |
||
270 | */ |
||
271 | protected function makeSureColumnHasRelationType($column) |
||
272 | { |
||
273 | if (isset($column['entity']) && $column['entity'] !== false) { |
||
274 | $column['relation_type'] = $column['relation_type'] ?? $this->inferRelationTypeFromRelationship($column); |
||
275 | } |
||
276 | |||
277 | return $column; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Move the most recently added column before or after the given target column. Default is before. |
||
282 | * |
||
283 | * @param string|array $targetColumn The target column name or array. |
||
284 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
||
285 | */ |
||
286 | protected function moveColumn($targetColumn, $before = true) |
||
287 | { |
||
288 | // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved |
||
289 | // into a common class |
||
290 | $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; |
||
291 | $columnsArray = $this->columns(); |
||
292 | |||
293 | if (array_key_exists($targetColumnName, $columnsArray)) { |
||
294 | $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) : |
||
295 | array_search($targetColumnName, array_keys($columnsArray)) + 1; |
||
296 | |||
297 | $element = array_pop($columnsArray); |
||
298 | $beginningPart = array_slice($columnsArray, 0, $targetColumnPosition, true); |
||
299 | $endingArrayPart = array_slice($columnsArray, $targetColumnPosition, null, true); |
||
300 | |||
301 | $columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); |
||
302 | $this->setOperationSetting('columns', $columnsArray); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Check if the column exists in the database, as a DB column. |
||
308 | * |
||
309 | * @param string $table |
||
310 | * @param string $name |
||
311 | * @return bool |
||
312 | */ |
||
313 | protected function hasDatabaseColumn($table, $name) |
||
314 | { |
||
315 | static $cache = []; |
||
316 | |||
317 | if (! $this->driverIsSql()) { |
||
318 | return true; |
||
319 | } |
||
320 | |||
321 | if (isset($cache[$table])) { |
||
322 | $columns = $cache[$table]; |
||
323 | } else { |
||
324 | $columns = $cache[$table] = $this->getSchema()->getColumnListing($table); |
||
325 | } |
||
326 | |||
327 | return in_array($name, $columns); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Prepare the column attributes and add it to operation settings. |
||
332 | */ |
||
333 | private function prepareAttributesAndAddColumn(array|string $column): CrudColumn |
||
341 | } |
||
342 | } |
||
343 |