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