23
23
/**
24
24
* Abstract immutable sequence with basic functional methods.
25
25
*
26
- * @template TKey of array-key
27
26
* @template TValue
27
+ * @template TKey of array-key
28
28
*
29
29
* @extends AbstractCypherObject<TKey, TValue>
30
30
*
31
31
* @psalm-immutable
32
+ *
33
+ * @psalm-suppress UnsafeInstantiation
32
34
*/
33
35
abstract class AbstractCypherSequence extends AbstractCypherObject implements Countable
34
36
{
35
37
/** @var array<TKey, TValue> */
36
- protected array $ sequence ;
38
+ protected array $ sequence = [] ;
37
39
38
40
/**
39
- * Creates a new instance from the given iterable.
40
- *
41
- * @template Value
42
- *
43
- * @param iterable<Value> $iterable
44
- *
45
- * @return static
46
- *
47
- * @pure
41
+ * @param iterable<TKey, TValue> $iterable
48
42
*/
49
- abstract public static function fromIterable (iterable $ iterable ): self ;
43
+ abstract public function __construct (iterable $ iterable );
50
44
51
45
final public function count (): int
52
46
{
@@ -56,14 +50,14 @@ final public function count(): int
56
50
/**
57
51
* Copies the sequence.
58
52
*
59
- * @return static
53
+ * @return static<TValue, TKey>
60
54
*/
61
55
final public function copy (): self
62
56
{
63
57
// Make sure the sequence is actually copied by reassigning it.
64
58
$ map = $ this ->sequence ;
65
59
66
- return $ this :: fromIterable ($ map );
60
+ return new static ($ map );
67
61
}
68
62
69
63
/**
@@ -89,7 +83,7 @@ final public function toArray(): array
89
83
*
90
84
* @param iterable<array-key, TValue> $values
91
85
*
92
- * @return static
86
+ * @return static<TValue, TKey>
93
87
*/
94
88
abstract public function merge (iterable $ values ): self ;
95
89
@@ -116,48 +110,50 @@ final public function hasValue($value): bool
116
110
/**
117
111
* Creates a filtered the sequence with the provided callback.
118
112
*
119
- * @param pure- callable(TValue, TKey):bool $callback
113
+ * @param callable(TValue, TKey):bool $callback
120
114
*
121
- * @return static
115
+ * @return static<TValue, TKey>
122
116
*/
123
117
final public function filter (callable $ callback ): self
124
118
{
119
+ /** @var array<TKey, TValue> $tbr */
125
120
$ tbr = [];
126
121
foreach ($ this ->sequence as $ key => $ value ) {
127
122
if ($ callback ($ value , $ key )) {
128
123
$ tbr [$ key ] = $ value ;
129
124
}
130
125
}
131
126
132
- return $ this :: fromIterable ($ tbr );
127
+ return new static ($ tbr );
133
128
}
134
129
135
130
/**
136
131
* Maps the values of this sequence to a new one with the provided callback.
137
132
*
138
- * @template U
133
+ * @template ReturnType
139
134
*
140
- * @param pure- callable(TValue, TKey):U $callback
135
+ * @param callable(TValue, TKey):ReturnType $callback
141
136
*
142
- * @return static
137
+ * @return static<ReturnType, TKey>
143
138
*/
144
139
final public function map (callable $ callback ): self
145
140
{
141
+ /** @var array<TKey, ReturnType> $tbr */
146
142
$ tbr = [];
147
143
foreach ($ this ->sequence as $ key => $ value ) {
148
144
$ tbr [$ key ] = $ callback ($ value , $ key );
149
145
}
150
146
151
- return $ this :: fromIterable ($ tbr );
147
+ return new static ($ tbr );
152
148
}
153
149
154
150
/**
155
151
* Reduces this sequence with the given callback.
156
152
*
157
153
* @template TInitial
158
154
*
159
- * @param pure- callable(TInitial|null, TValue, TKey):TInitial $callback
160
- * @param TInitial|null $initial
155
+ * @param callable(TInitial|null, TValue, TKey):TInitial $callback
156
+ * @param TInitial|null $initial
161
157
*
162
158
* @return TInitial
163
159
*/
@@ -185,24 +181,24 @@ final public function find($value)
185
181
/**
186
182
* Creates a reversed sequence.
187
183
*
188
- * @return static
184
+ * @return static<TValue, TKey>
189
185
*/
190
186
abstract public function reversed (): self ;
191
187
192
188
/**
193
189
* Slices a new sequence starting from the given offset with a certain length.
194
190
* If the length is null it will slice the entire remainder starting from the offset.
195
191
*
196
- * @return static
192
+ * @return static<TValue, TKey>
197
193
*/
198
194
abstract public function slice (int $ offset , int $ length = null ): self ;
199
195
200
196
/**
201
197
* Creates a sorted sequence. If the compoarator is null it will use natural ordering.
202
198
*
203
- * @param (pure- callable(TValue, TValue):int)|null $comparator
199
+ * @param (callable(TValue, TValue):int)|null $comparator
204
200
*
205
- * @return static
201
+ * @return static<TValue, TKey>
206
202
*/
207
203
abstract public function sorted (?callable $ comparator = null ): self ;
208
204
0 commit comments