We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 66 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 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) |
||
| 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) |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @deprecated Never used. Will be removed in a future version. |
||
| 165 | * |
||
| 166 | * @param array $column Column definition array. |
||
| 167 | * @return array Column definition array with wrapper. |
||
| 168 | * |
||
| 169 | * @codeCoverageIgnore |
||
| 170 | */ |
||
| 171 | protected function makeSureColumnHasWrapper($column) |
||
| 172 | { |
||
| 173 | if (! isset($column['wrapper'])) { |
||
| 174 | $column['wrapper'] = []; |
||
| 175 | } |
||
| 176 | |||
| 177 | return $column; |
||
| 178 | } |
||
| 179 | |||
| 180 | protected function makeSureColumnHasEntity($column) |
||
| 181 | { |
||
| 182 | if (isset($column['entity'])) { |
||
| 183 | return $column; |
||
| 184 | } |
||
| 185 | |||
| 186 | // if the name is an array it's definitely not a relationship |
||
| 187 | if (is_array($column['name'])) { |
||
| 188 | return $column; |
||
| 189 | } |
||
| 190 | |||
| 191 | // if the name is dot notation it might be a relationship |
||
| 192 | if (strpos($column['name'], '.') !== false) { |
||
| 193 | $possibleMethodName = Str::before($column['name'], '.'); |
||
| 194 | |||
| 195 | // if the first part of the string exists as method in the model |
||
| 196 | if (method_exists($this->model, $possibleMethodName)) { |
||
| 197 | // check model method for possibility of being a relationship |
||
| 198 | $column['entity'] = $this->modelMethodIsRelationship($this->model, $possibleMethodName) ? $column['name'] : false; |
||
| 199 | |||
| 200 | if ($column['entity']) { |
||
| 201 | // if the user setup the attribute in relation string, we are not going to infer that attribute from model |
||
| 202 | // instead we get the defined attribute by the user. |
||
| 203 | if ($this->isAttributeInRelationString($column)) { |
||
| 204 | $column['attribute'] = $column['attribute'] ?? Str::afterLast($column['entity'], '.'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return $column; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | // if there's a method on the model with this name |
||
| 213 | if (method_exists($this->model, $column['name']) || $this->model->isRelation($column['name'])) { |
||
| 214 | // check model method for possibility of being a relationship |
||
| 215 | $column['entity'] = $this->modelMethodIsRelationship($this->model, $column['name']); |
||
| 216 | |||
| 217 | return $column; |
||
| 218 | } |
||
| 219 | |||
| 220 | // if the name ends with _id and that method exists, |
||
| 221 | // we can probably use it as an entity |
||
| 222 | if (Str::endsWith($column['name'], '_id')) { |
||
| 223 | $possibleMethodName = Str::replaceLast('_id', '', $column['name']); |
||
| 224 | |||
| 225 | if (method_exists($this->model, $possibleMethodName)) { |
||
| 226 | // check model method for possibility of being a relationship |
||
| 227 | $column['entity'] = $this->modelMethodIsRelationship($this->model, $possibleMethodName); |
||
| 228 | |||
| 229 | return $column; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | return $column; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Infer the attribute for the column when needed. |
||
| 238 | * |
||
| 239 | * @param array $column |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | protected function makeSureColumnHasAttribute(array $column) |
||
| 243 | { |
||
| 244 | return $this->makeSureFieldHasAttribute($column); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * If an entity has been defined for the column, but no model, |
||
| 249 | * determine the model from that relationship. |
||
| 250 | * |
||
| 251 | * @param array $column Column definition array. |
||
| 252 | * @return array Column definition array with model. |
||
| 253 | */ |
||
| 254 | protected function makeSureColumnHasModel($column) |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * If an entity has been defined for the column, but no relation type, |
||
| 267 | * determine the relation type from that relationship. |
||
| 268 | * |
||
| 269 | * @param array $column Column definition array. |
||
| 270 | * @return array Column definition array with model. |
||
| 271 | */ |
||
| 272 | protected function makeSureColumnHasRelationType($column) |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Move the most recently added column before or after the given target column. Default is before. |
||
| 283 | * |
||
| 284 | * @param string|array $targetColumn The target column name or array. |
||
| 285 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
||
| 286 | */ |
||
| 287 | protected function moveColumn($targetColumn, $before = true) |
||
| 288 | { |
||
| 289 | // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved |
||
| 290 | // into a common class |
||
| 291 | $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; |
||
| 292 | $columnsArray = $this->columns(); |
||
| 293 | |||
| 294 | $targetColumnName = str_replace('.', '__', $targetColumnName); |
||
| 295 | |||
| 296 | if (array_key_exists($targetColumnName, $columnsArray)) { |
||
| 297 | $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) : |
||
| 298 | array_search($targetColumnName, array_keys($columnsArray)) + 1; |
||
| 299 | |||
| 300 | $element = array_pop($columnsArray); |
||
| 301 | |||
| 302 | if ($element['priority'] === count($columnsArray)) { |
||
| 303 | // the priority was most likely auto-set as it corresponds to the column array count |
||
| 304 | // update the priority to the target column position |
||
| 305 | $element['priority'] = $targetColumnPosition; |
||
| 306 | } |
||
| 307 | |||
| 308 | $beginningPart = array_slice($columnsArray, 0, $targetColumnPosition, true); |
||
| 309 | $endingArrayPart = array_slice($columnsArray, $targetColumnPosition, null, true); |
||
| 310 | |||
| 311 | $columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); |
||
| 312 | $this->setOperationSetting('columns', $columnsArray); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check if the column exists in the database, as a DB column. |
||
| 318 | * |
||
| 319 | * @param string $table |
||
| 320 | * @param string $name |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | protected function hasDatabaseColumn($table, $name) |
||
| 324 | { |
||
| 325 | static $cache = []; |
||
| 326 | |||
| 327 | if (! $this->driverIsSql()) { |
||
| 328 | return true; |
||
| 329 | } |
||
| 330 | |||
| 331 | if (isset($cache[$table])) { |
||
| 332 | $columns = $cache[$table]; |
||
| 333 | } else { |
||
| 334 | $columns = $cache[$table] = $this->getSchema()->getColumnListing($table); |
||
| 335 | } |
||
| 336 | |||
| 337 | return in_array($name, $columns); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Prepare the column attributes and add it to operation settings. |
||
| 342 | */ |
||
| 343 | private function prepareAttributesAndAddColumn(array|string $column): CrudColumn |
||
| 351 | } |
||
| 352 | } |
||
| 353 |