Skip to content

[9.x] Improve Collection's callable default return types #39805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions .github/workflows/databases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,49 +132,49 @@ jobs:
DB_CONNECTION: mysql
DB_USERNAME: root

pgsql:
runs-on: ubuntu-20.04

services:
postgresql:
image: postgres:14
env:
POSTGRES_DB: forge
POSTGRES_USER: forge
POSTGRES_PASSWORD: password
ports:
- 5432:5432
options: --health-cmd=pg_isready --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
fail-fast: true

name: PostgreSQL 14

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_pgsql
tools: composer:v2
coverage: none

- name: Install dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit tests/Integration/Database --verbose
env:
DB_CONNECTION: pgsql
DB_PASSWORD: password
# pgsql:
# runs-on: ubuntu-20.04

# services:
# postgresql:
# image: postgres:14
# env:
# POSTGRES_DB: forge
# POSTGRES_USER: forge
# POSTGRES_PASSWORD: password
# ports:
# - 5432:5432
# options: --health-cmd=pg_isready --health-interval=10s --health-timeout=5s --health-retries=3

# strategy:
# fail-fast: true

# name: PostgreSQL 14

# steps:
# - name: Checkout code
# uses: actions/checkout@v2

# - name: Setup PHP
# uses: shivammathur/setup-php@v2
# with:
# php-version: 8.1
# extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_pgsql
# tools: composer:v2
# coverage: none

# - name: Install dependencies
# uses: nick-invision/retry@v1
# with:
# timeout_minutes: 5
# max_attempts: 5
# command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress

# - name: Execute tests
# run: vendor/bin/phpunit tests/Integration/Database --verbose
# env:
# DB_CONNECTION: pgsql
# DB_PASSWORD: password

mssql:
runs-on: ubuntu-20.04
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function filter(callable $callback = null)
* @template TFirstDefault
*
* @param (callable(TValue, TKey): bool)|null $callback
* @param TFirstDefault $default
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
* @return TValue|TFirstDefault
*/
public function first(callable $callback = null, $default = null)
Expand Down Expand Up @@ -417,7 +417,7 @@ public function forget($keys)
* @template TGetDefault
*
* @param TKey $key
* @param TGetDefault $default
* @param TGetDefault|(\Closure(): TGetDefault) $default
* @return TValue|TGetDefault
*/
public function get($key, $default = null)
Expand Down Expand Up @@ -650,7 +650,7 @@ public function keys()
* @template TLastDefault
*
* @param (callable(TValue, TKey): bool)|null $callback
* @param TLastDefault $default
* @param TLastDefault|(\Closure(): TLastDefault) $default
* @return TValue|TLastDefault
*/
public function last(callable $callback = null, $default = null)
Expand Down Expand Up @@ -915,7 +915,7 @@ public function concat($source)
* @template TPullDefault
*
* @param TKey $key
* @param TPullDefault $default
* @param TPullDefault|(\Closure(): TPullDefault) $default
* @return TValue|TPullDefault
*/
public function pull($key, $default = null)
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public function whereInstanceOf($type);
* @template TFirstDefault
*
* @param (callable(TValue,TKey): bool)|null $callback
* @param TFirstDefault $default
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
* @return TValue|TFirstDefault
*/
public function first(callable $callback = null, $default = null);
Expand Down Expand Up @@ -495,7 +495,7 @@ public function flip();
* @template TGetDefault
*
* @param TKey $key
* @param TGetDefault $default
* @param TGetDefault|(\Closure(): TGetDefault) $default
* @return TValue|TGetDefault
*/
public function get($key, $default = null);
Expand Down Expand Up @@ -586,7 +586,7 @@ public function keys();
* @template TLastDefault
*
* @param (callable(TValue, TKey): bool)|null $callback
* @param TLastDefault $default
* @param TLastDefault|(\Closure(): TLastDefault) $default
* @return TValue|TLastDefault
*/
public function last(callable $callback = null, $default = null);
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public function filter(callable $callback = null)
* @template TFirstDefault
*
* @param (callable(TValue): bool)|null $callback
* @param TFirstDefault $default
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
* @return TValue|TFirstDefault
*/
public function first(callable $callback = null, $default = null)
Expand Down Expand Up @@ -471,7 +471,7 @@ public function flip()
* @template TGetDefault
*
* @param TKey|null $key
* @param TGetDefault $default
* @param TGetDefault|(\Closure(): TGetDefault) $default
* @return TValue|TGetDefault
*/
public function get($key, $default = null)
Expand Down Expand Up @@ -649,7 +649,7 @@ public function keys()
* @template TLastDefault
*
* @param (callable(TValue, TKey): bool)|null $callback
* @param TLastDefault $default
* @param TLastDefault|(\Closure(): TLastDefault) $default
* @return TValue|TLastDefault
*/
public function last(callable $callback = null, $default = null)
Expand Down
12 changes: 12 additions & 0 deletions types/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@

return false;
}, 'string'));
assertType('string|User', $collection->first(null, function () {
return 'string';
}));

assertType('Illuminate\Support\Collection<int, mixed>', $collection->flatten());
assertType('Illuminate\Support\Collection<int, mixed>', $collection::make(['string' => 'string'])->flatten(4));
Expand Down Expand Up @@ -412,6 +415,9 @@
assertType('string|User', $collection->last(function () {
return true;
}, 'string'));
assertType('string|User', $collection->last(null, function () {
return 'string';
}));

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

assertType('User|null', $collection->get(0));
assertType('string|User', $collection->get(0, 'string'));
assertType('string|User', $collection->get(0, function () {
return 'string';
}));

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

assertType('User|null', $collection->pull(1));
assertType('string|User', $collection->pull(1, 'string'));
assertType('string|User', $collection->pull(1, function () {
return 'string';
}));

assertType('Illuminate\Support\Collection<int, User>', $collection->put(1, new User));
assertType('Illuminate\Support\Collection<string, string>', $collection::make([
Expand Down
9 changes: 9 additions & 0 deletions types/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@

return false;
}, 'string'));
assertType('string|User', $collection->first(null, function () {
return 'string';
}));

assertType('Illuminate\Support\LazyCollection<int, mixed>', $collection->flatten());
assertType('Illuminate\Support\LazyCollection<int, mixed>', $collection::make(['string' => 'string'])->flatten(4));
Expand Down Expand Up @@ -412,6 +415,9 @@
assertType('string|User', $collection->last(function () {
return true;
}, 'string'));
assertType('string|User', $collection->last(null, function () {
return 'string';
}));

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

assertType('User|null', $collection->get(0));
assertType('string|User', $collection->get(0, 'string'));
assertType('string|User', $collection->get(0, function () {
return 'string';
}));

assertType(
'Illuminate\Support\LazyCollection<int, Illuminate\Support\LazyCollection<int, User>>',
Expand Down