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 ( 151d6f...d20332 )
by Aden
03:17
created

AttributeCollection::add()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

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 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Attributes;
4
5
use Illuminate\Support\Collection;
6
use LaravelFlare\Flare\Admin\Attributes\BaseAttribute;
7
8
class AttributeCollection extends Collection
9
{
10
    /**
11
     * Attribute Manager Instance
12
     * 
13
     * @var 
14
     */
15
    protected $attributeManager;
16
17
    /**
18
     * The items contained in the collection.
19
     *
20
     * @var array
21
     */
22
    protected $items = [];
23
24
    /**
25
     * Create a new collection.
26
     *
27
     * @param  mixed  $items
28
     * 
29
     * @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...
30
     */
31
    public function __construct($items = [])
32
    {
33
        if (!is_array($items) || func_num_args() > 1) {
34
            $items = func_get_args();
35
        }
36
37
        $this->attributeManager = new AttributeManager();
38
        $this->items = $items;
39
40
        $this->formatFields();
41
    }
42
43
    /**
44
     * Attempt to reformat the current attribute items array
45
     * into the most usable format (an Attribute Collection).
46
     * 
47
     * @return void
48
     */
49
    public function formatFields()
50
    {
51
        $items = $this->items;
52
        $formattedItems = [];
53
54
        foreach ($items as $name => $inner) {
55
            $formattedItems[$name] = $this->formatInnerField($name, $inner);
56
        }
57
58
        $this->items = $formattedItems;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Allows adding fields to the Attribute Collection 
65
     * 
66
     * @param array $items
67
     */
68
    public function add($items = [])
69
    {
70
        if (!is_array($items) || func_num_args() > 1) {
71
            $items = func_get_args();
72
        }
73
74
        $this->push($this->formatInnerField(null, $items));
75
    }
76
77
    /**
78
     * Format an Inner Field which can either be in the format
79
     * of an Array, an instance of BaseAttribute or even an
80
     * AttributeCollection object (which contains more!).
81
     *
82
     * @param  mixed $name
83
     * @param  mixed  $inner 
84
     * 
85
     * @return mixed
86
     */
87
    protected function formatInnerField($name = null, $inner = [])
88
    {
89
        if ($inner instanceof BaseAttribute) {
90
            return $inner;
91
        }
92
93
        if ($inner instanceof AttributeCollection) {
94
            $formattedItems = [];
95
96
            foreach ($inner->toArray() as $name => $inner) {
97
                $formattedItems[$name] = $this->formatInnerField($inner);
98
            }
99
100
            return $formattedItems;
101
        }
102
103
        if (is_scalar($inner) && $this->attributeManager->attributeTypeExists($inner)) {
104
            return $this->attributeManager->createAttribute($inner, $name, []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
        }
106
107
        if (is_array($inner) && array_key_exists('type', $inner) && $this->attributeManager->attributeTypeExists($inner['type'])) {
108
            $type = $inner['type'];
109
            array_forget($inner, 'type');
110
            return $this->attributeManager->createAttribute($type, $name, $inner);
0 ignored issues
show
Documentation introduced by
$inner is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
        }
112
113
        if (is_array($inner)) {
114
            return new AttributeCollection($inner);
115
        }
116
    }
117
}
118