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
Push — master ( b1b0e4...487ec3 )
by Tom
12:48 queued 11s
created

EnumRule::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Enum\Laravel\Rules;
4
5
use Exception;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Lang;
9
use Illuminate\Support\Str;
10
use InvalidArgumentException;
11
use Spatie\Enum\Enumerable;
12
13
class EnumRule implements Rule
14
{
15
    protected $rule = 'enum';
16
17
    /** @var Enumerable */
18
    protected $enum;
19
20
    /** @var string */
21
    protected $attribute;
22
23
    /** @var mixed */
24
    protected $value;
25
26
    public function __construct(string $enum)
27
    {
28
        if (! class_exists($enum) || ! isset(class_implements($enum)[Enumerable::class])) {
29
            throw new InvalidArgumentException("The given class {$enum} does not implement the Enumerable interface.");
30
        }
31
32
        $this->enum = $enum;
0 ignored issues
show
Documentation Bug introduced by
It seems like $enum of type string is incompatible with the declared type object<Spatie\Enum\Enumerable> of property $enum.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    public function passes($attribute, $value): bool
36
    {
37
        $this->attribute = $attribute;
38
        $this->value = $value;
39
40
        try {
41
            $this->enum::make($value);
42
43
            return true;
44
        } catch (Exception $ex) {
45
            return false;
46
        }
47
    }
48
49
    public function message(): string
50
    {
51
        return Lang::get('enum::validation.'.$this->rule, [
52
            'attribute' => $this->attribute,
53
            'value' => $this->value,
54
            'enum' => $this->enum,
55
            'other' => implode(', ', $this->getDisplayableOtherValues()),
56
        ]);
57
    }
58
59
    protected function getDisplayableOtherValues(): array
60
    {
61
        return array_map(function ($value): string {
62
            return $this->getValueTranslation($value) ?? $value;
63
        }, $this->getOtherValues());
64
    }
65
66
    /**
67
     * @param string|int $value
68
     *
69
     * @return string|null
70
     */
71
    protected function getValueTranslation($value): ?string
72
    {
73
        return Arr::get(Lang::get('enum::validation.enums'), $this->enum.'.'.Str::slug($this->enum::make($value)->getName(), '_'));
74
    }
75
76
    protected function getOtherValues(): array
77
    {
78
        return $this->enum::getValues();
79
    }
80
}
81