Meta   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 1
b 0
f 0
dl 0
loc 48
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A names() 0 3 1
A get() 0 3 1
A __construct() 0 8 3
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\Enum\Attributes;
6
7
use Attribute;
8
use InvalidArgumentException;
9
10
/**
11
 * The attribute to declare the meta of an enum case.
12
 */
13
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
14
class Meta
15
{
16
    /**
17
     * The declared meta and related values.
18
     *
19
     * @var array<string, mixed>
20
     */
21
    protected array $all;
22
23
    /**
24
     * Instantiate the class.
25
     */
26 49
    public function __construct(mixed ...$meta)
27
    {
28 49
        foreach ($meta as $key => $value) {
29 49
            if (! is_string($key)) {
30 1
                throw new InvalidArgumentException('The name of meta must be a string');
31
            }
32
33 48
            $this->all[$key] = $value;
34
        }
35
    }
36
37
    /**
38
     * Retrieve the meta names.
39
     *
40
     * @return string[]
41
     */
42 8
    public function names(): array
43
    {
44 8
        return array_keys($this->all);
45
    }
46
47
    /**
48
     * Determine whether the given meta exists.
49
     */
50 44
    public function has(string $meta): bool
51
    {
52 44
        return array_key_exists($meta, $this->all);
53
    }
54
55
    /**
56
     * Retrieve the value for the given meta.
57
     */
58 40
    public function get(string $meta): mixed
59
    {
60 40
        return $this->all[$meta] ?? null;
61
    }
62
}
63