Passed
Branch feature/cli (91c011)
by Andrea Marco
12:59
created

Backed::backCases()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
cc 2
nc 1
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\Enum\Enums;
6
7
use Cerbero\Enum\Attributes\Meta;
8
use Cerbero\Enum\Concerns\Enumerates;
9
use Generator;
10
11
use function Cerbero\Enum\camel;
12
use function Cerbero\Enum\parseCaseValue;
13
use function Cerbero\Enum\snake;
14
15
/**
16
 * The backed value when generating new enums.
17
 */
18
enum Backed
19
{
20
    use Enumerates;
21
22
    #[Meta(label: 'The enum is pure, no values needed')]
23
    case pure;
24
25
    #[Meta(label: 'Custom values to assign manually')]
26
    case custom;
27
28
    #[Meta(label: 'The name in snake case (case_one)')]
29
    case snake;
30
31
    #[Meta(label: 'The name in camel case (caseOne)')]
32
    case camel;
33
34
    #[Meta(label: 'The name in kebab case (case-one)')]
35
    case kebab;
36
37
    #[Meta(label: 'The name in upper case (CASEONE)')]
38
    case upper;
39
40
    #[Meta(label: 'The name in lower case (caseone)')]
41
    case lower;
42
43
    #[Meta(label: 'Integer starting from 0 (0, 1, 2...)')]
44
    case int0;
45
46
    #[Meta(label: 'Integer starting from 1 (1, 2, 3...)')]
47
    case int1;
48
49
    #[Meta(label: 'Bitwise flag (1, 2, 4...)')]
50
    case bitwise;
51
52
    /**
53
     * Retrieve the given cases, optionally backed by the provided backing strategy.
54
     *
55
     * @param string[] $cases
56
     * @return array<string, string|int|null>
57
     * @throws \ValueError
58
     */
59
    public static function backCases(array $cases, ?string $strategy = null): array
60
    {
61
        $backed = match (true) {
62
            is_string($strategy) => self::fromName($strategy),
0 ignored issues
show
Bug introduced by
The method fromName() does not exist on Cerbero\Enum\Enums\Backed. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

62
            is_string($strategy) => self::/** @scrutinizer ignore-call */ fromName($strategy),
Loading history...
63
            default => str_contains($cases[0] ?? '', '=') ? self::custom : self::pure,
64
        };
65
66
        return $backed->back($cases);
67
    }
68
69
    /**
70
     * Retrieve the given cases after backing them.
71
     *
72
     * @param string[] $cases
73
     * @return array<string, string|int|null>
74
     */
75
    public function back(array $cases): array
76
    {
77
        $backedCases = [];
78
        $pairs = $this->yieldPairs();
79
80
        foreach ($cases as $case) {
81
            $backedCases += $pairs->send($case);
82
83
            $pairs->next();
84
        }
85
86
        return $backedCases;
87
    }
88
89
    /**
90
     * Yield the case-value pairs.
91
     *
92
     * @return Generator<int, array<string, string|int|null>>
93
     */
94
    public function yieldPairs(): Generator
95
    {
96
        $i = 0;
97
98
        $callback = match ($this) {
99
            self::pure => fn(string $name) => [$name => null],
100
            self::custom => parseCaseValue(...),
0 ignored issues
show
Bug introduced by
The type Cerbero\Enum\parseCaseValue 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...
101
            self::snake => fn(string $name) => [$name => snake($name)],
102
            self::camel => fn(string $name) => [$name => camel($name)],
103
            self::kebab => fn(string $name) => [$name => snake($name, '-')],
104
            self::upper => fn(string $name) => [$name => strtoupper($name)],
105
            self::lower => fn(string $name) => [$name => strtolower($name)],
106
            self::int0 => fn(string $name, int $i) => [$name => $i],
107
            self::int1 => fn(string $name, int $i) => [$name => $i + 1],
108
            self::bitwise => fn(string $name, int $i) => [$name => "1 << {$i}"],
109
        };
110
111
        /** @phpstan-ignore while.alwaysTrue */
112
        while (true) {
113
            /** @phpstan-ignore-next-line */
114
            yield $callback(yield, $i++);
115
        }
116
    }
117
}
118