Passed
Branch feature/first-release (686b42)
by Andrea Marco
01:53
created

CollectsCases::casesBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Cerbero\Enum\Concerns;
4
5
use Cerbero\Enum\CasesCollection;
6
7
/**
8
 * The trait to collect cases of an enum.
9
 *
10
 */
11
trait CollectsCases
12
{
13
    /**
14
     * Retrieve a collection with all the cases
15
     *
16
     * @return CasesCollection
17
     */
18 26
    public static function collect(): CasesCollection
19
    {
20 26
        return new CasesCollection(static::cases());
0 ignored issues
show
Bug introduced by
The method cases() does not exist on Cerbero\Enum\Concerns\CollectsCases. Did you maybe mean casesBy()? ( Ignorable by Annotation )

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

20
        return new CasesCollection(static::/** @scrutinizer ignore-call */ cases());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
    }
22
23
    /**
24
     * Retrieve all cases keyed by name
25
     *
26
     * @return array<string, \UnitEnum>
27
     */
28 1
    public static function casesByName(): array
29
    {
30 1
        return static::collect()->keyByName();
31
    }
32
33
    /**
34
     * Retrieve all cases keyed by value
35
     *
36
     * @return array<string|int, \BackedEnum>
37
     */
38 1
    public static function casesByValue(): array
39
    {
40 1
        return static::collect()->keyByValue();
41
    }
42
43
    /**
44
     * Retrieve all cases keyed by the given key
45
     *
46
     * @param callable|string $key
47
     * @return array<mixed, \UnitEnum>
48
     */
49 2
    public static function casesBy(callable|string $key): array
50
    {
51 2
        return static::collect()->keyBy($key);
52
    }
53
54
    /**
55
     * Retrieve all the names of the cases
56
     *
57
     * @return array
58
     */
59 1
    public static function names(): array
60
    {
61 1
        return static::collect()->names();
62
    }
63
64
    /**
65
     * Retrieve all the values of the backed cases
66
     *
67
     * @return array
68
     */
69 1
    public static function values(): array
70
    {
71 1
        return static::collect()->values();
72
    }
73
74
    /**
75
     * Retrieve all the keys of the backed cases
76
     *
77
     * @param callable|string $key
78
     * @return array
79
     */
80 2
    public static function keys(callable|string $key): array
81
    {
82 2
        return static::collect()->keys($key);
83
    }
84
85
    /**
86
     * Retrieve a collection with the filtered cases
87
     *
88
     * @param callable $callback
89
     * @return CasesCollection
90
     */
91 1
    public static function filter(callable $callback): CasesCollection
92
    {
93 1
        return static::collect()->filter($callback);
94
    }
95
96
    /**
97
     * Retrieve a collection of cases having the given names
98
     *
99
     * @param string ...$name
100
     * @return CasesCollection
101
     */
102 1
    public static function only(string ...$name): CasesCollection
103
    {
104 1
        return static::collect()->only(...$name);
105
    }
106
107
    /**
108
     * Retrieve a collection of cases not having the given names
109
     *
110
     * @param string ...$name
111
     * @return CasesCollection
112
     */
113 1
    public static function except(string ...$name): CasesCollection
114
    {
115 1
        return static::collect()->except(...$name);
116
    }
117
118
    /**
119
     * Retrieve a collection of backed cases having the given values
120
     *
121
     * @param string|int ...$value
122
     * @return CasesCollection
123
     */
124 1
    public static function onlyValues(string|int ...$value): CasesCollection
125
    {
126 1
        return static::collect()->onlyValues(...$value);
127
    }
128
129
    /**
130
     * Retrieve a collection of backed cases not having the given values
131
     *
132
     * @param string|int ...$value
133
     * @return CasesCollection
134
     */
135 1
    public static function exceptValues(string|int ...$value): CasesCollection
136
    {
137 1
        return static::collect()->exceptValues(...$value);
138
    }
139
140
    /**
141
     * Retrieve an array of values optionally keyed by the given key
142
     *
143
     * @param callable|string|null $value
144
     * @param callable|string|null $key
145
     * @return array
146
     */
147 4
    public static function pluck(callable|string $value = null, callable|string $key = null): array
148
    {
149 4
        return static::collect()->pluck($value, $key);
150
    }
151
152
    /**
153
     * Retrieve a collection of cases sorted by name ascending
154
     *
155
     * @return CasesCollection
156
     */
157 1
    public static function sort(): CasesCollection
158
    {
159 1
        return static::collect()->sort();
160
    }
161
162
    /**
163
     * Retrieve a collection of cases sorted by name descending
164
     *
165
     * @return CasesCollection
166
     */
167 1
    public static function sortDesc(): CasesCollection
168
    {
169 1
        return static::collect()->sortDesc();
170
    }
171
172
    /**
173
     * Retrieve a collection of cases sorted by value ascending
174
     *
175
     * @return CasesCollection
176
     */
177 1
    public static function sortByValue(): CasesCollection
178
    {
179 1
        return static::collect()->sortByValue();
180
    }
181
182
    /**
183
     * Retrieve a collection of cases sorted by value descending
184
     *
185
     * @return CasesCollection
186
     */
187 1
    public static function sortDescByValue(): CasesCollection
188
    {
189 1
        return static::collect()->sortDescByValue();
190
    }
191
192
    /**
193
     * Retrieve a collection of cases sorted by the given key ascending
194
     *
195
     * @param callable|string $key
196
     * @return CasesCollection
197
     */
198 2
    public static function sortBy(callable|string $key): CasesCollection
199
    {
200 2
        return static::collect()->sortBy($key);
201
    }
202
203
    /**
204
     * Retrieve a collection of cases sorted by the given key descending
205
     *
206
     * @param callable|string $key
207
     * @return CasesCollection
208
     */
209 2
    public static function sortDescBy(callable|string $key): CasesCollection
210
    {
211 2
        return static::collect()->sortDescBy($key);
212
    }
213
214
    /**
215
     * Retrieve the count of cases
216
     *
217
     * @return int
218
     */
219 1
    public static function count(): int
220
    {
221 1
        return static::collect()->count();
222
    }
223
}
224