Passed
Push — feature/second-release ( e64b84...683aeb )
by Andrea Marco
02:47
created

MethodAnnotations::existing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
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 $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 1
        return [
29 1
            ...$this->forCaseNames(),
30 1
            ...$this->forMetaAttributes(),
31 1
            ...$this->forTranslations(),
32 1
            ...$this->includeExisting ? $this->existing() : [],
33 1
        ];
34
    }
35
36
    /**
37
     * Retrieve the method annotations for the case names.
38
     *
39
     * @return array<string, MethodAnnotation>
40
     */
41 1
    public function forCaseNames(): array
42
    {
43 1
        $annotations = [];
44
45 1
        foreach ($this->inspector->cases() as $case) {
46 1
            $annotations[$case->name] = $this->inspector->caseAnnotation($case);
47
        }
48
49 1
        return $annotations;
50
    }
51
52
    /**
53
     * Retrieve the method annotations for the meta attributes.
54
     *
55
     * @return array<string, MethodAnnotation>
56
     */
57 1
    public function forMetaAttributes(): array
58
    {
59 1
        $annotations = [];
60 1
        $cases = $this->inspector->cases();
61
62 1
        foreach ($this->inspector->metaAttributeNames() as $meta) {
63 1
            $returnType = metaReturnType($meta, $cases);
64
65
            /** @var class-string $class */
66 1
            $annotations[$meta] = method_exists($class = ltrim($returnType, '?'), '__invoke')
67 1
                ? MethodAnnotation::forInvokable($meta, $class)
68 1
                : MethodAnnotation::instance($meta, $returnType);
69
        }
70
71 1
        return $annotations;
72
    }
73
74
    /**
75
     * Retrieve the method annotations for the translations.
76
     *
77
     * @return array<string, MethodAnnotation>
78
     */
79 1
    public function forTranslations(): array
80
    {
81 1
        $annotations = [];
82
83 1
        foreach ($this->inspector->cases() as $case) {
84 1
            $key = Enums::resolveTranslationKey($case);
85
86 1
            if ($key === $translations = Lang::get($key)) {
87 1
                continue;
88
            }
89
90
            /** @var array<string, string> $translations */
91 1
            foreach ($translations as $name => $translation) {
92 1
                $annotations[$name] ??= MethodAnnotation::forTranslation($name, $translation);
93
            }
94
        }
95
96 1
        return $annotations;
97
    }
98
}
99