Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — main (#4854)
by Cristian
40:57 queued 26:46
created

BaseCollection::buildAttributes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Components;
4
5
use Backpack\CRUD\app\Library\Components\Attributes\BaseAttribute;
6
use Backpack\CRUD\app\Library\Components\Interfaces\SmartAttributeInterface;
7
use Backpack\CRUD\app\Library\Components\Interfaces\SmartCollectionInterface;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Facades\Validator;
10
11
class BaseCollection implements SmartCollectionInterface
12
{
13
    private Collection $attributes;
14
15
    public function __construct(array|string $initAttributes,
16
                                array $componentAttributes = [],
17
                                array $rules = [],
18
                                array $defaults = [],
19
                                private array $blockedAttributes = []
20
                            ) {
21
        $this->attributes = $this->buildAttributes($initAttributes, $componentAttributes);
22
23
        $this->setAttributeDefaults($componentAttributes);
24
25
        $this->validate();
26
27
        $item = $this->getItemByName((string) $this->getAttributeValue('name'));
28
        if ($item) {
29
            $this->attributes = $this->buildAttributes((array) $item, $componentAttributes);
30
        } else {
31
            $this->addCollectionItem($this);
32
        }
33
    }
34
35
    private function buildAttributes(array|string $attributes, $componentAttributes)
36
    {
37
        if (is_string($attributes)) {
0 ignored issues
show
introduced by
The condition is_string($attributes) is always false.
Loading history...
38
            $attributes = ['name' => $attributes];
39
        }
40
41
        $attributeNames = collect($componentAttributes)->mapWithKeys(function ($attribute) {
42
            return [$attribute::getAttributeName() => $attribute];
43
        })->toArray();
44
45
        return collect($attributes)->mapWithKeys(function ($value, $key) use ($attributeNames) {
0 ignored issues
show
Bug introduced by
$attributes of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

45
        return collect(/** @scrutinizer ignore-type */ $attributes)->mapWithKeys(function ($value, $key) use ($attributeNames) {
Loading history...
46
            if (isset($attributeNames[$key])) {
47
                return [$key => $attributeNames[$key]::make($key, $value, $attributeNames[$key]::getValidationRules($this))];
48
            }
49
50
            return [$key => new BaseAttribute($key, $value)];
51
        });
52
    }
53
54
    public function addCollectionItem($attributes)
55
    {
56
        $collection = $this->getCollection();
57
        $collection[$attributes->getAttributeValue('name')] = $attributes->toArray();
58
        $this->saveCollection($collection);
59
    }
60
61
    public function getItems(): Collection
62
    {
63
        return collect($this->getCollection());
64
    }
65
66
    public function getAttributes(): Collection
67
    {
68
        return $this->attributes;
69
    }
70
71
    public function getItemByName(string $name)
72
    {
73
        return $this->getItems()->first(function ($item, $key) use ($name) {
74
            return $key === $name;
75
        });
76
    }
77
78
    public function hasAttribute(string $attribute): bool
79
    {
80
        return $this->attributes->has($attribute);
81
    }
82
83
    public function getAttributeValue($attribute, $default = null)
84
    {
85
        return $this->attributes->has($attribute) ? $this->attributes->get($attribute, $default)->value() : $default;
86
    }
87
88
    public function validate()
89
    {
90
        $attributes = $this->toArray();
91
92
        $rules = $this->getValidationRules();
93
94
        $validator = Validator::make($attributes, $rules)->stopOnFirstFailure();
95
96
        if ($validator->fails()) {
97
            throw new \Exception($validator->errors()->first());
98
        }
99
    }
100
101
    private function getValidationRules()
102
    {
103
        $rules = [];
104
        foreach ($this->attributes as $attribute) {
105
            $rules[$attribute->name()] = $attribute->rules();
106
        }
107
108
        return $rules;
109
    }
110
111
    public function setAttribute($attribute, $value)
112
    {
113
        $item = $this->hasAttribute($attribute) ? $this->attributes->get($attribute)->setValue($value) : new BaseAttribute($attribute, $value);
114
        $this->updateItem($item);
115
    }
116
117
    private function updateItem(SmartAttributeInterface $item)
118
    {
119
        $this->attributes[$item->name()] = $item;
120
        $collection = $this->getCollection();
121
        $collection[$this->getAttributeValue('name')] = $this->toArray();
122
        $this->saveCollection($collection);
123
    }
124
125
    public function deleteItem(string $name)
126
    {
127
        $this->attributes->forget($name);
128
        $collection = $this->getCollection();
129
        $collection->forget($name);
130
        $this->saveCollection($collection);
131
    }
132
133
    public function toCollection(): Collection
134
    {
135
        return $this->attributes->mapWithKeys(function ($item, $key) {
136
            return [$key => $item->value()];
137
        });
138
    }
139
140
    public function toArray(): array
141
    {
142
        return $this->toCollection()->toArray();
143
    }
144
145
    private function setAttributeDefaults($componentAttributes)
146
    {
147
        foreach ($componentAttributes as $attribute) {
148
            if (! $this->hasAttribute($attribute::getAttributeName())) {
149
                $this->attributes[$attribute::getAttributeName()] = $attribute::make($attribute::getAttributeName(), $attribute::getDefault($this), $attribute::getValidationRules($this));
150
            }
151
        }
152
    }
153
154
    public static function attributes(): array
155
    {
156
        return [];
157
    }
158
159
    public function getCollection()
160
    {
161
        return collect();
162
    }
163
164
    public function saveCollection($collection)
165
    {
166
        return [];
167
    }
168
}
169