Passed
Branch feature/first-release (dd5f38)
by Andrea Marco
01:50
created

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