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 (#44)
by Tom
23:55 queued 11:07
created

HasEnums::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Spatie\Enum\Laravel;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
use Spatie\Enum\Enumerable;
10
use Spatie\Enum\Laravel\Exceptions\ExpectsArrayOfEnumsField;
11
use Spatie\Enum\Laravel\Exceptions\InvalidEnumError;
12
use Spatie\Enum\Laravel\Exceptions\NoSuchEnumField;
13
use Spatie\Enum\Laravel\Exceptions\NotNullableEnumField;
14
15
/**
16
 * @mixin Model
17 80
 */
18
trait HasEnums
19 80
{
20 80
    /**
21 72
     * @param \Illuminate\Database\Eloquent\Builder $builder
22
     * @param string $key
23
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
24 28
     *
25
     * @see \Illuminate\Database\Eloquent\Builder::whereIn()
26 28
     */
27
    public function scopeWhereEnum(
28 28
        Builder $builder,
29 28
        string $key,
30 28
        $enumerables
31
    ): void {
32
        $this->buildEnumScope(
33
            $builder,
34
            'whereIn',
35
            $key,
36
            $enumerables
37
        );
38
    }
39
40 32
    /**
41
     * @param \Illuminate\Database\Eloquent\Builder $builder
42
     * @param string $key
43
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
44
     *
45 32
     * @see \Illuminate\Database\Eloquent\Builder::orWhereIn()
46 32
     */
47 32
    public function scopeOrWhereEnum(
48 16
        Builder $builder,
49 16
        string $key,
50
        $enumerables
51 28
    ): void {
52
        $this->buildEnumScope(
53
            $builder,
54
            'orWhereIn',
55
            $key,
56
            $enumerables
57
        );
58
    }
59
60 12
    /**
61
     * @param \Illuminate\Database\Eloquent\Builder $builder
62
     * @param string $key
63
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
64
     *
65 12
     * @see \Illuminate\Database\Eloquent\Builder::whereNotIn()
66 12
     */
67 12
    public function scopeWhereNotEnum(
68 6
        Builder $builder,
69 6
        string $key,
70
        $enumerables
71 8
    ): void {
72
        $this->buildEnumScope(
73
            $builder,
74
            'whereNotIn',
75
            $key,
76
            $enumerables
77
        );
78
    }
79
80 20
    /**
81
     * @param \Illuminate\Database\Eloquent\Builder $builder
82
     * @param string $key
83
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
84
     *
85 20
     * @see \Illuminate\Database\Eloquent\Builder::orWhereNotIn()
86 20
     */
87 20
    public function scopeOrWhereNotEnum(
88 10
        Builder $builder,
89 10
        string $key,
90
        $enumerables
91 16
    ): void {
92
        $this->buildEnumScope(
93
            $builder,
94
            'orWhereNotIn',
95
            $key,
96
            $enumerables
97
        );
98
    }
99
100 12
    /**
101
     * @param string $key
102
     * @param \Spatie\Enum\Enumerable $enum
103
     *
104
     * @return int|string
105 12
     */
106 12
    protected function getStoredValue(string $key, Enumerable $enum)
107 12
    {
108 6
        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...
109 6
            ? $enum->getIndex()
110
            : $enum->getValue();
111 8
    }
112
113
    protected function isEnumAttribute(string $key): bool
114
    {
115
        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...
116
    }
117
118
    /**
119 80
     * @param string $class
120
     * @param int|string $value
121 80
     *
122
     * @return \Spatie\Enum\Enumerable
123 76
     */
124 16
    protected function asEnum(string $class, $value): Enumerable
125
    {
126
        if ($value instanceof Enumerable) {
127 76
            return $value;
128 4
        }
129
130
        return forward_static_call(
131 72
            $class.'::make',
132
            $value
133 72
        );
134
    }
135
136
    /**
137
     * @param \Illuminate\Database\Eloquent\Builder $builder
138
     * @param string $method
139
     * @param string $key
140
     * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables
141
     */
142 72
    protected function buildEnumScope(
143
        Builder $builder,
144 72
        string $method,
145 4
        string $key,
146 72
        $enumerables
147
    ): void {
148
        if (! $this->isEnumAttribute($key)) {
149
            throw NoSuchEnumField::make($key, static::class);
150
        }
151
152
        $enumerables = is_array($enumerables) ? $enumerables : [$enumerables];
153
154
        $builder->$method(
155 72
            $key,
156
            array_map(function ($value) use ($key) {
157 72
                return $this->getStoredValue($key, $this->asEnum($key, $value));
158
            }, $enumerables)
159
        );
160 96
    }
161
}
162