Compares   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 13
c 2
b 0
f 0
dl 0
loc 64
ccs 18
cts 18
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doesntHave() 0 3 1
A isNot() 0 3 1
A has() 0 9 3
A notIn() 0 3 1
A is() 0 3 2
A in() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\Enum\Concerns;
6
7
/**
8
 * The trait to compare the cases of an enum.
9
 */
10
trait Compares
11
{
12
    /**
13
     * Determine whether the enum includes the given target.
14
     */
15 32
    public static function has(mixed $target): bool
16
    {
17 32
        foreach (self::cases() as $case) {
18 32
            if ($case->is($target)) {
19 8
                return true;
20
            }
21
        }
22
23 24
        return false;
24
    }
25
26
    /**
27
     * Determine whether the enum does not include the given target.
28
     */
29 16
    public static function doesntHave(mixed $target): bool
30
    {
31 16
        return !self::has($target);
32
    }
33
34
    /**
35
     * Determine whether this case matches the given target.
36
     */
37 98
    public function is(mixed $target): bool
38
    {
39 98
        return in_array($target, [$this, self::isPure() ? $this->name : $this->value], true);
40
    }
41
42
    /**
43
     * Determine whether this case does not match the given target.
44
     */
45 16
    public function isNot(mixed $target): bool
46
    {
47 16
        return !$this->is($target);
48
    }
49
50
    /**
51
     * Determine whether this case matches at least one of the given targets.
52
     *
53
     * @param iterable<array-key, mixed> $targets
54
     */
55 32
    public function in(iterable $targets): bool
56
    {
57 32
        foreach ($targets as $target) {
58 32
            if ($this->is($target)) {
59 8
                return true;
60
            }
61
        }
62
63 24
        return false;
64
    }
65
66
    /**
67
     * Determine whether this case does not match any of the given targets.
68
     *
69
     * @param iterable<array-key, mixed> $targets
70
     */
71 16
    public function notIn(iterable $targets): bool
72
    {
73 16
        return !$this->in($targets);
74
    }
75
}
76