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

Inspector::caseAnnotation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Services;
6
7
use Cerbero\Enum\Services\Inspector as BaseInspector;
8
use Cerbero\LaravelEnum\Capsules;
9
use Cerbero\LaravelEnum\Concerns;
10
use Cerbero\LaravelEnum\Data\MethodAnnotation;
11
use UnitEnum;
12
13
/**
14
 * The enum inspector.
15
 *
16
 * @template TEnum of UnitEnum
17
 *
18
 * @extends BaseInspector<TEnum>
19
 */
20
final class Inspector extends BaseInspector
21
{
22
    /**
23
     * The main trait to supercharge enums.
24
     *
25
     * @var class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
26
     */
27
    protected string $mainTrait = Concerns\Enumerates::class;
28
29
    /**
30
     * The capsules keyed by the related trait.
31
     *
32
     * @var array<class-string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, class-string>.
Loading history...
33
     */
34
    private array $capsules = [
35
        Concerns\EnumeratesCacheKeys::class => Capsules\CacheKey::class,
36
        Concerns\EnumeratesSessionKeys::class => Capsules\SessionKey::class,
37
    ];
38
39
    /**
40
     * Determine whether the given namespace matches the enum namespace.
41
     */
42 1
    public function hasSameNamespace(string $namespace): bool
43
    {
44 1
        return $this->reflection->getNamespaceName() . '\\' . class_basename($namespace) == $namespace;
45
    }
46
47
    /**
48
     * Retrieve the method annotation for the given case.
49
     */
50 1
    public function caseAnnotation(UnitEnum $case): MethodAnnotation
51
    {
52 1
        foreach ($this->capsules as $trait => $capsule) {
53 1
            if ($this->uses($trait)) {
54
                /** @var \BackedEnum $case */
55 1
                return MethodAnnotation::forCapsule($case, $capsule);
56
            }
57
        }
58
59 1
        return MethodAnnotation::forCase($case);
60
    }
61
62
    /**
63
     * Retrieve the use statements.
64
     *
65
     * @return array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
66
     */
67 1
    public function useStatements(bool $includeExisting = true): array
68
    {
69 1
        return $this->useStatements ??= [...new UseStatements($this, $includeExisting)];
70
    }
71
72
    /**
73
     * Retrieve the method annotations.
74
     *
75
     * @return array<string, MethodAnnotation>
76
     */
77 1
    public function methodAnnotations(bool $includeExisting = true): array
78
    {
79
        /** @var array<string, MethodAnnotation> */
80 1
        return $this->methodAnnotations ??= [...new MethodAnnotations($this, $includeExisting)];
81
    }
82
}
83