We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 21 |
Paths | 528 |
Total Lines | 56 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public function compactFakeFields($requestInput, $model = false, $fields = []) |
||
23 | { |
||
24 | // get the right fields according to the form type (create/update) |
||
25 | if (empty($fields)) { |
||
26 | $fields = $this->fields(); |
||
|
|||
27 | } |
||
28 | |||
29 | $model = $model ? (is_string($model) ? app($model) : $model) : $this->model; |
||
30 | |||
31 | $compactedFakeFields = []; |
||
32 | |||
33 | foreach ($fields as $field) { |
||
34 | // compact fake fields |
||
35 | // cast the field name to array first, to account for array field names |
||
36 | // in fields that send multiple inputs and want them all saved to the database |
||
37 | foreach ((array) $field['name'] as $fieldName) { |
||
38 | $fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras'; |
||
39 | $isFakeField = $field['fake'] ?? false; |
||
40 | |||
41 | // field is represented by the subfields |
||
42 | if (isset($field['subfields']) && $field['model'] === get_class($model)) { |
||
43 | foreach ($field['subfields'] as $subfield) { |
||
44 | $subfieldName = Str::afterLast($subfield['name'], '.'); |
||
45 | $isSubfieldFake = $subfield['fake'] ?? false; |
||
46 | $subFakeFieldKey = isset($subfield['store_in']) ? $subfield['store_in'] : 'extras'; |
||
47 | |||
48 | if (array_key_exists($subfieldName, $requestInput) && $isSubfieldFake) { |
||
49 | $this->addCompactedField($requestInput, $subfieldName, $subFakeFieldKey); |
||
50 | |||
51 | if (! in_array($subFakeFieldKey, $compactedFakeFields)) { |
||
52 | $compactedFakeFields[] = $subFakeFieldKey; |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | if (array_key_exists($fieldName, $requestInput) && $isFakeField) { |
||
60 | $this->addCompactedField($requestInput, $fieldName, $fakeFieldKey); |
||
61 | |||
62 | if (! in_array($fakeFieldKey, $compactedFakeFields)) { |
||
63 | $compactedFakeFields[] = $fakeFieldKey; |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | // json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted |
||
70 | foreach ($compactedFakeFields as $value) { |
||
71 | if (! (property_exists($model, 'translatable') && in_array($value, $model->getTranslatableAttributes(), true)) && $model->shouldEncodeFake($value)) { |
||
72 | $requestInput[$value] = json_encode($requestInput[$value]); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | // if there are no fake fields defined, return the original request input |
||
77 | return $requestInput; |
||
78 | } |
||
95 |