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 Pedro
40:26 queued 25:42
created

AttributeCollection   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 149
rs 9.84
wmc 32

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getItems() 0 3 1
A validate() 0 10 2
A __construct() 0 20 4
A updateItem() 0 4 1
A deleteItem() 0 4 1
A addAttribute() 0 5 2
A getAttributeValue() 0 3 2
A getCollectionRepository() 0 3 1
A toArray() 0 5 1
A setCollectionManager() 0 5 1
A replaceAttributes() 0 3 1
B setAttributeDefaults() 0 21 7
A setValidationRules() 0 12 4
A addCollectionOfAttributes() 0 3 1
A hasAttribute() 0 3 1
A buildAttributes() 0 8 2
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Components;
4
5
use Backpack\CRUD\app\Library\Components\Attributes\BackpackAttribute;
6
use Backpack\CRUD\app\Library\Components\Interfaces\AttributeInterface;
7
use Backpack\CRUD\app\Library\Components\Interfaces\SmartComponentInterface;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Facades\Validator;
10
11
class AttributeCollection
12
{
13
    protected Collection $items;
14
15
    public function __construct(array|string|Collection $attributes,
16
                                private CollectionRepository $collectionRepository,
17
                                ValidationRules|array $rules = [],
18
                                array $defaults = []
19
                            ) {
20
        $attributes = is_string($attributes) ? collect(['name' => $attributes]) : collect($attributes);
0 ignored issues
show
introduced by
The condition is_string($attributes) is always false.
Loading history...
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

20
        $attributes = is_string($attributes) ? collect(['name' => $attributes]) : collect(/** @scrutinizer ignore-type */ $attributes);
Loading history...
21
        $rules = is_array($rules) ? new ValidationRules($rules) : $rules;
0 ignored issues
show
introduced by
The condition is_array($rules) is always true.
Loading history...
22
23
        $defaults = collect($defaults)->mapWithKeys(function ($default, $attribute) {
24
            if (is_a($default, AttributeInterface::class, true)) {
25
                return [$default::getAttributeName() => $default];
26
            }
27
28
            return [$attribute => $default];
29
        })->toArray();
30
31
        $this->items = $this->buildAttributes($attributes, $rules, $defaults);
32
33
        assert($this->hasAttribute('name'), 'Component name cant be empty.');
34
        assert(is_string($this->getAttributeValue('name')), 'Component name must be a valid string.');
35
    }
36
37
    private function buildAttributes(Collection $attributes, ValidationRules $rules, array $defaults): Collection
38
    {
39
        return $attributes->mapWithKeys(function ($attribute, $key) use ($rules, $defaults) {
40
            if (! is_a($attribute, AttributeInterface::class, true)) {
41
                return [$key => new BackpackAttribute($key, $attribute, $defaults[$key] ?? null, $rules->for($key))];
42
            }
43
44
            return [$attribute::getAttributeName() => $attribute];
45
        });
46
    }
47
48
    public function setCollectionManager(CollectionRepository $collection)
49
    {
50
        $this->collectionRepository = $collection;
51
52
        return $collection;
53
    }
54
55
    public function hasAttribute($attribute)
56
    {
57
        return $this->items->has($attribute);
58
    }
59
60
    public function getAttributeValue($attribute, $default = null)
61
    {
62
        return $this->items->has($attribute) ? $this->items->get($attribute, $default)->value() : $default;
63
    }
64
65
    public function getCollectionRepository()
66
    {
67
        return $this->collectionRepository;
68
    }
69
70
    public function replaceAttributes(AttributeCollection $attributes)
71
    {
72
        $this->attributes = $attributes->toArray();
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
    }
74
75
    public function setValidationRules(array $rules)
76
    {
77
        $rules = new ValidationRules($rules);
78
79
        $this->items->each(function ($item, $key) use ($rules) {
80
            if (! is_string($key) && is_a($item, AttributeInterface::class, true)) {
81
                $key = $item::getAttributeName();
82
            }
83
            if (is_string($rules->for($key) ?? [])) {
84
                dd($key, $rules);
85
            }
86
            $item->setRules($rules->for($key) ?? []);
87
        });
88
    }
89
90
    public function validate($attributes = null, $rules = null)
91
    {
92
        $attributes = $attributes ?? $this->toArray();
93
94
        $rules = $rules ?? $this->rules;
0 ignored issues
show
Bug Best Practice introduced by
The property rules does not exist on Backpack\CRUD\app\Librar...nts\AttributeCollection. Did you maybe forget to declare it?
Loading history...
95
96
        $validator = Validator::make($attributes, $rules)->stopOnFirstFailure();
97
98
        if ($validator->fails()) {
99
            throw new \Exception($validator->errors()->first());
100
        }
101
    }
102
103
    public function getItems()
104
    {
105
        return $this->items;
106
    }
107
108
    public function addAttribute($attribute, $value)
109
    {
110
        $item = $this->hasAttribute($attribute) ? $this->items->get($attribute)->setValue($value) : new BackpackAttribute($attribute, $value);
111
        $item->validate($value);
112
        $this->updateItem($item);
113
    }
114
115
    public function addCollectionOfAttributes(SmartComponentInterface $item)
116
    {
117
        $this->collectionRepository->addItem($item->getAttribute('name'), $item->getAttributesArray());
118
    }
119
120
    private function updateItem(AttributeInterface|BackpackAttribute $item)
121
    {
122
        $this->items[$item->attribute] = $item;
0 ignored issues
show
Bug introduced by
The property attribute does not seem to exist on Backpack\CRUD\app\Librar...butes\BackpackAttribute.
Loading history...
123
        $this->collectionRepository->setItemAttributes($this->getAttributeValue('name'), $this->toArray());
124
    }
125
126
    public function deleteItem(string $name)
127
    {
128
        $this->items->forget($name);
129
        $this->collectionRepository->removeItem($name);
130
    }
131
132
    public function toArray()
133
    {
134
        return $this->items->mapWithKeys(function ($item, $key) {
135
            return [$key => $item->value()];
136
        })->toArray();
137
    }
138
139
    public function setAttributeDefaults(array $defaults)
140
    {
141
        foreach ($defaults as $key => $default) {
142
            if (! is_string($key) && is_a($default, AttributeInterface::class, true)) {
143
                $key = $default::getAttributeName();
144
            }
145
146
            if (! $this->items->has($key)) {
147
                if (is_a($default, AttributeInterface::class, true)) {
148
                    $this->items[$key] = $default::make($key, $default::getDefault($this), $default, []);
149
150
                    continue;
151
                }
152
153
                if ($default instanceof \Closure) {
154
                    $this->items[$key] = new BackpackAttribute($key, $default($this), $default, []);
155
156
                    continue;
157
                }
158
159
                $this->items[$key] = new BackpackAttribute($key, $default, $default, []);
160
            }
161
        }
162
    }
163
}
164