GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AttributeCollection::formatInnerField()   C
last analyzed

Complexity

Conditions 12
Paths 8

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 6.9666
c 0
b 0
f 0
cc 12
nc 8
nop 2

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 LaravelFlare\Flare\Admin\Models;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use LaravelFlare\Fields\FieldManager;
8
use LaravelFlare\Fields\Types\BaseField;
9
10
class AttributeCollection extends Collection
11
{
12
    /**
13
     * The items contained in the collection.
14
     *
15
     * @var array
16
     */
17
    protected $items = [];
18
19
    /**
20
     * Field Manager Instance.
21
     * 
22
     * @var \LaravelFlare\Fields\FieldManager
23
     */
24
    protected $fields;
25
26
    /**
27
     * ModelAdmin which contains this Attribute Collection.
28
     * 
29
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
30
     */
31
    public $modelManager;
32
33
    /**
34
     * Create a new collection.
35
     *
36
     * @param mixed $items
37
     */
38
    public function __construct($items = [], $modelManager = null)
39
    {
40
        $this->fields = app(FieldManager::class);
41
        $this->items = $items;
0 ignored issues
show
Documentation Bug introduced by
It seems like $items of type * is incompatible with the declared type array of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->modelManager = $modelManager;
43
44
        $this->formatFields();
45
    }
46
47
    /**
48
     * Attempt to reformat the current attribute items array
49
     * into the most usable format (an Attribute Collection).
50
     */
51
    public function formatFields()
52
    {
53
        $items = $this->items;
54
        $formattedItems = [];
55
56
        foreach ($items as $name => $inner) {
57
            $formattedItems[$name] = $this->formatInnerField($name, $inner);
58
        }
59
60
        $this->items = $formattedItems;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Allows adding fields to the Attribute Collection.
67
     * 
68
     * @param array $items
69
     */
70
    public function add($items = [])
71
    {
72
        if (!is_array($items) || func_num_args() > 1) {
73
            $items = func_get_args();
74
        }
75
76
        $this->push($this->formatInnerField(null, $items));
77
    }
78
79
    /**
80
     * Format an Inner Field which can either be in the format
81
     * of an Array, an instance of BaseAttribute or even an
82
     * AttributeCollection object (which contains more!).
83
     *
84
     * @param mixed $name
85
     * @param mixed $inner
86
     * 
87
     * @return mixed
88
     */
89
    protected function formatInnerField($name = null, $inner = [])
90
    {
91
        if ($inner instanceof BaseField) {
92
            return $inner;
93
        }
94
95
        if ($inner instanceof self) {
96
            $formattedItems = [];
97
98
            foreach ($inner->toArray() as $name => $inner) {
99
                $formattedItems[$name] = $this->formatInnerField($inner);
100
            }
101
102
            return $formattedItems;
103
        }
104
105
        if (is_scalar($inner) && $this->fields->typeExists($inner)) {
106
            return $this->createField($inner, $name, $this->getValue($name), $inner);
107
        }
108
109
        if (is_array($inner) && array_key_exists('type', $inner) && is_scalar($inner['type']) && $this->fields->typeExists($inner['type'])) {
110
            $type = $inner['type'];
111
            array_forget($inner, 'type');
112
113
            return $this->createField($type, $name, $this->getValue($name), $inner);
114
        }
115
116
        if (is_array($inner)) {
117
            $formattedItems = [];
118
119
            foreach ($inner as $name => $inner) {
120
                $formattedItems[$name] = $this->formatInnerField($inner);
121
            }
122
123
            return $formattedItems;
124
        }
125
    }
126
127
    /**
128
     * Create and return a Field Instance.
129
     * 
130
     * @param mixed  $type
131
     * @param string $name
132
     * @param string $value
133
     * @param mixed  $inner
134
     * 
135
     * @return 
136
     */
137
    private function createField($type, $name, $value, $inner)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

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

Loading history...
138
    {
139
        if ($this->hasOptionsMethod($name)) {
140
            $inner = array_merge($inner, ['options' => $this->getOptions($name)]);
141
        }
142
143
        return $this->fields->create($type, $name, $this->getValue($name), $inner);
144
    }
145
146
    /**
147
     * Get any dynamic options for an attribute.
148
     * 
149
     * @param string $name
150
     * 
151
     * @return mixed
152
     */
153
    private function getOptions($name)
154
    {
155
        $method = 'get'.Str::studly($name).'Options';
156
157
        return $this->modelManager->{$method}();
158
    }
159
160
    /**
161
     * Determine if an options method exists for an attribute.
162
     *
163
     * @param string $key
164
     * 
165
     * @return bool
166
     */
167
    public function hasOptionsMethod($key)
168
    {
169
        return method_exists($this->modelManager, 'get'.Str::studly($key).'Options');
170
    }
171
172
    /**
173
     * Get the value of an attribute for the field.
174
     * 
175
     * @param string $name
176
     * 
177
     * @return mixed
178
     */
179
    private function getValue($name)
180
    {
181
        if (!$name) {
182
            return;
183
        }
184
185
        return $this->modelManager->getAttribute($name);
186
    }
187
}
188