We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 63 |
Total Lines | 366 |
Duplicated Lines | 0 % |
Changes | 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) |
||
93 | { |
||
94 | foreach ($fields as $key => $field) { |
||
95 | $fields[$key] = $this->overwriteFieldNameFromDotNotationToArray($field); |
||
96 | } |
||
97 | |||
98 | return $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) |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * If entity is not present, but it looks like the field SHOULD be a relationship field, |
||
127 | * try to determine the method on the model that defines the relationship, and pass it to |
||
128 | * the field as 'entity'. |
||
129 | * |
||
130 | * @param array $field |
||
131 | * @return array |
||
132 | */ |
||
133 | protected function makeSureFieldHasEntity($field) |
||
134 | { |
||
135 | $model = isset($field['baseModel']) ? (new $field['baseModel']) : $this->model; |
||
136 | |||
137 | if (isset($field['entity'])) { |
||
138 | return $field; |
||
139 | } |
||
140 | |||
141 | // by default, entity is false if we cannot link it with guessing functions to a relation |
||
142 | $field['entity'] = false; |
||
143 | |||
144 | //if the name is dot notation we are sure it's a relationship |
||
145 | if (strpos($field['name'], '.') !== false) { |
||
146 | $possibleMethodName = Str::of($field['name'])->before('.'); |
||
147 | // check model method for possibility of being a relationship |
||
148 | $field['entity'] = $this->modelMethodIsRelationship($model, $possibleMethodName) ? $field['name'] : false; |
||
149 | |||
150 | return $field; |
||
151 | } |
||
152 | |||
153 | // if there's a method on the model with this name |
||
154 | if (method_exists($model, $field['name'])) { |
||
155 | // check model method for possibility of being a relationship |
||
156 | $field['entity'] = $this->modelMethodIsRelationship($model, $field['name']); |
||
157 | |||
158 | return $field; |
||
159 | } |
||
160 | |||
161 | // if the name ends with _id and that method exists, |
||
162 | // we can probably use it as an entity |
||
163 | if (Str::endsWith($field['name'], '_id')) { |
||
164 | $possibleMethodName = Str::replaceLast('_id', '', $field['name']); |
||
165 | |||
166 | if (method_exists($model, $possibleMethodName)) { |
||
167 | // check model method for possibility of being a relationship |
||
168 | $field['entity'] = $this->modelMethodIsRelationship($model, $possibleMethodName); |
||
169 | |||
170 | return $field; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | return $field; |
||
175 | } |
||
176 | |||
177 | protected function makeSureFieldHasAttribute($field) |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Set the label of a field, if it's missing, by capitalizing the name and replacing |
||
199 | * underscores with spaces. |
||
200 | * |
||
201 | * @param array $field Field definition array. |
||
202 | * @return array Field definition array that contains label too. |
||
203 | */ |
||
204 | protected function makeSureFieldHasLabel($field) |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Set the type of a field, if it's missing, by inferring it from the |
||
217 | * db column type. |
||
218 | * |
||
219 | * @param array $field Field definition array. |
||
220 | * @return array Field definition array that contains type too. |
||
221 | */ |
||
222 | protected function makeSureFieldHasType($field) |
||
229 | } |
||
230 | |||
231 | protected function inferFieldTypeFromRelationType($relationType) |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * If a field has subfields, go through each subfield and guess |
||
250 | * its attribute, filling in whatever is missing. |
||
251 | * |
||
252 | * @param array $field Field definition array. |
||
253 | * @return array The improved definition of that field (a better 'subfields' array) |
||
254 | */ |
||
255 | protected function makeSureSubfieldsHaveNecessaryAttributes($field) |
||
256 | { |
||
257 | if (! isset($field['subfields'])) { |
||
258 | return $field; |
||
259 | } |
||
260 | |||
261 | foreach ($field['subfields'] as $key => $subfield) { |
||
262 | if (empty($subfield) || ! isset($subfield['name'])) { |
||
263 | abort(500, 'Subfield name can\'t be empty'); |
||
264 | } |
||
265 | |||
266 | // make sure the field definition is an array |
||
267 | if (is_string($subfield)) { |
||
268 | $subfield = ['name' => $subfield]; |
||
269 | } |
||
270 | |||
271 | $subfield['parentFieldName'] = $field['name']; |
||
272 | |||
273 | if (! isset($field['model'])) { |
||
274 | // we're inside a simple 'repeatable' with no model/relationship, so |
||
275 | // we assume all subfields are supposed to be text fields |
||
276 | $subfield['type'] = $subfield['type'] ?? 'text'; |
||
277 | $subfield['entity'] = $subfield['entity'] ?? false; |
||
278 | } else { |
||
279 | // we should use 'model' as the `baseModel` for all subfields, so that when |
||
280 | // we look if `category()` relationship exists on the model, we look on |
||
281 | // the model this repeatable represents, not the main CRUD model |
||
282 | $currentEntity = $subfield['baseEntity'] ?? $field['entity']; |
||
283 | $subfield['baseModel'] = $subfield['baseModel'] ?? $field['model']; |
||
284 | $subfield['baseEntity'] = isset($field['baseEntity']) ? $field['baseEntity'].'.'.$currentEntity : $currentEntity; |
||
285 | $subfield['baseFieldName'] = is_array($subfield['name']) ? implode(',', $subfield['name']) : $subfield['name']; |
||
286 | $subfield['baseFieldName'] = Str::afterLast($subfield['baseFieldName'], '.'); |
||
287 | } |
||
288 | |||
289 | $field['subfields'][$key] = $this->makeSureFieldHasNecessaryAttributes($subfield); |
||
290 | } |
||
291 | |||
292 | // when field has any of `many` relations we need to append either the pivot selector for the `ToMany` or the |
||
293 | // local key for the `many` relations. Other relations don't need any special treatment when used as subfields. |
||
294 | if (isset($field['relation_type'])) { |
||
295 | switch ($field['relation_type']) { |
||
296 | case 'MorphToMany': |
||
297 | case 'BelongsToMany': |
||
298 | $pivotSelectorField = static::getPivotFieldStructure($field); |
||
299 | |||
300 | $pivot = Arr::where($field['subfields'], function ($item) use ($pivotSelectorField) { |
||
301 | return $item['name'] === $pivotSelectorField['name']; |
||
302 | }); |
||
303 | |||
304 | if (! empty($pivot)) { |
||
305 | break; |
||
306 | } |
||
307 | |||
308 | $this->setupFieldValidation($pivotSelectorField, $field['name']); |
||
309 | $field['subfields'] = Arr::prepend($field['subfields'], $pivotSelectorField); |
||
310 | |||
311 | break; |
||
312 | case 'MorphMany': |
||
313 | case 'HasMany': |
||
314 | $entity = isset($field['baseEntity']) ? $field['baseEntity'].'.'.$field['entity'] : $field['entity']; |
||
315 | $relationInstance = $this->getRelationInstance(['entity' => $entity]); |
||
316 | |||
317 | $localKeyField = Arr::where($field['subfields'], function ($item) use ($relationInstance) { |
||
318 | return $item['name'] === $relationInstance->getRelated()->getKeyName(); |
||
319 | }); |
||
320 | |||
321 | if (! empty($localKeyField)) { |
||
322 | break; |
||
323 | } |
||
324 | |||
325 | $field['subfields'] = Arr::prepend($field['subfields'], [ |
||
326 | 'name' => $relationInstance->getRelated()->getKeyName(), |
||
327 | 'type' => 'hidden', |
||
328 | ]); |
||
329 | break; |
||
330 | } |
||
331 | } |
||
332 | |||
333 | return $field; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Enable the tabs functionality, if a field has a tab defined. |
||
338 | * |
||
339 | * @param array $field Field definition array. |
||
340 | * @return void |
||
341 | */ |
||
342 | protected function enableTabsIfFieldUsesThem($field) |
||
343 | { |
||
344 | // if a tab was mentioned, we should enable it |
||
345 | if (isset($field['tab'])) { |
||
346 | if (! $this->tabsEnabled()) { |
||
347 | $this->enableTabs(); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Add a field to the current operation, using the Settings API. |
||
354 | * |
||
355 | * @param array $field Field definition array. |
||
356 | */ |
||
357 | protected function addFieldToOperationSettings($field) |
||
358 | { |
||
359 | $allFields = $this->getOperationSetting('fields'); |
||
360 | $allFields = array_merge($this->getCleanStateFields(), [$field['name'] => $field]); |
||
361 | |||
362 | $this->setOperationSetting('fields', $allFields); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Get the string that should be used as an array key, for the attributive array |
||
367 | * where the fields are stored for the current operation. |
||
368 | * |
||
369 | * @deprecated v6 |
||
370 | */ |
||
371 | protected function getFieldKey(array $field): string |
||
374 | } |
||
375 | } |
||
376 |