We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 43 |
| Total Lines | 264 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FieldsProtectedMethods 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 FieldsProtectedMethods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait FieldsProtectedMethods |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * If field has entity we want to get the relation type from it. |
||
| 12 | * |
||
| 13 | * @param array $field |
||
| 14 | * @return array |
||
| 15 | */ |
||
| 16 | public function makeSureFieldHasRelationType($field) |
||
| 17 | { |
||
| 18 | $field['relation_type'] = $field['relation_type'] ?? $this->inferRelationTypeFromRelationship($field); |
||
|
|
|||
| 19 | |||
| 20 | return $field; |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * If field has entity we want to make sure it also has a model for that relation. |
||
| 25 | * |
||
| 26 | * @param array $field |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function makeSureFieldHasModel($field) |
||
| 30 | { |
||
| 31 | $field['model'] = $field['model'] ?? $this->inferFieldModelFromRelationship($field); |
||
| 32 | |||
| 33 | return $field; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Based on relation type we can guess if pivot is set. |
||
| 38 | * |
||
| 39 | * @param array $field |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | public function makeSureFieldHasPivot($field) |
||
| 43 | { |
||
| 44 | $field['pivot'] = $field['pivot'] ?? $this->guessIfFieldHasPivotFromRelationType($field['relation_type']); |
||
| 45 | |||
| 46 | return $field; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Based on relation type we can try to guess if it is a multiple field. |
||
| 51 | * |
||
| 52 | * @param array $field |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | public function makeSureFieldHasMultiple($field) |
||
| 56 | { |
||
| 57 | if (isset($field['relation_type'])) { |
||
| 58 | $field['multiple'] = $field['multiple'] ?? $this->guessIfFieldHasMultipleFromRelationType($field['relation_type']); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $field; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * In case field name is dot notation we want to convert it to a valid HTML array field name for validation purposes. |
||
| 66 | * |
||
| 67 | * @param array $field |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function overwriteFieldNameFromDotNotationToArray($field) |
||
| 71 | { |
||
| 72 | if (! is_array($field['name']) && strpos($field['name'], '.') !== false) { |
||
| 73 | $entity_array = explode('.', $field['name']); |
||
| 74 | $name_string = ''; |
||
| 75 | |||
| 76 | foreach ($entity_array as $key => $array_entity) { |
||
| 77 | $name_string .= ($key == 0) ? $array_entity : '['.$array_entity.']'; |
||
| 78 | } |
||
| 79 | |||
| 80 | $field['name'] = $name_string; |
||
| 81 | } |
||
| 82 | |||
| 83 | return $field; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * If the field_definition_array array is a string, it means the programmer was lazy |
||
| 88 | * and has only passed the name of the field. Turn that into a proper array. |
||
| 89 | * |
||
| 90 | * @param string|array $field The field definition array (or string). |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | protected function makeSureFieldHasName($field) |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * If entity is not present, but it looks like the field SHOULD be a relationship field, |
||
| 108 | * try to determine the method on the model that defines the relationship, and pass it to |
||
| 109 | * the field as 'entity'. |
||
| 110 | * |
||
| 111 | * @param array $field |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | protected function makeSureFieldHasEntity($field) |
||
| 152 | } |
||
| 153 | |||
| 154 | protected function overwriteFieldNameFromEntity($field) |
||
| 155 | { |
||
| 156 | // if the entity doesn't have a dot, it means we don't need to overwrite the name |
||
| 157 | if (! Str::contains($field['entity'], '.')) { |
||
| 158 | return $field; |
||
| 159 | } |
||
| 160 | |||
| 161 | // only 1-1 relationships are supported, if it's anything else, abort |
||
| 162 | if ($field['relation_type'] != 'HasOne' || $field['relation_type'] != 'MorphOne') { |
||
| 163 | return $field; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (count(explode('.', $field['entity'])) == count(explode('.', $this->getOnlyRelationEntity($field)))) { |
||
| 167 | $field['name'] = implode('.', array_slice(explode('.', $field['entity']), 0, -1)); |
||
| 168 | $relation = $this->getRelationInstance($field); |
||
| 169 | if (! empty($field['name'])) { |
||
| 170 | $field['name'] .= '.'; |
||
| 171 | } |
||
| 172 | $field['name'] .= $relation->getForeignKeyName(); |
||
| 173 | } |
||
| 174 | |||
| 175 | return $field; |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function makeSureFieldHasAttribute($field) |
||
| 179 | { |
||
| 180 | // if there's a model defined, but no attribute |
||
| 181 | // guess an attribute using the identifiableAttribute functionality in CrudTrait |
||
| 182 | if (isset($field['model']) && ! isset($field['attribute']) && method_exists($field['model'], 'identifiableAttribute')) { |
||
| 183 | $field['attribute'] = call_user_func([(new $field['model']), 'identifiableAttribute']); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $field; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the label of a field, if it's missing, by capitalizing the name and replacing |
||
| 191 | * underscores with spaces. |
||
| 192 | * |
||
| 193 | * @param array $field Field definition array. |
||
| 194 | * @return array Field definition array that contains label too. |
||
| 195 | */ |
||
| 196 | protected function makeSureFieldHasLabel($field) |
||
| 197 | { |
||
| 198 | if (! isset($field['label'])) { |
||
| 199 | $name = is_array($field['name']) ? $field['name'][0] : $field['name']; |
||
| 200 | $name = str_replace('_id', '', $name); |
||
| 201 | $field['label'] = mb_ucfirst(str_replace('_', ' ', $name)); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $field; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the type of a field, if it's missing, by inferring it from the |
||
| 209 | * db column type. |
||
| 210 | * |
||
| 211 | * @param array $field Field definition array. |
||
| 212 | * @return array Field definition array that contains type too. |
||
| 213 | */ |
||
| 214 | protected function makeSureFieldHasType($field) |
||
| 215 | { |
||
| 216 | if (! isset($field['type'])) { |
||
| 217 | $field['type'] = isset($field['relation_type']) ? $this->inferFieldTypeFromFieldRelation($field) : $this->inferFieldTypeFromDbColumnType($field['name']); |
||
| 218 | } |
||
| 219 | |||
| 220 | return $field; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Enable the tabs functionality, if a field has a tab defined. |
||
| 225 | * |
||
| 226 | * @param array $field Field definition array. |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function enableTabsIfFieldUsesThem($field) |
||
| 230 | { |
||
| 231 | // if a tab was mentioned, we should enable it |
||
| 232 | if (isset($field['tab'])) { |
||
| 233 | if (! $this->tabsEnabled()) { |
||
| 234 | $this->enableTabs(); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add a field to the current operation, using the Settings API. |
||
| 241 | * |
||
| 242 | * @param array $field Field definition array. |
||
| 243 | */ |
||
| 244 | protected function addFieldToOperationSettings($field) |
||
| 245 | { |
||
| 246 | $fieldKey = $this->getFieldKey($field); |
||
| 247 | |||
| 248 | $allFields = $this->getOperationSetting('fields'); |
||
| 249 | $allFields = Arr::add($this->fields(), $fieldKey, $field); |
||
| 250 | |||
| 251 | $this->setOperationSetting('fields', $allFields); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the string that should be used as an array key, for the attributive array |
||
| 256 | * where the fields are stored for the current operation. |
||
| 257 | * |
||
| 258 | * The array key for the field should be: |
||
| 259 | * - name (if the name is a string) |
||
| 260 | * - name1_name2_name3 (if the name is an array) |
||
| 261 | * |
||
| 262 | * @param array $field Field definition array. |
||
| 263 | * @return string The string that should be used as array key. |
||
| 264 | */ |
||
| 265 | protected function getFieldKey($field) |
||
| 272 | } |
||
| 273 | } |
||
| 274 |