Passed
Push — master ( 42a16a...44846f )
by Andrea Marco
12:16 queued 14s
created

SelfAware::resolveMetaAttribute()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 7
nop 1
crap 5
1
<?php
2
3
namespace Cerbero\Enum\Concerns;
4
5
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum 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 Cerbero\Enum\Attributes\Meta;
7
use ReflectionAttribute;
8
use ReflectionEnum;
0 ignored issues
show
Bug introduced by
The type ReflectionEnum 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...
9
use ReflectionEnumUnitCase;
0 ignored issues
show
Bug introduced by
The type ReflectionEnumUnitCase 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...
10
use ReflectionMethod;
11
use ValueError;
12
13
/**
14
 * The trait to make an enum self-aware.
15
 */
16
trait SelfAware
17
{
18
    /**
19
     * Determine whether the enum is pure.
20
     */
21 96
    public static function isPure(): bool
22
    {
23 96
        return !self::isBacked();
24
    }
25
26
    /**
27
     * Determine whether the enum is backed.
28
     */
29 96
    public static function isBacked(): bool
30
    {
31 96
        return is_subclass_of(self::class, BackedEnum::class);
32
    }
33
34
    /**
35
     * Retrieve all the meta names of the enum.
36
     *
37
     * @return string[]
38
     */
39 3
    public static function metaNames(): array
40
    {
41 3
        $meta = [];
42 3
        $enum = new ReflectionEnum(self::class);
43
44 3
        foreach ($enum->getAttributes(Meta::class) as $attribute) {
45 3
            array_push($meta, ...$attribute->newInstance()->names());
46
        }
47
48 2
        foreach ($enum->getCases() as $case) {
49 2
            foreach ($case->getAttributes(Meta::class) as $attribute) {
50 2
                array_push($meta, ...$attribute->newInstance()->names());
51
            }
52
        }
53
54 2
        foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
55 2
            if (! $method->isStatic() && $method->getFileName() == $enum->getFileName()) {
56 2
                $meta[] = $method->getShortName();
57
            }
58
        }
59
60 2
        return array_values(array_unique($meta));
61
    }
62
63
    /**
64
     * Retrieve the given item of this case.
65
     *
66
     * @template TItemValue
67
     *
68
     * @param (callable(self): TItemValue)|string $item
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): TItemValue)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
69
     * @return TItemValue
70
     * @throws ValueError
71
     */
72 44
    public function resolveItem(callable|string $item): mixed
73
    {
74
        return match (true) {
75 44
            is_callable($item) => $item($this),
76 34
            property_exists($this, $item) => $this->$item,
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type callable; however, parameter $property of property_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            property_exists($this, /** @scrutinizer ignore-type */ $item) => $this->$item,
Loading history...
77 44
            default => $this->resolveMeta($item),
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type callable; however, parameter $meta of Cerbero\Enum\Concerns\SelfAware::resolveMeta() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

77
            default => $this->resolveMeta(/** @scrutinizer ignore-type */ $item),
Loading history...
78
        };
79
    }
80
81
    /**
82
     * Retrieve the given meta of this case.
83
     *
84
     * @throws ValueError
85
     */
86 35
    public function resolveMeta(string $meta): mixed
87
    {
88 35
        $enum = new ReflectionEnum($this);
89 35
        $enumFileName = $enum->getFileName();
90
91 35
        foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
92 35
            if (! $method->isStatic() && $method->getFileName() == $enumFileName && $method->getShortName() == $meta) {
93 7
                return $this->$meta();
94
            }
95
        }
96
97 28
        return $this->resolveMetaAttribute($meta);
98
    }
99
100
    /**
101
     * Retrieve the given meta from the attributes.
102
     *
103
     * @throws ValueError
104
     */
105 40
    public function resolveMetaAttribute(string $meta): mixed
106
    {
107 40
        $case = new ReflectionEnumUnitCase($this, $this->name);
108
109 40
        foreach ($case->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
110 40
            if (($metadata = $attribute->newInstance())->has($meta)) {
111 36
                return $metadata->get($meta);
112
            }
113
        }
114
115 36
        foreach ($case->getEnum()->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
116 36
            if (($metadata = $attribute->newInstance())->has($meta)) {
117 32
                return $metadata->get($meta);
118
            }
119
        }
120
121 4
        throw new ValueError(sprintf('"%s" is not a valid meta for enum "%s"', $meta, self::class));
122
    }
123
124
    /**
125
     * Retrieve the value of a backed case or the name of a pure case.
126
     */
127 2
    public function value(): string|int
128
    {
129 2
        return $this->value ?? $this->name;
130
    }
131
}
132