Passed
Push — develop ( 9b8c4e...79e7ae )
by Andrea Marco
03:34 queued 14s
created

MethodAnnotations::forTranslations()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Services;
6
7
use Cerbero\Enum\Services\MethodAnnotations as BaseMethodAnnotations;
8
use Cerbero\LaravelEnum\Data\MethodAnnotation;
9
use Cerbero\LaravelEnum\Enums;
10
use Illuminate\Support\Facades\Lang;
11
12
use function Cerbero\LaravelEnum\metaReturnType;
13
14
/**
15
 * The method annotations collector.
16
 *
17
 * @property-read Inspector<\UnitEnum> $inspector
18
 */
19
final class MethodAnnotations extends BaseMethodAnnotations
20
{
21
    /**
22
     * Retrieve all the method annotations.
23
     *
24
     * @return array<string, MethodAnnotation>
25
     */
26 1
    public function all(): array
27
    {
28
        /** @var array<string, MethodAnnotation> */
29 1
        return [
30 1
            ...$this->forCaseNames(),
31 1
            ...$this->forMetaAttributes(),
32 1
            ...$this->forTranslations(),
33 1
            ...$this->includeExisting ? $this->existing() : [],
34 1
        ];
35
    }
36
37
    /**
38
     * Retrieve the method annotations for the case names.
39
     *
40
     * @return array<string, MethodAnnotation>
41
     */
42 1
    public function forCaseNames(): array
43
    {
44 1
        $annotations = [];
45
46 1
        foreach ($this->inspector->cases() as $case) {
47 1
            $annotations[$case->name] = $this->inspector->caseAnnotation($case);
48
        }
49
50 1
        return $annotations;
51
    }
52
53
    /**
54
     * Retrieve the method annotations for the meta attributes.
55
     *
56
     * @return array<string, MethodAnnotation>
57
     */
58 1
    public function forMetaAttributes(): array
59
    {
60 1
        $annotations = [];
61 1
        $cases = $this->inspector->cases();
62
63 1
        foreach ($this->inspector->metaAttributeNames() as $meta) {
64 1
            $returnType = metaReturnType($meta, $cases);
65
66
            /** @var class-string $class */
67 1
            $annotations[$meta] = method_exists($class = ltrim($returnType, '?'), '__invoke')
68 1
                ? MethodAnnotation::forInvokable($meta, $class)
69 1
                : MethodAnnotation::instance($meta, $returnType);
70
        }
71
72 1
        return $annotations;
73
    }
74
75
    /**
76
     * Retrieve the method annotations for the translations.
77
     *
78
     * @return array<string, MethodAnnotation>
79
     */
80 1
    public function forTranslations(): array
81
    {
82 1
        $annotations = [];
83
84 1
        foreach ($this->inspector->cases() as $case) {
85 1
            $key = Enums::resolveTranslationKey($case);
86
87 1
            if ($key === $translations = Lang::get($key)) {
88 1
                continue;
89
            }
90
91
            /** @var array<string, string> $translations */
92 1
            foreach ($translations as $name => $translation) {
93 1
                $annotations[$name] ??= MethodAnnotation::forTranslation($name, $translation);
94
            }
95
        }
96
97 1
        return $annotations;
98
    }
99
}
100