3
3
namespace Drenso \Shared \Helper ;
4
4
5
5
use Doctrine \Common \Collections \ReadableCollection ;
6
+ use Drenso \Shared \Exception \NullGuard \IdRequiredException ;
6
7
use Drenso \Shared \Interfaces \IdInterface ;
7
8
use InvalidArgumentException ;
8
9
9
10
class ArrayHelper
10
11
{
11
- /** Verify that all array elements are of the supplied type. */
12
+ /**
13
+ * Verify that all array elements are of the supplied type.
14
+ *
15
+ * @template T
16
+ *
17
+ * @param 'int'|'integer'|'string'|'float'|'bool'|'boolean'|'array'|class-string<T> $type
18
+ *
19
+ * @phpstan-assert T[] $variables
20
+ */
12
21
public static function assertType (array $ variables , string $ type ): void
13
22
{
14
23
match ($ type ) {
@@ -21,7 +30,11 @@ public static function assertType(array $variables, string $type): void
21
30
};
22
31
}
23
32
24
- /** Verify that all array elements are integers. */
33
+ /**
34
+ * Verify that all array elements are integers.
35
+ *
36
+ * @phpstan-assert int[] $variables
37
+ */
25
38
public static function assertInt (array $ variables ): void
26
39
{
27
40
foreach ($ variables as $ variable ) {
@@ -32,7 +45,11 @@ public static function assertInt(array $variables): void
32
45
}
33
46
}
34
47
35
- /** Verify that all array elements are strings. */
48
+ /**
49
+ * Verify that all array elements are strings.
50
+ *
51
+ * @phpstan-assert string[] $variables
52
+ */
36
53
public static function assertString (array $ variables ): void
37
54
{
38
55
foreach ($ variables as $ variable ) {
@@ -43,7 +60,11 @@ public static function assertString(array $variables): void
43
60
}
44
61
}
45
62
46
- /** Verify that all array elements are floats. */
63
+ /**
64
+ * Verify that all array elements are floats.
65
+ *
66
+ * @phpstan-assert float[] $variables
67
+ */
47
68
public static function assertFloat (array $ variables ): void
48
69
{
49
70
foreach ($ variables as $ variable ) {
@@ -54,7 +75,11 @@ public static function assertFloat(array $variables): void
54
75
}
55
76
}
56
77
57
- /** Verify that all array elements are booleans. */
78
+ /**
79
+ * Verify that all array elements are booleans.
80
+ *
81
+ * @phpstan-assert bool[] $variables
82
+ */
58
83
public static function assertBool (array $ variables ): void
59
84
{
60
85
foreach ($ variables as $ variable ) {
@@ -65,7 +90,11 @@ public static function assertBool(array $variables): void
65
90
}
66
91
}
67
92
68
- /** Verify that all array elements are arrays. */
93
+ /**
94
+ * Verify that all array elements are arrays.
95
+ *
96
+ * @phpstan-assert array<array> $variables
97
+ */
69
98
public static function assertArray (array $ variables ): void
70
99
{
71
100
foreach ($ variables as $ variable ) {
@@ -76,7 +105,15 @@ public static function assertArray(array $variables): void
76
105
}
77
106
}
78
107
79
- /** Verify that all array elements are objects of the supplied class. */
108
+ /**
109
+ * Verify that all array elements are objects of the supplied class.
110
+ *
111
+ * @template T
112
+ *
113
+ * @param class-string<T> $class
114
+ *
115
+ * @phpstan-assert T[] $objects
116
+ */
80
117
public static function assertClass (array $ objects , string $ class ): void
81
118
{
82
119
foreach ($ objects as $ object ) {
@@ -101,7 +138,7 @@ public static function mapById(array $objects): array
101
138
{
102
139
$ result = [];
103
140
foreach ($ objects as $ object ) {
104
- $ result [$ object ->getId ()] = $ object ;
141
+ $ result [$ object ->getId () ?? throw new IdRequiredException () ] = $ object ;
105
142
}
106
143
107
144
return $ result ;
@@ -117,7 +154,7 @@ public static function mapById(array $objects): array
117
154
public static function mapToId (array $ objects ): array
118
155
{
119
156
return array_values (array_map (
120
- fn ($ object ): ? int => $ object ->getId (),
157
+ fn ($ object ): int => $ object ->getId () ?? throw new IdRequiredException (),
121
158
$ objects
122
159
));
123
160
}
@@ -153,7 +190,7 @@ public static function filterEmptyValuesFromStringArray(array $data): array
153
190
*
154
191
* @template T
155
192
*
156
- * @param array<T>|ReadableCollection<T> $arrayOrCollection
193
+ * @param array<T>|ReadableCollection<int, T> $arrayOrCollection
157
194
*
158
195
* @return array<T>
159
196
*/
0 commit comments