HandleFieldsGroups::handleFieldsGroups()   B
last analyzed

Complexity

Conditions 10
Paths 17

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 22
c 1
b 0
f 0
nc 17
nop 1
dl 0
loc 39
ccs 0
cts 0
cp 0
crap 110
rs 7.6666

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
trait HandleFieldsGroups
9
{
10
    /**
11
     * @param \A17\Twill\Models\Model $object
12
     * @param array|null $fields
13
     * @return \A17\Twill\Models\Model
14
     */
15 3
    public function hydrateHandleFieldsGroups($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

15
    public function hydrateHandleFieldsGroups($object, /** @scrutinizer ignore-unused */ $fields)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17 3
        foreach ($this->fieldsGroups as $group => $groupFields) {
18
            if ($object->$group) {
19
                $casts = $this->getModelCasts($object);
20
                if (!array_key_exists($group, $casts) || (array_key_exists($group, $casts) && $casts[$group] !== 'array')) {
21
                    $object->$group = json_encode($object->$group);
22
                }
23 3
            }
24
        }
25
26
        return $object;
27
    }
28
29
    /**
30
     * @param \A17\Twill\Models\Model|null $object
31 31
     * @param array $fields
32
     * @return array
33 31
     */
34
    public function prepareFieldsBeforeSaveHandleFieldsGroups($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function prepareFieldsBeforeSaveHandleFieldsGroups(/** @scrutinizer ignore-unused */ $object, $fields)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        return $this->handleFieldsGroups($fields);
37
    }
38
39
    /**
40
     * @param array $fields
41 26
     * @return array
42
     */
43 26
    public function prepareFieldsBeforeCreateHandleFieldsGroups($fields)
44
    {
45
        return $this->handleFieldsGroups($fields);
46
    }
47
48
    /**
49
     * @param \A17\Twill\Models\Model $object
50
     * @param array $fields
51 6
     * @return array
52
     */
53 6
    public function getFormFieldsHandleFieldsGroups($object, $fields)
54
    {
55
        foreach ($this->fieldsGroups as $group => $groupFields) {
56
            if ($object->$group) {
57
                $casts = $this->getModelCasts($object);
58
                if (array_key_exists($group, $casts) && $casts[$group] === 'array') {
59
                    $decoded_fields = $object->$group;
60
                } else {
61
                    $decoded_fields = json_decode($object->$group, true) ?? [];
62
                }
63
64 6
                // In case that some field read the value through $item->$name
65
                foreach ($decoded_fields as $field_name => $field_value) {
66
                    if ($this->fieldsGroupsFormFieldNamesAutoPrefix) {
67 32
                        $decoded_fields[$group . $this->fieldsGroupsFormFieldNameSeparator . $field_name] = $field_value;
68 32
                        unset($decoded_fields[$field_name]);
69
70
                        if (!is_array($field_value)) {
71
                            $object->setAttribute($group . $this->fieldsGroupsFormFieldNameSeparator . $field_name, $field_value);
72
                        }
73
                    } else {
74
                        $object->setAttribute($field_name, $field_value);
75
                    }
76
                }
77
                $fields = array_merge($fields, $decoded_fields);
78
            }
79
        }
80 32
81
        return $fields;
82
    }
83
84
    protected function handleFieldsGroups($fields)
85
    {
86
        foreach ($this->fieldsGroups as $group => $groupFields) {
87
            if ($this->fieldsGroupsFormFieldNamesAutoPrefix) {
88
                $groupFields = array_map(function ($field_name) use ($group) {
89
                    return $group . $this->fieldsGroupsFormFieldNameSeparator . $field_name;
90
                }, $groupFields);
91
            }
92
93
            $fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
            $fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
                return !empty($value);
95
            });
96
97
            if ($this->fieldsGroupsFormFieldNamesAutoPrefix) {
98
                $fieldsGroupWithGroupSeparator = [];
99
                foreach ($fields[$group] as $key => $value) {
100
                    $fieldsGroupWithGroupSeparator[Str::replaceFirst($group . $this->fieldsGroupsFormFieldNameSeparator, '', $key)] = $value;
101
                }
102
                $fields[$group] = $fieldsGroupWithGroupSeparator;
103
            }
104
105
            if (in_array($group, $this->model->getTranslatedAttributes()) && is_array($fields[$group])) {
106
                $fieldForTranslationTrait = [];
107
                foreach ($fields[$group] as $field => $translatedValues) {
108
                    foreach ($translatedValues as $locale => $value) {
109
                        $fieldForTranslationTrait[$locale][$field] = $value;
110
                    }
111
                }
112
                $fields[$group] = $fieldForTranslationTrait;
113
            }
114
115
            if (empty($fields[$group])) {
116
                $fields[$group] = null;
117
            }
118
119
            Arr::forget($fields, $groupFields);
120
        }
121
122
        return $fields;
123
    }
124
125
    /**
126
     * @param \A17\Twill\Models\Model $object
127
     * @return array
128
     */
129
    protected function getModelCasts($object)
130
    {
131
        $casts = $object->getCasts();
132
        if ($this->model->isTranslatable()) {
133
            $casts = $casts + app()->make($this->model->getTranslationModelNameDefault())->getCasts();
134
        }
135
136
        return $casts;
137
    }
138
}
139