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
01:09
created

HasEnums::getEnumClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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