Enum   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
c 2
b 0
f 0
dl 0
loc 27
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A validate() 0 4 2
A passes() 0 15 4
1
<?php
2
3
namespace BiiiiiigMonster\LaravelEnum\Rules;
4
5
use BiiiiiigMonster\LaravelEnum\EnumServiceProvider;
6
use Closure;
7
use Illuminate\Contracts\Validation\ValidationRule;
8
9
class Enum implements ValidationRule
10
{
11
    public function __construct(protected string $enum)
12
    {
13
    }
14
15
    public function passes(string $attribute, mixed $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

15
    public function passes(/** @scrutinizer ignore-unused */ string $attribute, mixed $value): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17
        if (! enum_exists($this->enum)) {
0 ignored issues
show
Bug introduced by
The function enum_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        if (! /** @scrutinizer ignore-call */ enum_exists($this->enum)) {
Loading history...
18
            return false;
19
        }
20
21
        if ($value instanceof $this->enum) {
22
            return true;
23
        }
24
25
        if (! method_exists($this->enum, 'tryFrom')) {
26
            return false;
27
        }
28
29
        return ! is_null($this->enum::tryFrom($value));
30
    }
31
32
    public function validate(string $attribute, mixed $value, Closure $fail): void
33
    {
34
        if (! $this->passes($attribute, $value)) {
35
            $fail(EnumServiceProvider::LANG_NAMESPACE.'::validations.enumerate')->translate();
36
        }
37
    }
38
}
39