Skip to content

Commit ee5efbc

Browse files
[9.x] Improve Collection's callable default return types (#39805)
* improve collection callable default return types * change callable to closure * fix enumerable closure default return types * Disable PostgreSQL build for now * improve collection callable default return types * change callable to closure * fix enumerable closure default return types Co-authored-by: Dries Vints <dries@vints.io>
1 parent de98bd0 commit ee5efbc

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function filter(callable $callback = null)
367367
* @template TFirstDefault
368368
*
369369
* @param (callable(TValue, TKey): bool)|null $callback
370-
* @param TFirstDefault $default
370+
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
371371
* @return TValue|TFirstDefault
372372
*/
373373
public function first(callable $callback = null, $default = null)
@@ -417,7 +417,7 @@ public function forget($keys)
417417
* @template TGetDefault
418418
*
419419
* @param TKey $key
420-
* @param TGetDefault $default
420+
* @param TGetDefault|(\Closure(): TGetDefault) $default
421421
* @return TValue|TGetDefault
422422
*/
423423
public function get($key, $default = null)
@@ -650,7 +650,7 @@ public function keys()
650650
* @template TLastDefault
651651
*
652652
* @param (callable(TValue, TKey): bool)|null $callback
653-
* @param TLastDefault $default
653+
* @param TLastDefault|(\Closure(): TLastDefault) $default
654654
* @return TValue|TLastDefault
655655
*/
656656
public function last(callable $callback = null, $default = null)
@@ -915,7 +915,7 @@ public function concat($source)
915915
* @template TPullDefault
916916
*
917917
* @param TKey $key
918-
* @param TPullDefault $default
918+
* @param TPullDefault|(\Closure(): TPullDefault) $default
919919
* @return TValue|TPullDefault
920920
*/
921921
public function pull($key, $default = null)

src/Illuminate/Collections/Enumerable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public function whereInstanceOf($type);
459459
* @template TFirstDefault
460460
*
461461
* @param (callable(TValue,TKey): bool)|null $callback
462-
* @param TFirstDefault $default
462+
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
463463
* @return TValue|TFirstDefault
464464
*/
465465
public function first(callable $callback = null, $default = null);
@@ -495,7 +495,7 @@ public function flip();
495495
* @template TGetDefault
496496
*
497497
* @param TKey $key
498-
* @param TGetDefault $default
498+
* @param TGetDefault|(\Closure(): TGetDefault) $default
499499
* @return TValue|TGetDefault
500500
*/
501501
public function get($key, $default = null);
@@ -586,7 +586,7 @@ public function keys();
586586
* @template TLastDefault
587587
*
588588
* @param (callable(TValue, TKey): bool)|null $callback
589-
* @param TLastDefault $default
589+
* @param TLastDefault|(\Closure(): TLastDefault) $default
590590
* @return TValue|TLastDefault
591591
*/
592592
public function last(callable $callback = null, $default = null);

src/Illuminate/Collections/LazyCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public function filter(callable $callback = null)
404404
* @template TFirstDefault
405405
*
406406
* @param (callable(TValue): bool)|null $callback
407-
* @param TFirstDefault $default
407+
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
408408
* @return TValue|TFirstDefault
409409
*/
410410
public function first(callable $callback = null, $default = null)
@@ -471,7 +471,7 @@ public function flip()
471471
* @template TGetDefault
472472
*
473473
* @param TKey|null $key
474-
* @param TGetDefault $default
474+
* @param TGetDefault|(\Closure(): TGetDefault) $default
475475
* @return TValue|TGetDefault
476476
*/
477477
public function get($key, $default = null)
@@ -649,7 +649,7 @@ public function keys()
649649
* @template TLastDefault
650650
*
651651
* @param (callable(TValue, TKey): bool)|null $callback
652-
* @param TLastDefault $default
652+
* @param TLastDefault|(\Closure(): TLastDefault) $default
653653
* @return TValue|TLastDefault
654654
*/
655655
public function last(callable $callback = null, $default = null)

types/Support/Collection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@
364364

365365
return false;
366366
}, 'string'));
367+
assertType('string|User', $collection->first(null, function () {
368+
return 'string';
369+
}));
367370

368371
assertType('Illuminate\Support\Collection<int, mixed>', $collection->flatten());
369372
assertType('Illuminate\Support\Collection<int, mixed>', $collection::make(['string' => 'string'])->flatten(4));
@@ -412,6 +415,9 @@
412415
assertType('string|User', $collection->last(function () {
413416
return true;
414417
}, 'string'));
418+
assertType('string|User', $collection->last(null, function () {
419+
return 'string';
420+
}));
415421

416422
assertType('Illuminate\Support\Collection<int, int>', $collection->map(function () {
417423
return 1;
@@ -772,6 +778,9 @@
772778

773779
assertType('User|null', $collection->get(0));
774780
assertType('string|User', $collection->get(0, 'string'));
781+
assertType('string|User', $collection->get(0, function () {
782+
return 'string';
783+
}));
775784

776785
assertType('Illuminate\Support\Collection<int, User>', $collection->forget(1));
777786
assertType('Illuminate\Support\Collection<int, User>', $collection->forget([1, 2]));
@@ -790,6 +799,9 @@
790799

791800
assertType('User|null', $collection->pull(1));
792801
assertType('string|User', $collection->pull(1, 'string'));
802+
assertType('string|User', $collection->pull(1, function () {
803+
return 'string';
804+
}));
793805

794806
assertType('Illuminate\Support\Collection<int, User>', $collection->put(1, new User));
795807
assertType('Illuminate\Support\Collection<string, string>', $collection::make([

types/Support/LazyCollection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@
364364

365365
return false;
366366
}, 'string'));
367+
assertType('string|User', $collection->first(null, function () {
368+
return 'string';
369+
}));
367370

368371
assertType('Illuminate\Support\LazyCollection<int, mixed>', $collection->flatten());
369372
assertType('Illuminate\Support\LazyCollection<int, mixed>', $collection::make(['string' => 'string'])->flatten(4));
@@ -412,6 +415,9 @@
412415
assertType('string|User', $collection->last(function () {
413416
return true;
414417
}, 'string'));
418+
assertType('string|User', $collection->last(null, function () {
419+
return 'string';
420+
}));
415421

416422
assertType('Illuminate\Support\LazyCollection<int, int>', $collection->map(function () {
417423
return 1;
@@ -779,6 +785,9 @@
779785

780786
assertType('User|null', $collection->get(0));
781787
assertType('string|User', $collection->get(0, 'string'));
788+
assertType('string|User', $collection->get(0, function () {
789+
return 'string';
790+
}));
782791

783792
assertType(
784793
'Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<int, User>>',

0 commit comments

Comments
 (0)