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 ( d20332...b9a354 )
by Aden
04:10
created

AttributeCollection::formatInnerField()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 36
rs 5.1612
cc 12
eloc 19
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\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
     * ModelAdmin which contains this Attribute Collection.
26
     * 
27
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
28
     */
29
    protected $modelManager;
30
31
    /**
32
     * Create a new collection.
33
     *
34
     * @param  mixed  $items
35
     * 
36
     * @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...
37
     */
38
    public function __construct($items = [], $modelManager = null)
39
    {
40
        $this->attributeManager = new AttributeManager();
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
     * @return void
52
     */
53
    public function formatFields()
54
    {
55
        $items = $this->items;
56
        $formattedItems = [];
57
58
        foreach ($items as $name => $inner) {
59
            $formattedItems[$name] = $this->formatInnerField($name, $inner);
60
        }
61
62
        $this->items = $formattedItems;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Allows adding fields to the Attribute Collection 
69
     * 
70
     * @param array $items
71
     */
72
    public function add($items = [])
73
    {
74
        if (!is_array($items) || func_num_args() > 1) {
75
            $items = func_get_args();
76
        }
77
78
        $this->push($this->formatInnerField(null, $items));
79
    }
80
81
    /**
82
     * Format an Inner Field which can either be in the format
83
     * of an Array, an instance of BaseAttribute or even an
84
     * AttributeCollection object (which contains more!).
85
     *
86
     * @param  mixed $name
87
     * @param  mixed  $inner 
88
     * 
89
     * @return mixed
90
     */
91
    protected function formatInnerField($name = null, $inner = [])
92
    {
93
        if ($inner instanceof BaseAttribute) {
94
            return $inner;
95
        }
96
97
        if ($inner instanceof AttributeCollection) {
98
            $formattedItems = [];
99
100
            foreach ($inner->toArray() as $name => $inner) {
101
                $formattedItems[$name] = $this->formatInnerField($inner);
102
            }
103
104
            return $formattedItems;
105
        }
106
107
        if (is_scalar($inner) && $this->attributeManager->attributeTypeExists($inner)) {
108
            return $this->attributeManager->createAttribute($inner, $name, [], $this->modelManager);
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...
Documentation introduced by
$this->modelManager is of type object<LaravelFlare\Flar...dmin\Models\ModelAdmin>, but the function expects a string|null.

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...
109
        }
110
111
        if (is_array($inner) && array_key_exists('type', $inner) && is_scalar($inner['type']) && $this->attributeManager->attributeTypeExists($inner['type'])) {
112
            $type = $inner['type'];
113
            array_forget($inner, 'type');
114
            return $this->attributeManager->createAttribute($type, $name, $inner, $this->modelManager);
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...
Documentation introduced by
$this->modelManager is of type object<LaravelFlare\Flar...dmin\Models\ModelAdmin>, but the function expects a string|null.

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...
115
        }
116
117
        if (is_array($inner)) {
118
            $formattedItems = [];
119
120
            foreach ($inner as $name => $inner) {
121
                $formattedItems[$name] = $this->formatInnerField($inner);
122
            }
123
124
            return $formattedItems;
125
        }
126
    }
127
}
128