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.
Completed
Push — master ( 978098...4d47d7 )
by Aden
02:52
created

AttributeCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\ModelAdmin;
4
5
use Illuminate\Support\Collection;
6
use LaravelFlare\Fields\FieldManager;
7
use LaravelFlare\Fields\Types\BaseField;
8
9
class AttributeCollection extends Collection
10
{
11
    /**
12
     * The items contained in the collection.
13
     *
14
     * @var array
15
     */
16
    protected $items = [];
17
18
    /**
19
     * Field Manager Instance
20
     * 
21
     * @var \LaravelFlare\Fields\FieldManager
22
     */
23
    protected $fields;
24
25
    /**
26
     * ModelAdmin which contains this Attribute Collection.
27
     * 
28
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
29
     */
30
    protected $modelManager;
31
32
    /**
33
     * Create a new collection.
34
     *
35
     * @param  mixed  $items
36
     * 
37
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
     */
39
    public function __construct($items = [], $modelManager = null)
40
    {
41
        $this->fields = \App::make(FieldManager::class);
42
        $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...
43
        $this->modelManager = $modelManager;
44
45
        $this->formatFields();
46
    }
47
48
    /**
49
     * Attempt to reformat the current attribute items array
50
     * into the most usable format (an Attribute Collection).
51
     * 
52
     * @return void
53
     */
54
    public function formatFields()
55
    {
56
        $items = $this->items;
57
        $formattedItems = [];
58
59
        foreach ($items as $name => $inner) {
60
            $formattedItems[$name] = $this->formatInnerField($name, $inner);
61
        }
62
63
        $this->items = $formattedItems;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Allows adding fields to the Attribute Collection 
70
     * 
71
     * @param array $items
72
     */
73
    public function add($items = [])
74
    {
75
        if (!is_array($items) || func_num_args() > 1) {
76
            $items = func_get_args();
77
        }
78
79
        $this->push($this->formatInnerField(null, $items));
80
    }
81
82
    /**
83
     * Format an Inner Field which can either be in the format
84
     * of an Array, an instance of BaseAttribute or even an
85
     * AttributeCollection object (which contains more!).
86
     *
87
     * @param  mixed $name
88
     * @param  mixed  $inner 
89
     * 
90
     * @return mixed
91
     */
92
    protected function formatInnerField($name = null, $inner = [])
93
    {
94
        if ($inner instanceof BaseField) {
95
            return $inner;
96
        }
97
98
        if ($inner instanceof AttributeCollection) {
99
            $formattedItems = [];
100
101
            foreach ($inner->toArray() as $name => $inner) {
102
                $formattedItems[$name] = $this->formatInnerField($inner);
103
            }
104
105
            return $formattedItems;
106
        }
107
108
        if (is_scalar($inner) && $this->attributeManager->attributeTypeExists($inner)) {
109
            return $this->attributeManager->createAttribute($inner, $name, [], $this->modelManager);
0 ignored issues
show
Bug introduced by
The property attributeManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
110
        }
111
112
        if (is_array($inner) && array_key_exists('type', $inner) && is_scalar($inner['type']) && $this->attributeManager->attributeTypeExists($inner['type'])) {
113
            $type = $inner['type'];
114
            array_forget($inner, 'type');
115
            return $this->attributeManager->createAttribute($type, $name, $inner, $this->modelManager);
116
        }
117
118
        if (is_array($inner)) {
119
            $formattedItems = [];
120
121
            foreach ($inner as $name => $inner) {
122
                $formattedItems[$name] = $this->formatInnerField($inner);
123
            }
124
125
            return $formattedItems;
126
        }
127
    }
128
}
129