99 *
1010 * @template TKey of array-key
1111 * @template TValue
12+ * @template TKeyless
1213 *
1314 * @phpstan-require-implements CollectionInterface
1415 */
@@ -47,6 +48,7 @@ public function add($value)
4748 {
4849 $ items = $ this ->Items ;
4950 $ items [] = $ value ;
51+ /** @var TKeyless */
5052 return $ this ->replaceItems ($ items , true );
5153 }
5254
@@ -95,7 +97,6 @@ public function reverse()
9597 */
9698 public function map (callable $ callback , int $ mode = CollectionInterface::CALLBACK_USE_VALUE )
9799 {
98- $ items = [];
99100 $ prev = null ;
100101 $ item = null ;
101102 $ key = null ;
@@ -116,28 +117,25 @@ public function map(callable $callback, int $mode = CollectionInterface::CALLBAC
116117 }
117118
118119 // @phpstan-ignore argument.type, return.type
119- return $ this ->maybeReplaceItems ($ items , true );
120+ return $ this ->maybeReplaceItems ($ items ?? [] , true );
120121 }
121122
122123 /**
123124 * @inheritDoc
124125 */
125126 public function filter (callable $ callback , int $ mode = CollectionInterface::CALLBACK_USE_VALUE )
126127 {
127- $ items = [];
128128 $ prev = null ;
129129 $ item = null ;
130130 $ key = null ;
131131 $ value = null ;
132132
133133 foreach ($ this ->Items as $ nextKey => $ nextValue ) {
134134 $ next = $ this ->getCallbackValue ($ mode , $ nextKey , $ nextValue );
135- if ($ item !== null ) {
136- if ($ callback ($ item , $ next , $ prev )) {
137- /** @var TKey $key */
138- /** @var TValue $value */
139- $ items [$ key ] = $ value ;
140- }
135+ if ($ item !== null && $ callback ($ item , $ next , $ prev )) {
136+ /** @var TKey $key */
137+ /** @var TValue $value */
138+ $ items [$ key ] = $ value ;
141139 }
142140 $ prev = $ item ;
143141 $ item = $ next ;
@@ -150,7 +148,7 @@ public function filter(callable $callback, int $mode = CollectionInterface::CALL
150148 $ items [$ key ] = $ value ;
151149 }
152150
153- return $ this ->maybeReplaceItems ($ items );
151+ return $ this ->maybeReplaceItems ($ items ?? [] );
154152 }
155153
156154 /**
@@ -204,10 +202,12 @@ public function slice(int $offset, ?int $length = null)
204202 public function push (...$ items )
205203 {
206204 if (!$ items ) {
205+ /** @var TKeyless */
207206 return $ this ;
208207 }
209208 $ _items = $ this ->Items ;
210209 array_push ($ _items , ...$ items );
210+ /** @var TKeyless */
211211 return $ this ->replaceItems ($ _items , true );
212212 }
213213
@@ -246,15 +246,15 @@ public function shift(&$first = null)
246246 public function unshift (...$ items )
247247 {
248248 if (!$ items ) {
249+ /** @var TKeyless */
249250 return $ this ;
250251 }
251252 $ _items = $ this ->Items ;
252253 array_unshift ($ _items , ...$ items );
254+ /** @var TKeyless */
253255 return $ this ->replaceItems ($ _items , true );
254256 }
255257
256- // Partial implementation of `ArrayAccess`:
257-
258258 /**
259259 * @param TKey|null $offset
260260 * @param TValue $value
@@ -281,8 +281,6 @@ public function offsetUnset($offset): void
281281 $ this ->maybeReplaceItems ($ items , false , false );
282282 }
283283
284- // --
285-
286284 /**
287285 * @param array<TKey,TValue> $items
288286 * @return static
0 commit comments