Passed
Push — master ( 2a05cb...3d1925 )
by Andrea Marco
12:50 queued 15s
created

SelfAware::metaNames()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 3
nop 0
crap 4
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 98
    public static function isPure(): bool
22
    {
23 98
        return !self::isBacked();
24
    }
25
26
    /**
27
     * Determine whether the enum is backed.
28
     */
29 98
    public static function isBacked(): bool
30
    {
31 98
        return is_subclass_of(self::class, BackedEnum::class);
32
    }
33
34
    /**
35
     * Determine whether the enum is backed by integer.
36
     */
37 1
    public static function isBackedByInteger(): bool
38
    {
39 1
        return (new ReflectionEnum(self::class))->getBackingType()?->getName() === 'int';
40
    }
41
42
    /**
43
     * Determine whether the enum is backed by string.
44
     */
45 1
    public static function isBackedByString(): bool
46
    {
47 1
        return (new ReflectionEnum(self::class))->getBackingType()?->getName() === 'string';
48
    }
49
50
    /**
51
     * Retrieve all the meta names of the enum.
52
     *
53
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type Cerbero\Enum\Concerns\list 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...
54
     */
55 3
    public static function metaNames(): array
56
    {
57 3
        $meta = self::metaAttributeNames();
58 2
        $enum = new ReflectionEnum(self::class);
59
60 2
        foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
61 2
            if (! $method->isStatic() && $method->getFileName() == $enum->getFileName()) {
62 2
                $meta[] = $method->getShortName();
63
            }
64
        }
65
66 2
        return array_values(array_unique($meta));
67
    }
68
69
    /**
70
     * Retrieve all the meta attribute names of the enum.
71
     *
72
     * @return list<string>
73
     */
74 5
    public static function metaAttributeNames(): array
75
    {
76 5
        $meta = [];
77 5
        $enum = new ReflectionEnum(self::class);
78
79 5
        foreach ($enum->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
80 5
            array_push($meta, ...$attribute->newInstance()->names());
81
        }
82
83 4
        foreach ($enum->getCases() as $case) {
84 4
            foreach ($case->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
85 4
                array_push($meta, ...$attribute->newInstance()->names());
86
            }
87
        }
88
89 4
        return array_values(array_unique($meta));
90
    }
91
92
    /**
93
     * Retrieve the given item of this case.
94
     *
95
     * @template TItemValue
96
     *
97
     * @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...
98
     * @return TItemValue
99
     * @throws ValueError
100
     */
101 44
    public function resolveItem(callable|string $item): mixed
102
    {
103
        return match (true) {
104 44
            is_callable($item) => $item($this),
105 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

105
            property_exists($this, /** @scrutinizer ignore-type */ $item) => $this->$item,
Loading history...
106 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

106
            default => $this->resolveMeta(/** @scrutinizer ignore-type */ $item),
Loading history...
107
        };
108
    }
109
110
    /**
111
     * Retrieve the given meta of this case.
112
     *
113
     * @throws ValueError
114
     */
115 35
    public function resolveMeta(string $meta): mixed
116
    {
117 35
        $enum = new ReflectionEnum($this);
118 35
        $enumFileName = $enum->getFileName();
119
120 35
        foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
121 35
            if (! $method->isStatic() && $method->getFileName() == $enumFileName && $method->getShortName() == $meta) {
122 7
                return $this->$meta();
123
            }
124
        }
125
126 28
        return $this->resolveMetaAttribute($meta);
127
    }
128
129
    /**
130
     * Retrieve the given meta from the attributes.
131
     *
132
     * @throws ValueError
133
     */
134 40
    public function resolveMetaAttribute(string $meta): mixed
135
    {
136 40
        $case = new ReflectionEnumUnitCase($this, $this->name);
137
138 40
        foreach ($case->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
139 40
            if (($metadata = $attribute->newInstance())->has($meta)) {
140 36
                return $metadata->get($meta);
141
            }
142
        }
143
144 36
        foreach ($case->getEnum()->getAttributes(Meta::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
145 36
            if (($metadata = $attribute->newInstance())->has($meta)) {
146 32
                return $metadata->get($meta);
147
            }
148
        }
149
150 4
        throw new ValueError(sprintf('"%s" is not a valid meta for enum "%s"', $meta, self::class));
151
    }
152
153
    /**
154
     * Retrieve the value of a backed case or the name of a pure case.
155
     */
156 2
    public function value(): string|int
157
    {
158 2
        return $this->value ?? $this->name;
159
    }
160
}
161