We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 68 |
Total Lines | 389 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | 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) |
||
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) |
||
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 (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 | * Run the field name overwrite in multiple fields. |
||
88 | * |
||
89 | * @param array $fields |
||
90 | * @return array |
||
91 | */ |
||
92 | public function overwriteFieldNamesFromDotNotationToArray($fields) |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * If the field_definition_array array is a string, it means the programmer was lazy |
||
103 | * and has only passed the name of the field. Turn that into a proper array. |
||
104 | * |
||
105 | * @param string|array $field The field definition array (or string). |
||
106 | * @return array |
||
107 | */ |
||
108 | protected function makeSureFieldHasName($field) |
||
109 | { |
||
110 | if (empty($field)) { |
||
111 | abort(500, 'Field name can\'t be empty', ['developer-error-exception']); |
||
112 | } |
||
113 | |||
114 | if (is_string($field)) { |
||
115 | return ['name' => Str::replace(' ', '', $field)]; |
||
116 | } |
||
117 | |||
118 | if (is_array($field) && ! isset($field['name'])) { |
||
119 | abort(500, 'All fields must have their name defined', ['developer-error-exception']); |
||
120 | } |
||
121 | |||
122 | if (is_array($field['name'])) { |
||
123 | abort(500, 'Field name can\'t be an array. It should be a string. Error in field: '.json_encode($field['name']), ['developer-error-exception']); |
||
124 | } |
||
125 | |||
126 | $field['name'] = Str::replace(' ', '', $field['name']); |
||
127 | |||
128 | return $field; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * If entity is not present, but it looks like the field SHOULD be a relationship field, |
||
133 | * try to determine the method on the model that defines the relationship, and pass it to |
||
134 | * the field as 'entity'. |
||
135 | * |
||
136 | * @param array $field |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function makeSureFieldHasEntity($field) |
||
140 | { |
||
141 | $model = isset($field['baseModel']) ? (new $field['baseModel']) : $this->getModel(); |
||
142 | |||
143 | if (isset($field['entity'])) { |
||
144 | return $field; |
||
145 | } |
||
146 | |||
147 | // by default, entity is false if we cannot link it with guessing functions to a relation |
||
148 | $field['entity'] = false; |
||
149 | |||
150 | //if the name is dot notation we are sure it's a relationship |
||
151 | if (strpos($field['name'], '.') !== false) { |
||
152 | $possibleMethodName = Str::of($field['name'])->before('.')->value(); |
||
153 | // check model method for possibility of being a relationship |
||
154 | $field['entity'] = $this->modelMethodIsRelationship($model, $possibleMethodName) ? $field['name'] : false; |
||
155 | |||
156 | return $field; |
||
157 | } |
||
158 | |||
159 | // if there's a method on the model with this name |
||
160 | if (method_exists($model, $field['name']) || $model->isRelation($field['name'])) { |
||
161 | // check model method for possibility of being a relationship |
||
162 | $field['entity'] = $this->modelMethodIsRelationship($model, $field['name']); |
||
163 | |||
164 | return $field; |
||
165 | } |
||
166 | |||
167 | // if the name ends with _id and that method exists, |
||
168 | // we can probably use it as an entity |
||
169 | if (Str::endsWith($field['name'], '_id')) { |
||
170 | $possibleMethodName = Str::replaceLast('_id', '', $field['name']); |
||
171 | |||
172 | if (method_exists($model, $possibleMethodName)) { |
||
173 | // check model method for possibility of being a relationship |
||
174 | $field['entity'] = $this->modelMethodIsRelationship($model, $possibleMethodName); |
||
175 | |||
176 | return $field; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $field; |
||
181 | } |
||
182 | |||
183 | protected function makeSureFieldHasAttribute($field) |
||
184 | { |
||
185 | if (isset($field['entity']) && $field['entity']) { |
||
186 | // if the user setup the attribute in relation string, we are not going to infer that attribute from model |
||
187 | // instead we get the defined attribute by the user. |
||
188 | if ($this->isAttributeInRelationString($field)) { |
||
189 | $field['attribute'] = $field['attribute'] ?? Str::afterLast($field['entity'], '.'); |
||
190 | |||
191 | return $field; |
||
192 | } |
||
193 | } |
||
194 | // if there's a model defined, but no attribute |
||
195 | // guess an attribute using the identifiableAttribute functionality in CrudTrait |
||
196 | if (isset($field['model']) && ! isset($field['attribute']) && method_exists($field['model'], 'identifiableAttribute')) { |
||
197 | $field['attribute'] = (new $field['model']())->identifiableAttribute(); |
||
198 | } |
||
199 | |||
200 | return $field; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Set the label of a field, if it's missing, by capitalizing the name and replacing |
||
205 | * underscores with spaces. |
||
206 | * |
||
207 | * @param array $field Field definition array. |
||
208 | * @return array Field definition array that contains label too. |
||
209 | */ |
||
210 | protected function makeSureFieldHasLabel($field) |
||
211 | { |
||
212 | if (! isset($field['label'])) { |
||
213 | $name = str_replace(',', ' ', $field['name']); |
||
214 | $name = str_replace('_id', '', $name); |
||
215 | $field['label'] = mb_ucfirst(str_replace('_', ' ', $name)); |
||
216 | } |
||
217 | |||
218 | return $field; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Set the type of a field, if it's missing, by inferring it from the |
||
223 | * db column type. |
||
224 | * |
||
225 | * @param array $field Field definition array. |
||
226 | * @return array Field definition array that contains type too. |
||
227 | */ |
||
228 | protected function makeSureFieldHasType($field) |
||
229 | { |
||
230 | if (! isset($field['type'])) { |
||
231 | $field['type'] = isset($field['relation_type']) ? $this->inferFieldTypeFromRelationType($field['relation_type']) : $this->inferFieldTypeFromDbColumnType($field['name']); |
||
232 | } |
||
233 | |||
234 | return $field; |
||
235 | } |
||
236 | |||
237 | protected function inferFieldTypeFromRelationType($relationType) |
||
238 | { |
||
239 | if (backpack_pro()) { |
||
240 | return 'relationship'; |
||
241 | } |
||
242 | |||
243 | switch ($relationType) { |
||
244 | case 'BelongsTo': |
||
245 | return 'select'; |
||
246 | case 'BelongsToMany': |
||
247 | case 'MorphToMany': |
||
248 | return 'select_multiple'; |
||
249 | default: |
||
250 | return 'text'; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * If a field has subfields, go through each subfield and guess |
||
256 | * its attribute, filling in whatever is missing. |
||
257 | * |
||
258 | * @param array $field Field definition array. |
||
259 | * @return array The improved definition of that field (a better 'subfields' array) |
||
260 | */ |
||
261 | protected function makeSureSubfieldsHaveNecessaryAttributes($field) |
||
262 | { |
||
263 | if (! isset($field['subfields']) || ! is_array($field['subfields'])) { |
||
264 | return $field; |
||
265 | } |
||
266 | |||
267 | if (! is_multidimensional_array($field['subfields'], true)) { |
||
268 | abort(500, 'Subfields of «'.$field['name'].'» are malformed. Make sure you provide an array of subfields.', ['developer-error-exception']); |
||
269 | } |
||
270 | |||
271 | foreach ($field['subfields'] as $key => $subfield) { |
||
272 | if (empty($subfield) || ! isset($subfield['name'])) { |
||
273 | abort(500, 'A subfield of «'.$field['name'].'» is malformed. Subfield attribute name can\'t be empty.', ['developer-error-exception']); |
||
274 | } |
||
275 | |||
276 | // make sure the field definition is an array |
||
277 | if (is_string($subfield)) { |
||
278 | $subfield = ['name' => $subfield]; |
||
279 | } |
||
280 | |||
281 | $subfield['name'] = Str::replace(' ', '', $subfield['name']); |
||
282 | |||
283 | $subfield['parentFieldName'] = $field['name']; |
||
284 | $subfield['baseFieldName'] = is_array($subfield['name']) ? implode(',', $subfield['name']) : $subfield['name']; |
||
285 | $subfield['baseFieldName'] = Str::afterLast($subfield['baseFieldName'], '.'); |
||
286 | |||
287 | if (! isset($field['model'])) { |
||
288 | // we're inside a simple 'repeatable' with no model/relationship, so |
||
289 | // we assume all subfields are supposed to be text fields |
||
290 | $subfield['type'] = $subfield['type'] ?? 'text'; |
||
291 | $subfield['entity'] = $subfield['entity'] ?? false; |
||
292 | } else { |
||
293 | // we should use 'model' as the `baseModel` for all subfields, so that when |
||
294 | // we look if `category()` relationship exists on the model, we look on |
||
295 | // the model this repeatable represents, not the main CRUD model |
||
296 | $currentEntity = $subfield['baseEntity'] ?? $field['entity']; |
||
297 | $subfield['baseModel'] = $subfield['baseModel'] ?? $field['model']; |
||
298 | $subfield['baseEntity'] = isset($field['baseEntity']) ? $field['baseEntity'].'.'.$currentEntity : $currentEntity; |
||
299 | } |
||
300 | |||
301 | $field['subfields'][$key] = $this->makeSureFieldHasNecessaryAttributes($subfield); |
||
302 | } |
||
303 | |||
304 | // when field has any of `many` relations we need to append either the pivot selector for the `ToMany` or the |
||
305 | // local key for the `many` relations. Other relations don't need any special treatment when used as subfields. |
||
306 | if (isset($field['relation_type'])) { |
||
307 | switch ($field['relation_type']) { |
||
308 | case 'MorphToMany': |
||
309 | case 'BelongsToMany': |
||
310 | $pivotSelectorField = static::getPivotFieldStructure($field); |
||
311 | |||
312 | $pivot = Arr::where($field['subfields'], function ($item) use ($pivotSelectorField) { |
||
313 | return $item['name'] === $pivotSelectorField['name']; |
||
314 | }); |
||
315 | |||
316 | if (! empty($pivot)) { |
||
317 | break; |
||
318 | } |
||
319 | |||
320 | if ($field['allow_duplicate_pivots'] ?? false) { |
||
321 | $pivotSelectorField['allow_duplicate_pivots'] = true; |
||
322 | $field['subfields'] = Arr::prepend($field['subfields'], [ |
||
323 | 'name' => $field['pivot_key_name'] ?? 'id', |
||
324 | 'type' => 'hidden', |
||
325 | 'wrapper' => [ |
||
326 | 'class' => 'd-none', |
||
327 | ], |
||
328 | ]); |
||
329 | } |
||
330 | |||
331 | $this->setupFieldValidation($pivotSelectorField, $field['name']); |
||
332 | $field['subfields'] = Arr::prepend($field['subfields'], $pivotSelectorField); |
||
333 | |||
334 | break; |
||
335 | case 'MorphMany': |
||
336 | case 'HasMany': |
||
337 | $entity = isset($field['baseEntity']) ? $field['baseEntity'].'.'.$field['entity'] : $field['entity']; |
||
338 | $relationInstance = $this->getRelationInstance(['entity' => $entity]); |
||
339 | |||
340 | $localKeyField = Arr::where($field['subfields'], function ($item) use ($relationInstance) { |
||
341 | return $item['name'] === $relationInstance->getRelated()->getKeyName(); |
||
342 | }); |
||
343 | |||
344 | if (! empty($localKeyField)) { |
||
345 | break; |
||
346 | } |
||
347 | |||
348 | $field['subfields'] = Arr::prepend($field['subfields'], [ |
||
349 | 'name' => $relationInstance->getRelated()->getKeyName(), |
||
350 | 'type' => 'hidden', |
||
351 | ]); |
||
352 | break; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return $field; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Enable the tabs functionality, if a field has a tab defined. |
||
361 | * |
||
362 | * @param array $field Field definition array. |
||
363 | * @return void |
||
364 | */ |
||
365 | protected function enableTabsIfFieldUsesThem($field) |
||
366 | { |
||
367 | // if a tab was mentioned, we should enable it |
||
368 | if (isset($field['tab'])) { |
||
369 | if (! $this->tabsEnabled()) { |
||
370 | $this->enableTabs(); |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Add a field to the current operation, using the Settings API. |
||
377 | * |
||
378 | * @param array $field Field definition array. |
||
379 | */ |
||
380 | protected function addFieldToOperationSettings($field) |
||
381 | { |
||
382 | $allFields = $this->getOperationSetting('fields'); |
||
383 | $allFields = array_merge($this->getCleanStateFields(), [$field['name'] => $field]); |
||
384 | |||
385 | $this->setOperationSetting('fields', $allFields); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get the string that should be used as an array key, for the attributive array |
||
390 | * where the fields are stored for the current operation. |
||
391 | * |
||
392 | * @deprecated v6 |
||
393 | */ |
||
394 | protected function getFieldKey(array $field): string |
||
397 | } |
||
398 | } |
||
399 |