We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 41 | 
| Total Lines | 260 | 
| Duplicated Lines | 0 % | 
| Changes | 4 | ||
| Bugs | 1 | Features | 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 | ||
| 8 | trait ColumnsProtectedMethods | ||
| 9 | { | ||
| 10 | /** | ||
| 11 | * Add a column to the current operation, using the Setting API. | ||
| 12 | * | ||
| 13 | * @param array $column Column definition array. | ||
| 14 | */ | ||
| 15 | protected function addColumnToOperationSettings($column) | ||
| 16 |     { | ||
| 17 | $allColumns = $this->columns(); | ||
|  | |||
| 18 | $allColumns = Arr::add($allColumns, $column['key'], $column); | ||
| 19 | |||
| 20 |         $this->setOperationSetting('columns', $allColumns); | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * If a column priority has not been defined, provide a default one. | ||
| 25 | * | ||
| 26 | * @param array $column Column definition array. | ||
| 27 | * @return array Proper array defining the column. | ||
| 28 | */ | ||
| 29 | protected function makeSureColumnHasPriority($column) | ||
| 30 |     { | ||
| 31 | $columns_count = $this->countColumnsWithoutActions(); | ||
| 32 | $assumed_priority = $columns_count ? $columns_count : 0; | ||
| 33 | |||
| 34 | $column['priority'] = $column['priority'] ?? $assumed_priority; | ||
| 35 | |||
| 36 | return $column; | ||
| 37 | } | ||
| 38 | |||
| 39 | /** | ||
| 40 | * If the field definition array is actually a string, it means the programmer was lazy | ||
| 41 | * and has only passed the name of the column. Turn that into a proper array. | ||
| 42 | * | ||
| 43 | * @param array $column Column definition array. | ||
| 44 | * @return array Proper array defining the column. | ||
| 45 | */ | ||
| 46 | protected function makeSureColumnHasName($column) | ||
| 47 |     { | ||
| 48 |         if (is_string($column)) { | ||
| 49 | $column = ['name' => $column]; | ||
| 50 | } | ||
| 51 | |||
| 52 |         if (is_array($column) && ! isset($column['name'])) { | ||
| 53 | $column['name'] = 'anonymous_column_'.Str::random(5); | ||
| 54 | } | ||
| 55 | |||
| 56 | return $column; | ||
| 57 | } | ||
| 58 | |||
| 59 | /** | ||
| 60 | * If a column array is missing the "label" attribute, an ugly error would be show. | ||
| 61 | * So we add the field Name as a label - it's better than nothing. | ||
| 62 | * | ||
| 63 | * @param array $column Column definition array. | ||
| 64 | * @return array Proper array defining the column. | ||
| 65 | */ | ||
| 66 | protected function makeSureColumnHasLabel($column) | ||
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * If a column definition is missing the type, set a default. | ||
| 77 | * | ||
| 78 | * @param array $column Column definition array. | ||
| 79 | * @return array Column definition array with type. | ||
| 80 | */ | ||
| 81 | protected function makeSureColumnHasType($column) | ||
| 82 |     { | ||
| 83 | $could_be_relation = isset($column['entity']) && $column['entity'] !== false; | ||
| 84 | |||
| 85 |         if (! isset($column['type']) && $could_be_relation) { | ||
| 86 | $column['type'] = 'relationship'; | ||
| 87 | } | ||
| 88 | |||
| 89 |         if (! isset($column['type'])) { | ||
| 90 | $column['type'] = 'text'; | ||
| 91 | } | ||
| 92 | |||
| 93 | return $column; | ||
| 94 | } | ||
| 95 | |||
| 96 | /** | ||
| 97 | * If a column definition is missing the key, set the default. | ||
| 98 | * The key is used when storing all columns using the Settings API, | ||
| 99 | * it is used as the "key" of the associative array that holds all columns. | ||
| 100 | * | ||
| 101 | * @param array $column Column definition array. | ||
| 102 | * @return array Column definition array with key. | ||
| 103 | */ | ||
| 104 | protected function makeSureColumnHasKey($column) | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * If a column definition is missing the wrapper element, set the default (empty). | ||
| 115 | * The wrapper is the HTML element that wrappes around the column text. | ||
| 116 | * By defining this array a developer can wrap the text into an anchor (link), | ||
| 117 | * span, div or whatever they want. | ||
| 118 | * | ||
| 119 | * @param array $column Column definition array. | ||
| 120 | * @return array Column definition array with wrapper. | ||
| 121 | */ | ||
| 122 | protected function makeSureColumnHasWrapper($column) | ||
| 129 | } | ||
| 130 | |||
| 131 | protected function makeSureColumnHasEntity($column) | ||
| 132 |     { | ||
| 199 | } | ||
| 200 | |||
| 201 | /** | ||
| 202 | * If an entity has been defined for the column, but no model, | ||
| 203 | * determine the model from that relationship. | ||
| 204 | * | ||
| 205 | * @param array $column Column definition array. | ||
| 206 | * @return array Column definition array with model. | ||
| 207 | */ | ||
| 208 | protected function makeSureColumnHasModel($column) | ||
| 209 |     { | ||
| 210 | // if this is a relation type field and no corresponding model was specified, | ||
| 211 | // get it from the relation method defined in the main model | ||
| 212 |         if (isset($column['entity']) && $column['entity'] !== false && ! isset($column['model'])) { | ||
| 213 | $column['model'] = $this->getRelationModel($column['entity']); | ||
| 214 | } | ||
| 215 | |||
| 216 | return $column; | ||
| 217 | } | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Move the most recently added column before or after the given target column. Default is before. | ||
| 221 | * | ||
| 222 | * @param string|array $targetColumn The target column name or array. | ||
| 223 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. | ||
| 224 | */ | ||
| 225 | protected function moveColumn($targetColumn, $before = true) | ||
| 226 |     { | ||
| 227 | // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved | ||
| 228 | // into a common class | ||
| 229 | $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; | ||
| 230 | $columnsArray = $this->columns(); | ||
| 231 | |||
| 232 |         if (array_key_exists($targetColumnName, $columnsArray)) { | ||
| 233 | $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) : | ||
| 234 | array_search($targetColumnName, array_keys($columnsArray)) + 1; | ||
| 235 | |||
| 236 | $element = array_pop($columnsArray); | ||
| 237 | $beginningPart = array_slice($columnsArray, 0, $targetColumnPosition, true); | ||
| 238 | $endingArrayPart = array_slice($columnsArray, $targetColumnPosition, null, true); | ||
| 239 | |||
| 240 | $columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); | ||
| 241 |             $this->setOperationSetting('columns', $columnsArray); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * Check if the column exists in the database, as a DB column. | ||
| 247 | * | ||
| 248 | * @param string $table | ||
| 249 | * @param string $name | ||
| 250 | * | ||
| 251 | * @return bool | ||
| 252 | */ | ||
| 253 | protected function hasDatabaseColumn($table, $name) | ||
| 268 | } | ||
| 269 | } | ||
| 270 |