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 (#25)
by Gaël
11:38
created

HasEnums::getAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 4
cts 4
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Spatie\Enum\Laravel;
4
5
use Spatie\Enum\Enumerable;
6
use InvalidArgumentException;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
use Spatie\Enum\Laravel\Exceptions\NoSuchEnumField;
10
use Spatie\Enum\Laravel\Exceptions\InvalidEnumError;
11
12
/**
13
 * @mixin Model
14
 */
15
trait HasEnums
16
{
17 80 View Code Duplication
    public function setAttribute($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
    {
19 80
        if (! $this->isEnumAttribute($key) || $this->isNullableEnum($key, $value)) {
20 80
            return parent::setAttribute($key, $value);
21 72
        }
22
23
        return $this->setEnumAttribute($key, $value);
24 28
    }
25
26 28 View Code Duplication
    public function getAttribute($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28 28
        $value = parent::getAttribute($key);
29 28
30 28
        if (! $this->isEnumAttribute($key) || $this->isNullableEnum($key, $value)) {
31
            return $value;
32
        }
33
34
        return $this->getEnumAttribute($key, $value);
35
    }
36
37
    /**
38
     * @param \Illuminate\Database\Eloquent\Builder $builder
39
     * @param string $key
40 32
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
41
     *
42
     * @see \Illuminate\Database\Eloquent\Builder::whereIn()
43
     */
44
    public function scopeWhereEnum(
45 32
        Builder $builder,
46 32
        string $key,
47 32
        $enumerables
48 24
    ): void {
49 24
        $this->buildEnumScope(
50
            $builder,
51 28
            'whereIn',
52
            $key,
53
            $enumerables
54
        );
55
    }
56
57
    /**
58
     * @param \Illuminate\Database\Eloquent\Builder $builder
59
     * @param string $key
60 12
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
61
     *
62
     * @see \Illuminate\Database\Eloquent\Builder::orWhereIn()
63
     */
64
    public function scopeOrWhereEnum(
65 12
        Builder $builder,
66 12
        string $key,
67 12
        $enumerables
68 9
    ): void {
69 9
        $this->buildEnumScope(
70
            $builder,
71 8
            'orWhereIn',
72
            $key,
73
            $enumerables
74
        );
75
    }
76
77
    /**
78
     * @param \Illuminate\Database\Eloquent\Builder $builder
79
     * @param string $key
80 20
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
81
     *
82
     * @see \Illuminate\Database\Eloquent\Builder::whereNotIn()
83
     */
84
    public function scopeWhereNotEnum(
85 20
        Builder $builder,
86 20
        string $key,
87 20
        $enumerables
88 15
    ): void {
89 15
        $this->buildEnumScope(
90
            $builder,
91 16
            'whereNotIn',
92
            $key,
93
            $enumerables
94
        );
95
    }
96
97
    /**
98
     * @param \Illuminate\Database\Eloquent\Builder $builder
99
     * @param string $key
100 12
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
101
     *
102
     * @see \Illuminate\Database\Eloquent\Builder::orWhereNotIn()
103
     */
104
    public function scopeOrWhereNotEnum(
105 12
        Builder $builder,
106 12
        string $key,
107 12
        $enumerables
108 9
    ): void {
109 9
        $this->buildEnumScope(
110
            $builder,
111 8
            'orWhereNotIn',
112
            $key,
113
            $enumerables
114
        );
115
    }
116
117
    /**
118
     * @param string $key
119 80
     * @param int|string|\Spatie\Enum\Enumerable $value
120
     *
121 80
     * @return $this
122
     */
123 76
    protected function setEnumAttribute(string $key, $value)
124 16
    {
125
        $enumClass = $this->getEnumClass($key);
126
127 76
        if (is_string($value) || is_int($value)) {
128 4
            $value = $this->asEnum($enumClass, $value);
129
        }
130
131 72
        if (! is_a($value, $enumClass)) {
132
            throw InvalidEnumError::make(static::class, $key, $enumClass, get_class($value));
133 72
        }
134
135
        $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...
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param string $key
142 72
     * @param \Spatie\Enum\Enumerable $enum
143
     *
144 72
     * @return int|string
145 4
     */
146 72
    protected function getStoredValue(string $key, Enumerable $enum)
147
    {
148
        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...
149
            ? $enum->getIndex()
150
            : $enum->getValue();
151
    }
152
153
    /**
154
     * @param string $key
155 72
     * @param int|string $value
156
     *
157 72
     * @return \Spatie\Enum\Enumerable
158
     */
159
    protected function getEnumAttribute(string $key, $value): Enumerable
160 96
    {
161
        return $this->asEnum($this->getEnumClass($key), $value);
162 96
    }
163
164
    protected function isEnumAttribute(string $key): bool
165 80
    {
166
        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...
167 80
    }
168 80
169 80
    protected function isNullableEnum(string $key, $value): bool
170
    {
171 80
        $enum = $this->enums[$key];
172 4
173
        if (! is_array($enum)) {
174
            return false;
175 76
        }
176
177
        if (! isset($enum['nullable'])) {
178
            return false;
179
        }
180
181
        return $enum['nullable'] && is_null($value);
182
    }
183
184 72
    protected function getEnumClass(string $key): string
185
    {
186 72
        $enumClass = $this->enums[$key];
187 36
188
        if (is_array($enumClass)) {
189
            $enumClass = $enumClass['class'] ?? null;
190 40
        }
191 40
192 30
        $enumInterface = Enumerable::class;
193
194
        if (is_null($enumClass)) {
195
            throw new InvalidArgumentException("Could not find Enumerable class in \$enums property {$key}");
196
        }
197
198
        $classImplementsEnumerable = class_implements($enumClass)[$enumInterface] ?? false;
199
200
        if (! $classImplementsEnumerable) {
201
            throw new InvalidArgumentException("Expected {$enumClass} to implement {$enumInterface}");
202 60
        }
203
204
        return $enumClass;
205
    }
206
207
    /**
208 60
     * @param string $class
209 16
     * @param int|string $value
210
     *
211
     * @return \Spatie\Enum\Enumerable
212 44
     */
213
    protected function asEnum(string $class, $value): Enumerable
214 44
    {
215 44
        if ($value instanceof Enumerable) {
216
            return $value;
217 44
        }
218 44
219
        return forward_static_call(
220 44
            $class.'::make',
221
            $value
222
        );
223
    }
224
225
    /**
226
     * @param \Illuminate\Database\Eloquent\Builder $builder
227
     * @param string $method
228
     * @param string $key
229
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
230
     */
231
    protected function buildEnumScope(
232
        Builder $builder,
233
        string $method,
234
        string $key,
235
        $enumerables
236
    ): void {
237
        if (! $this->isEnumAttribute($key)) {
238
            throw NoSuchEnumField::make($key, get_class($this));
239
        }
240
241
        $enumerables = is_array($enumerables) ? $enumerables : [$enumerables];
242
243
        $builder->$method(
244
            $key,
245
            array_map(function ($value) use ($key) {
246
                return $this->getStoredValue($key, $this->getEnumAttribute($key, $value));
247
            }, $enumerables)
248
        );
249
    }
250
}
251