Passed
Push — main ( ca6308...1322d6 )
by Yunfeng
58s queued 12s
created

Enum::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
namespace BiiiiiigMonster\LaravelEnum\Rules;
4
5
use BiiiiiigMonster\LaravelEnum\Concerns\EnumTraits;
0 ignored issues
show
Bug introduced by
The type BiiiiiigMonster\LaravelEnum\Concerns\EnumTraits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use BiiiiiigMonster\LaravelEnum\EnumServiceProvider;
7
use Closure;
8
use Illuminate\Contracts\Validation\ValidationRule;
9
10
class Enum implements ValidationRule
11
{
12
    public function __construct(protected string $enum)
13
    {
14
    }
15
16
    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

16
    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...
17
    {
18
        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

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