Backed::yieldPairs()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.7998
cc 2
nc 2
nop 0
crap 2
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 4
    public static function backCases(array $cases, ?string $strategy = null): array
60
    {
61 4
        $backed = match (true) {
62 4
            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 2
            default => str_contains($cases[0] ?? '', '=') ? self::custom : self::pure,
64 4
        };
65
66 4
        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 4
    public function back(array $cases): array
76
    {
77 4
        $backedCases = [];
78 4
        $pairs = $this->yieldPairs();
79
80 4
        foreach ($cases as $case) {
81 3
            $backedCases += $pairs->send($case);
82
83 3
            $pairs->next();
84
        }
85
86 4
        return $backedCases;
87
    }
88
89
    /**
90
     * Yield the case-value pairs.
91
     *
92
     * @return Generator<int, array<string, string|int|null>>
93
     */
94 3
    public function yieldPairs(): Generator
95
    {
96 3
        $i = 0;
97
98 3
        $callback = match ($this) {
99 3
            self::pure => fn(string $name) => [$name => null],
100 2
            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 2
            self::snake => fn(string $name) => [$name => snake($name)],
102 1
            self::camel => fn(string $name) => [$name => camel($name)],
103 1
            self::kebab => fn(string $name) => [$name => snake($name, '-')],
104 1
            self::upper => fn(string $name) => [$name => strtoupper($name)],
105 1
            self::lower => fn(string $name) => [$name => strtolower($name)],
106 1
            self::int0 => fn(string $name, int $i) => [$name => $i],
107 1
            self::int1 => fn(string $name, int $i) => [$name => $i + 1],
108 1
            self::bitwise => fn(string $name, int $i) => [$name => "1 << {$i}"],
109 3
        };
110
111
        /** @phpstan-ignore while.alwaysTrue */
112 3
        while (true) {
113
            /** @phpstan-ignore-next-line */
114 3
            yield $callback(yield, $i++);
115
        }
116
    }
117
}
118