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
Pull Request — master (#4)
by Tom
05:24
created

HasEnums::scopeWhereNotEnum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Spatie\Enum\Laravel;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use InvalidArgumentException;
8
use Spatie\Enum\Enumerable;
9
use Spatie\Enum\Laravel\Exceptions\InvalidEnumError;
10
use Spatie\Enum\Laravel\Exceptions\NoSuchEnumField;
11
12
/**
13
 * @mixin Model
14
 */
15
trait HasEnums
16
{
17 80
    public function setAttribute($key, $value)
18
    {
19 80
        return $this->isEnumAttribute($key)
20 80
            ? $this->setEnumAttribute($key, $value)
21 72
            : parent::setAttribute($key, $value);
22
    }
23
24 28
    public function getAttribute($key)
25
    {
26 28
        $value = parent::getAttribute($key);
27
28 28
        return $this->isEnumAttribute($key)
29 28
            ? $this->getEnumAttribute($key, $value)
30 28
            : $value;
31
    }
32
33
    /**
34
     * @param \Illuminate\Database\Eloquent\Builder $builder
35
     * @param string $key
36
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
37
     *
38
     * @see \Illuminate\Database\Eloquent\Builder::whereIn()
39
     */
40 32
    public function scopeWhereEnum(
41
        Builder $builder,
42
        string $key,
43
        $enumerables
44
    ): void {
45 32
        $this->buildEnumScope(
46 32
            $builder,
47 32
            'whereIn',
48 16
            $key,
49 16
            $enumerables
50
        );
51 28
    }
52
53
    /**
54
     * @param \Illuminate\Database\Eloquent\Builder $builder
55
     * @param string $key
56
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
57
     *
58
     * @see \Illuminate\Database\Eloquent\Builder::orWhereIn()
59
     */
60 12
    public function scopeOrWhereEnum(
61
        Builder $builder,
62
        string $key,
63
        $enumerables
64
    ): void {
65 12
        $this->buildEnumScope(
66 12
            $builder,
67 12
            'orWhereIn',
68 6
            $key,
69 6
            $enumerables
70
        );
71 8
    }
72
73
    /**
74
     * @param \Illuminate\Database\Eloquent\Builder $builder
75
     * @param string $key
76
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
77
     *
78
     * @see \Illuminate\Database\Eloquent\Builder::whereNotIn()
79
     */
80 20
    public function scopeWhereNotEnum(
81
        Builder $builder,
82
        string $key,
83
        $enumerables
84
    ): void {
85 20
        $this->buildEnumScope(
86 20
            $builder,
87 20
            'whereNotIn',
88 10
            $key,
89 10
            $enumerables
90
        );
91 16
    }
92
93
    /**
94
     * @param \Illuminate\Database\Eloquent\Builder $builder
95
     * @param string $key
96
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
97
     *
98
     * @see \Illuminate\Database\Eloquent\Builder::orWhereNotIn()
99
     */
100 12
    public function scopeOrWhereNotEnum(
101
        Builder $builder,
102
        string $key,
103
        $enumerables
104
    ): void {
105 12
        $this->buildEnumScope(
106 12
            $builder,
107 12
            'orWhereNotIn',
108 6
            $key,
109 6
            $enumerables
110
        );
111 8
    }
112
113
    /**
114
     * @param string $key
115
     * @param int|string|\Spatie\Enum\Enumerable $value
116
     *
117
     * @return $this
118
     */
119 80
    protected function setEnumAttribute(string $key, $value)
120
    {
121 80
        $enumClass = $this->getEnumClass($key);
122
123 76
        if (is_string($value) || is_int($value)) {
124 16
            $value = $this->asEnum($enumClass, $value);
125
        }
126
127 76
        if (! is_a($value, $enumClass)) {
128 4
            throw InvalidEnumError::make(static::class, $key, $enumClass, get_class($value));
129
        }
130
131 72
        $this->attributes[$key] = $this->getStoredValue($key, $value);
0 ignored issues
show
Bug introduced by
The property attributes 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...
132
133 72
        return $this;
134
    }
135
136
    /**
137
     * @param string $key
138
     * @param \Spatie\Enum\Enumerable $enum
139
     *
140
     * @return int|string
141
     */
142 72
    protected function getStoredValue(string $key, Enumerable $enum)
143
    {
144 72
        return $this->hasCast($key, ['int', 'integer'])
0 ignored issues
show
Bug introduced by
It seems like hasCast() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
145 4
            ? $enum->getIndex()
146 72
            : $enum->getValue();
147
    }
148
149
    /**
150
     * @param string $key
151
     * @param int|string $value
152
     *
153
     * @return \Spatie\Enum\Enumerable
154
     */
155 72
    protected function getEnumAttribute(string $key, $value): Enumerable
156
    {
157 72
        return $this->asEnum($this->getEnumClass($key), $value);
158
    }
159
160 96
    protected function isEnumAttribute(string $key): bool
161
    {
162 96
        return isset($this->enums[$key]);
0 ignored issues
show
Bug introduced by
The property enums 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...
163
    }
164
165 80
    protected function getEnumClass(string $key): string
166
    {
167 80
        $enumClass = $this->enums[$key];
168 80
        $enumInterface = Enumerable::class;
169 80
        $classImplementsEnumerable = class_implements($enumClass)[$enumInterface] ?? false;
170
171 80
        if (! $classImplementsEnumerable) {
172 4
            throw new InvalidArgumentException("Expected {$enumClass} to implement {$enumInterface}");
173
        }
174
175 76
        return $enumClass;
176
    }
177
178
    /**
179
     * @param string $class
180
     * @param int|string $value
181
     *
182
     * @return \Spatie\Enum\Enumerable
183
     */
184 72
    protected function asEnum(string $class, $value): Enumerable
185
    {
186 72
        if ($value instanceof Enumerable) {
187 36
            return $value;
188
        }
189
190 40
        return forward_static_call(
191 40
            $class . '::make',
192 20
            $value
193
        );
194
    }
195
196
    /**
197
     * @param \Illuminate\Database\Eloquent\Builder $builder
198
     * @param string $method
199
     * @param string $key
200
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
201
     */
202 60
    protected function buildEnumScope(
203
        Builder $builder,
204
        string $method,
205
        string $key,
206
        $enumerables
207
    ): void {
208 60
        if (! $this->isEnumAttribute($key)) {
209 16
            throw NoSuchEnumField::make($key, get_class($this));
210
        }
211
212 44
        $enumerables = is_array($enumerables) ? $enumerables : [$enumerables];
213
214 44
        $builder->$method(
215 44
            $key,
216
            array_map(function ($value) use ($key) {
217 44
                return $this->getStoredValue($key, $this->getEnumAttribute($key, $value));
218 44
            }, $enumerables)
219
        );
220 44
    }
221
}
222