Passed
Push — master ( 42a16a...44846f )
by Andrea Marco
12:16 queued 14s
created

CollectsCases::sortDescByValue()   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 0
crap 1
1
<?php
2
3
namespace Cerbero\Enum\Concerns;
4
5
use Cerbero\Enum\CasesCollection;
6
7
/**
8
 * The trait to collect the cases of an enum.
9
 */
10
trait CollectsCases
11
{
12
    /**
13
     * Retrieve a collection with all the cases.
14
     *
15
     * @return CasesCollection<array-key, self>
16
     */
17 34
    public static function collect(): CasesCollection
18
    {
19 34
        return new CasesCollection(self::cases());
20
    }
21
22
    /**
23
     * Retrieve the count of cases.
24
     */
25 1
    public static function count(): int
26
    {
27 1
        return self::collect()->count();
28
    }
29
30
    /**
31
     * Retrieve the first case.
32
     *
33
     * @param (callable(self, array-key): bool)|null $callback
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self, array-key): bool)|null at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
34
     */
35 2
    public static function first(callable $callback = null): ?self
36
    {
37 2
        return self::collect()->first($callback);
38
    }
39
40
    /**
41
     * Retrieve the name of all the cases.
42
     *
43
     * @return string[]
44
     */
45 1
    public static function names(): array
46
    {
47 1
        return self::collect()->names();
48
    }
49
50
    /**
51
     * Retrieve the value of all the backed cases.
52
     *
53
     * @return list<string|int>
0 ignored issues
show
Bug introduced by
The type Cerbero\Enum\Concerns\list 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...
54
     */
55 1
    public static function values(): array
56
    {
57 1
        return self::collect()->values();
58
    }
59
60
    /**
61
     * Retrieve an array of values optionally keyed by the given key.
62
     *
63
     * @template TPluckValue
64
     *
65
     * @param (callable(self): TPluckValue)|string $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): TPluckValue)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
66
     * @param (callable(self): array-key)|string|null $key
67
     * @return array<array-key, TPluckValue>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, TPluckValue> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, TPluckValue>.
Loading history...
68
     */
69 4
    public static function pluck(callable|string $value, callable|string $key = null): array
70
    {
71 4
        return self::collect()->pluck($value, $key);
72
    }
73
74
    /**
75
     * Retrieve the result of mapping over all the cases.
76
     *
77
     * @template TMapValue
78
     *
79
     * @param callable(self, array-key): TMapValue $callback
80
     * @return array<array-key, TMapValue>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, TMapValue> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, TMapValue>.
Loading history...
81
     */
82 2
    public static function map(callable $callback): array
83
    {
84 2
        return self::collect()->map($callback);
85
    }
86
87
    /**
88
     * Retrieve all the cases keyed by their own name.
89
     *
90
     * @return CasesCollection<array-key, self>
91
     */
92 1
    public static function keyByName(): CasesCollection
93
    {
94 1
        return self::collect()->keyByName();
95
    }
96
97
    /**
98
     * Retrieve all the cases keyed by the given key.
99
     *
100
     * @param (callable(self): array-key)|string $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): array-key)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
101
     * @return CasesCollection<array-key, self>
102
     */
103 2
    public static function keyBy(callable|string $key): CasesCollection
104
    {
105 2
        return self::collect()->keyBy($key);
106
    }
107
108
    /**
109
     * Retrieve all the cases keyed by their own value.
110
     *
111
     * @return CasesCollection<array-key, self>
112
     */
113 1
    public static function keyByValue(): CasesCollection
114
    {
115 1
        return self::collect()->keyByValue();
116
    }
117
118
    /**
119
     * Retrieve all the cases grouped by the given key.
120
     *
121
     * @param (callable(self): array-key)|string $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): array-key)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
122
     * @return CasesCollection<array-key, CasesCollection<array-key, self>>
123
     */
124 4
    public static function groupBy(callable|string $key): CasesCollection
125
    {
126 4
        return self::collect()->groupBy($key);
127
    }
128
129
    /**
130
     * Retrieve only the filtered cases.
131
     *
132
     * @param (callable(self): bool)|string $filter
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): bool)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
133
     * @return CasesCollection<array-key, self>
134
     */
135 3
    public static function filter(callable|string $filter): CasesCollection
136
    {
137 3
        return self::collect()->filter($filter);
138
    }
139
140
    /**
141
     * Retrieve only the cases having the given names.
142
     *
143
     * @return CasesCollection<array-key, self>
144
     */
145 1
    public static function only(string ...$names): CasesCollection
146
    {
147 1
        return self::collect()->only(...$names);
148
    }
149
150
    /**
151
     * Retrieve only the cases not having the given names.
152
     *
153
     * @return CasesCollection<array-key, self>
154
     */
155 1
    public static function except(string ...$names): CasesCollection
156
    {
157 1
        return self::collect()->except(...$names);
158
    }
159
160
    /**
161
     * Retrieve only the cases having the given values.
162
     *
163
     * @return CasesCollection<array-key, self>
164
     */
165 1
    public static function onlyValues(string|int ...$values): CasesCollection
166
    {
167 1
        return self::collect()->onlyValues(...$values);
168
    }
169
170
    /**
171
     * Retrieve only the cases not having the given values.
172
     *
173
     * @return CasesCollection<array-key, self>
174
     */
175 1
    public static function exceptValues(string|int ...$values): CasesCollection
176
    {
177 1
        return self::collect()->exceptValues(...$values);
178
    }
179
180
    /**
181
     * Retrieve all the cases sorted by their own name ascending.
182
     *
183
     * @return CasesCollection<array-key, self>
184
     */
185 1
    public static function sort(): CasesCollection
186
    {
187 1
        return self::collect()->sort();
188
    }
189
190
    /**
191
     * Retrieve all the cases sorted by the given key ascending.
192
     *
193
     * @param (callable(self): mixed)|string $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): mixed)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
194
     * @return CasesCollection<array-key, self>
195
     */
196 2
    public static function sortBy(callable|string $key): CasesCollection
197
    {
198 2
        return self::collect()->sortBy($key);
199
    }
200
201
    /**
202
     * Retrieve all the cases sorted by their own value ascending.
203
     *
204
     * @return CasesCollection<array-key, self>
205
     */
206 1
    public static function sortByValue(): CasesCollection
207
    {
208 1
        return self::collect()->sortByValue();
209
    }
210
211
    /**
212
     * Retrieve all the cases sorted by their own name descending.
213
     *
214
     * @return CasesCollection<array-key, self>
215
     */
216 1
    public static function sortDesc(): CasesCollection
217
    {
218 1
        return self::collect()->sortDesc();
219
    }
220
221
    /**
222
     * Retrieve all the cases sorted by the given key descending.
223
     *
224
     * @param (callable(self): mixed)|string $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(self): mixed)|string at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
225
     * @return CasesCollection<array-key, self>
226
     */
227 2
    public static function sortByDesc(callable|string $key): CasesCollection
228
    {
229 2
        return self::collect()->sortByDesc($key);
230
    }
231
232
    /**
233
     * Retrieve all the cases sorted by their own value descending.
234
     *
235
     * @return CasesCollection<array-key, self>
236
     */
237 1
    public static function sortByDescValue(): CasesCollection
238
    {
239 1
        return self::collect()->sortByDescValue();
240
    }
241
}
242