-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamically determine return type for
Collection::reject
(#2107)
- Loading branch information
Showing
6 changed files
with
85 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace CollectionReject; | ||
|
||
use App\Account; | ||
use App\User; | ||
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | ||
use Illuminate\Support\Collection as SupportCollection; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function convertToAccount(User $user): ?Account | ||
{ } | ||
|
||
function dummyReject($value) | ||
{ | ||
if ($value instanceof User) { | ||
return true; | ||
} | ||
|
||
return random_int(0, 1) > 1; | ||
} | ||
|
||
/** @param EloquentCollection<int, User> $users */ | ||
function test(User $user, SupportCollection $users): void | ||
{ | ||
assertType("Illuminate\Support\Collection<(int|string), 0|0.0|''|'0'|array{}|false|null>", collect()->reject()); | ||
|
||
assertType("Illuminate\Support\Collection<int, ''|'0'|null>", collect(['foo', null, '', 'bar', null])->reject()); | ||
|
||
assertType('Illuminate\Support\Collection<int, int<min, 2>>', collect([1, 2, 3, 4, 5, 6])->reject(function (int $value) { | ||
return $value > 2; | ||
})); | ||
assertType('Illuminate\Support\Collection<int, int<min, 2>>', collect([1, 2, 3, 4, 5, 6])->reject(fn (int $value) => $value > 2)); | ||
|
||
assertType("Illuminate\Database\Eloquent\Collection<int, App\User>", $users->reject(function (User $user): bool { | ||
return ! $user->blocked; | ||
})); | ||
assertType("Illuminate\Database\Eloquent\Collection<int, App\User>", $users->reject(fn (User $user) => ! $user->blocked)); | ||
|
||
assertType( | ||
'Illuminate\Support\Collection<int, null>', | ||
collect($users->all()) | ||
->map(function (User $attachment): ?Account { | ||
return convertToAccount($attachment); | ||
}) | ||
->reject() | ||
); | ||
|
||
$accounts = $user->accounts()->active()->get(); | ||
assertType('App\AccountCollection<int, App\Account>', $accounts); | ||
|
||
assertType('App\AccountCollection<int, App\Account>', $accounts->reject(function ($account) { | ||
return \CollectionStubs\dummyReject($account); | ||
})); | ||
|
||
$accounts->reject(function ($account) { | ||
return dummyReject($account); | ||
}) | ||
->map(function ($account) { | ||
assertType('App\Account', $account); | ||
}); | ||
} |