diff --git a/composer.json b/composer.json index f39a7a8229a3..43135fb59aa2 100644 --- a/composer.json +++ b/composer.json @@ -113,7 +113,7 @@ "orchestra/testbench-core": "^9.6", "pda/pheanstalk": "^5.0.6", "php-http/discovery": "^1.15", - "phpstan/phpstan": "^1.11.5", + "phpstan/phpstan": "^2.0", "phpunit/phpunit": "^10.5.35|^11.3.6", "predis/predis": "^2.3", "resend/resend-php": "^0.10.0", diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 14d17f9925db..dbb872c2c39d 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1873,7 +1873,7 @@ protected function normalizeCastClassResponse($key, $value) /** * Get all of the current attributes on the model. * - * @return array + * @return array */ public function getAttributes() { diff --git a/src/Illuminate/Process/PendingProcess.php b/src/Illuminate/Process/PendingProcess.php index 668385460ac2..1a252a848f3f 100644 --- a/src/Illuminate/Process/PendingProcess.php +++ b/src/Illuminate/Process/PendingProcess.php @@ -246,9 +246,8 @@ public function run(array|string|null $command = null, ?callable $output = null) { $this->command = $command ?: $this->command; + $process = $this->toSymfonyProcess($command); try { - $process = $this->toSymfonyProcess($command); - if ($fake = $this->fakeFor($command = $process->getCommandline())) { return tap($this->resolveSynchronousFake($command, $fake), function ($result) { $this->factory->recordIfRecording($this, $result); diff --git a/types/Cache/Repository.php b/types/Cache/Repository.php index 87c21550df45..ac80b6a194f1 100644 --- a/types/Cache/Repository.php +++ b/types/Cache/Repository.php @@ -18,12 +18,12 @@ assertType('mixed', $cache->pull('cache', function (): int { return 30; })); -assertType('int', $cache->sear('cache', function (): int { +assertType('33', $cache->sear('cache', function (): int { return 33; })); -assertType('int', $cache->remember('cache', now(), function (): int { +assertType('36', $cache->remember('cache', now(), function (): int { return 36; })); -assertType('int', $cache->rememberForever('cache', function (): int { +assertType('36', $cache->rememberForever('cache', function (): int { return 36; })); diff --git a/types/Collections/helpers.php b/types/Collections/helpers.php index ddfc56ad1656..36aa16362046 100644 --- a/types/Collections/helpers.php +++ b/types/Collections/helpers.php @@ -2,10 +2,10 @@ use function PHPStan\Testing\assertType; -assertType('string', value('foo', 42)); -assertType('int', value(fn () => 42)); -assertType('int', value(function ($foo) { - assertType('bool', $foo); +assertType("'foo'", value('foo', 42)); +assertType('42', value(fn () => 42)); +assertType('42', value(function ($foo) { + assertType('true', $foo); return 42; }, true)); diff --git a/types/Contracts/Cache/Repository.php b/types/Contracts/Cache/Repository.php index 0fa9bbedaf02..fee8862c78f3 100644 --- a/types/Contracts/Cache/Repository.php +++ b/types/Contracts/Cache/Repository.php @@ -14,16 +14,16 @@ })); assertType('mixed', $cache->pull('key')); -assertType('int', $cache->pull('cache', 28)); -assertType('int', $cache->pull('cache', function (): int { +assertType('28', $cache->pull('cache', 28)); +assertType('30', $cache->pull('cache', function (): int { return 30; })); -assertType('int', $cache->sear('cache', function (): int { +assertType('33', $cache->sear('cache', function (): int { return 33; })); -assertType('int', $cache->remember('cache', now(), function (): int { +assertType('36', $cache->remember('cache', now(), function (): int { return 36; })); -assertType('int', $cache->rememberForever('cache', function (): int { +assertType('36', $cache->rememberForever('cache', function (): int { return 36; })); diff --git a/types/Contracts/Validation/ValidationRule.php b/types/Contracts/Validation/ValidationRule.php index 565a7d972340..4682bac952a8 100644 --- a/types/Contracts/Validation/ValidationRule.php +++ b/types/Contracts/Validation/ValidationRule.php @@ -4,7 +4,7 @@ use function PHPStan\Testing\assertType; -new class implements ValidationRule +$class = new class implements ValidationRule { public function validate(string $attribute, mixed $value, Closure $fail): void { diff --git a/types/Database/Eloquent/Builder.php b/types/Database/Eloquent/Builder.php index ba46642414eb..857228e0c675 100644 --- a/types/Database/Eloquent/Builder.php +++ b/types/Database/Eloquent/Builder.php @@ -46,11 +46,11 @@ function test( assertType('Illuminate\Database\Eloquent\Collection', $query->findOr([1], callback: fn () => 42)); assertType('User', $query->findOrFail(1)); assertType('User|null', $query->find(1)); - assertType('int|User', $query->findOr(1, fn () => 42)); - assertType('int|User', $query->findOr(1, callback: fn () => 42)); + assertType('42|User', $query->findOr(1, fn () => 42)); + assertType('42|User', $query->findOr(1, callback: fn () => 42)); assertType('User|null', $query->first()); - assertType('int|User', $query->firstOr(fn () => 42)); - assertType('int|User', $query->firstOr(callback: fn () => 42)); + assertType('42|User', $query->firstOr(fn () => 42)); + assertType('42|User', $query->firstOr(callback: fn () => 42)); assertType('User', $query->firstOrNew(['id' => 1])); assertType('User', $query->findOrNew(1)); assertType('User', $query->firstOrCreate(['id' => 1])); diff --git a/types/Database/Eloquent/Collection.php b/types/Database/Eloquent/Collection.php index e95003a304fd..385d63d88d82 100644 --- a/types/Database/Eloquent/Collection.php +++ b/types/Database/Eloquent/Collection.php @@ -6,7 +6,7 @@ assertType('Illuminate\Database\Eloquent\Collection', $collection); assertType('User|null', $collection->find(1)); -assertType('string|User', $collection->find(1, 'string')); +assertType("'string'|User", $collection->find(1, 'string')); assertType('Illuminate\Database\Eloquent\Collection', $collection->find([1])); assertType('Illuminate\Database\Eloquent\Collection', $collection->load('string')); diff --git a/types/Database/Eloquent/Relations.php b/types/Database/Eloquent/Relations.php index 34575aa73ed8..8494b8aea1e8 100644 --- a/types/Database/Eloquent/Relations.php +++ b/types/Database/Eloquent/Relations.php @@ -46,16 +46,16 @@ function test(User $user, Post $post, Comment $comment, ChildUser $child): void assertType('Illuminate\Database\Eloquent\Collection', $user->roles()->findMany([1, 2, 3])); assertType('Illuminate\Database\Eloquent\Collection', $user->roles()->findOrNew([1])); assertType('Illuminate\Database\Eloquent\Collection', $user->roles()->findOrFail([1])); - assertType('Illuminate\Database\Eloquent\Collection|int', $user->roles()->findOr([1], fn () => 42)); - assertType('Illuminate\Database\Eloquent\Collection|int', $user->roles()->findOr([1], callback: fn () => 42)); + assertType('42|Illuminate\Database\Eloquent\Collection', $user->roles()->findOr([1], fn () => 42)); + assertType('42|Illuminate\Database\Eloquent\Collection', $user->roles()->findOr([1], callback: fn () => 42)); assertType('Illuminate\Types\Relations\Role', $user->roles()->findOrNew(1)); assertType('Illuminate\Types\Relations\Role', $user->roles()->findOrFail(1)); assertType('Illuminate\Types\Relations\Role|null', $user->roles()->find(1)); - assertType('Illuminate\Types\Relations\Role|int', $user->roles()->findOr(1, fn () => 42)); - assertType('Illuminate\Types\Relations\Role|int', $user->roles()->findOr(1, callback: fn () => 42)); + assertType('42|Illuminate\Types\Relations\Role', $user->roles()->findOr(1, fn () => 42)); + assertType('42|Illuminate\Types\Relations\Role', $user->roles()->findOr(1, callback: fn () => 42)); assertType('Illuminate\Types\Relations\Role|null', $user->roles()->first()); - assertType('Illuminate\Types\Relations\Role|int', $user->roles()->firstOr(fn () => 42)); - assertType('Illuminate\Types\Relations\Role|int', $user->roles()->firstOr(callback: fn () => 42)); + assertType('42|Illuminate\Types\Relations\Role', $user->roles()->firstOr(fn () => 42)); + assertType('42|Illuminate\Types\Relations\Role', $user->roles()->firstOr(callback: fn () => 42)); assertType('Illuminate\Types\Relations\Role|null', $user->roles()->firstWhere('foo')); assertType('Illuminate\Types\Relations\Role', $user->roles()->firstOrNew()); assertType('Illuminate\Types\Relations\Role', $user->roles()->firstOrFail()); @@ -81,14 +81,14 @@ function test(User $user, Post $post, Comment $comment, ChildUser $child): void assertType('Illuminate\Database\Eloquent\Relations\HasOneThrough', $user->car()); assertType('Illuminate\Types\Relations\Car|null', $user->car()->getResults()); assertType('Illuminate\Database\Eloquent\Collection', $user->car()->find([1])); - assertType('Illuminate\Database\Eloquent\Collection|int', $user->car()->findOr([1], fn () => 42)); - assertType('Illuminate\Database\Eloquent\Collection|int', $user->car()->findOr([1], callback: fn () => 42)); + assertType('42|Illuminate\Database\Eloquent\Collection', $user->car()->findOr([1], fn () => 42)); + assertType('42|Illuminate\Database\Eloquent\Collection', $user->car()->findOr([1], callback: fn () => 42)); assertType('Illuminate\Types\Relations\Car|null', $user->car()->find(1)); - assertType('Illuminate\Types\Relations\Car|int', $user->car()->findOr(1, fn () => 42)); - assertType('Illuminate\Types\Relations\Car|int', $user->car()->findOr(1, callback: fn () => 42)); + assertType('42|Illuminate\Types\Relations\Car', $user->car()->findOr(1, fn () => 42)); + assertType('42|Illuminate\Types\Relations\Car', $user->car()->findOr(1, callback: fn () => 42)); assertType('Illuminate\Types\Relations\Car|null', $user->car()->first()); - assertType('Illuminate\Types\Relations\Car|int', $user->car()->firstOr(fn () => 42)); - assertType('Illuminate\Types\Relations\Car|int', $user->car()->firstOr(callback: fn () => 42)); + assertType('42|Illuminate\Types\Relations\Car', $user->car()->firstOr(fn () => 42)); + assertType('42|Illuminate\Types\Relations\Car', $user->car()->firstOr(callback: fn () => 42)); assertType('Illuminate\Support\LazyCollection', $user->car()->lazy()); assertType('Illuminate\Support\LazyCollection', $user->car()->lazyById()); assertType('Illuminate\Support\LazyCollection', $user->car()->cursor()); diff --git a/types/Database/Query/Builder.php b/types/Database/Query/Builder.php index 55ae1c28edc0..0be765cc416e 100644 --- a/types/Database/Query/Builder.php +++ b/types/Database/Query/Builder.php @@ -12,8 +12,8 @@ function test(Builder $query, EloquentBuilder $userQuery): void { assertType('object|null', $query->first()); assertType('object|null', $query->find(1)); - assertType('int|object', $query->findOr(1, fn () => 42)); - assertType('int|object', $query->findOr(1, callback: fn () => 42)); + assertType('42|object', $query->findOr(1, fn () => 42)); + assertType('42|object', $query->findOr(1, callback: fn () => 42)); assertType('Illuminate\Database\Query\Builder', $query->selectSub($userQuery, 'alias')); assertType('Illuminate\Database\Query\Builder', $query->fromSub($userQuery, 'alias')); assertType('Illuminate\Database\Query\Builder', $query->from($userQuery, 'alias')); diff --git a/types/Foundation/Configuration/Middleware.php b/types/Foundation/Configuration/Middleware.php index 0bb34c69ed69..efeb538650cc 100644 --- a/types/Foundation/Configuration/Middleware.php +++ b/types/Foundation/Configuration/Middleware.php @@ -6,13 +6,13 @@ $middleware = new Middleware(); $middleware->convertEmptyStringsToNull(except: [ - fn ($request) => $request->has('skip-all-1'), - fn ($request) => $request->has('skip-all-2'), + fn (Request $request): bool => $request->has('skip-all-1'), + fn (Request $request): bool => $request->has('skip-all-2'), ]); $middleware->trimStrings(except: [ 'aaa', - fn ($request) => $request->has('skip-all'), + fn (Request $request): bool => $request->has('skip-all'), ]); $middleware->trustProxies(at: '*'); diff --git a/types/Foundation/Helpers.php b/types/Foundation/Helpers.php index d3317b6fd15e..54d39b905c1a 100644 --- a/types/Foundation/Helpers.php +++ b/types/Foundation/Helpers.php @@ -35,9 +35,9 @@ assertType('Illuminate\Log\LogManager', logs()); assertType('Psr\Log\LoggerInterface', logs('foo')); -assertType('int|null', rescue(fn () => 123)); -assertType('int', rescue(fn () => 123, 345)); -assertType('int', rescue(fn () => 123, fn () => 345)); +assertType('123|null', rescue(fn () => 123)); +assertType('123|345', rescue(fn () => 123, 345)); +assertType('123|345', rescue(fn () => 123, fn () => 345)); assertType('Illuminate\Routing\Redirector', redirect()); assertType('Illuminate\Http\RedirectResponse', redirect('foo')); diff --git a/types/Support/Arr.php b/types/Support/Arr.php index b0bbed63a0fd..72de39eee4d5 100644 --- a/types/Support/Arr.php +++ b/types/Support/Arr.php @@ -8,7 +8,7 @@ /** @var iterable $iterable */ $iterable = []; /** @var Traversable $traversable */ -$traversable = []; +$traversable = new ArrayIterator([new User]); assertType('User|null', Arr::first($array)); assertType('User|null', Arr::first($array, function ($user) { @@ -16,12 +16,12 @@ return true; })); -assertType('string|User', Arr::first($array, function ($user) { +assertType("'string'|User", Arr::first($array, function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', Arr::first($array, null, function () { +assertType("'string'|User", Arr::first($array, null, function () { return 'string'; })); @@ -31,12 +31,12 @@ return true; })); -assertType('string|User', Arr::first($iterable, function ($user) { +assertType("'string'|User", Arr::first($iterable, function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', Arr::first($iterable, null, function () { +assertType("'string'|User", Arr::first($iterable, null, function () { return 'string'; })); @@ -46,12 +46,12 @@ return true; })); -assertType('string|User', Arr::first($traversable, function ($user) { +assertType("'string'|User", Arr::first($traversable, function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', Arr::first($traversable, null, function () { +assertType("'string'|User", Arr::first($traversable, null, function () { return 'string'; })); @@ -61,12 +61,12 @@ return true; })); -assertType('string|User', Arr::last($array, function ($user) { +assertType("'string'|User", Arr::last($array, function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', Arr::last($array, null, function () { +assertType("'string'|User", Arr::last($array, null, function () { return 'string'; })); @@ -76,12 +76,12 @@ return true; })); -assertType('string|User', Arr::last($iterable, function ($user) { +assertType("'string'|User", Arr::last($iterable, function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', Arr::last($iterable, null, function () { +assertType("'string'|User", Arr::last($iterable, null, function () { return 'string'; })); @@ -91,11 +91,11 @@ return true; })); -assertType('string|User', Arr::last($traversable, function ($user) { +assertType("'string'|User", Arr::last($traversable, function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', Arr::last($traversable, null, function () { +assertType("'string'|User", Arr::last($traversable, null, function () { return 'string'; })); diff --git a/types/Support/Collection.php b/types/Support/Collection.php index 30d13f3943d7..e5ee7727db9d 100644 --- a/types/Support/Collection.php +++ b/types/Support/Collection.php @@ -5,13 +5,21 @@ use function PHPStan\Testing\assertType; +/** @implements Arrayable */ +class Users implements Arrayable +{ + public function toArray(): array + { + return [new User()]; + } +} + $collection = collect([new User]); -/** @var Arrayable $arrayable */ -$arrayable = []; +$arrayable = new Users; /** @var iterable $iterable */ -$iterable = []; +$iterable = [1]; /** @var Traversable $traversable */ -$traversable = []; +$traversable = new ArrayIterator(['string']); class Invokable { @@ -62,7 +70,7 @@ public function __invoke(): string assertType('Illuminate\Support\Collection<(int|string), string>', $collection->wrap(['string'])); assertType('Illuminate\Support\Collection<(int|string), User>', $collection->wrap(['string' => new User])); -assertType('array', $collection->unwrap(['string'])); +assertType("array<0, 'string'>", $collection->unwrap(['string'])); assertType('array', $collection->unwrap( $collection )); @@ -259,7 +267,7 @@ public function __invoke(): string return true; })); -assertType('bool|Illuminate\Support\Collection', $collection->when(true, function ($collection) { +assertType('Illuminate\Support\Collection|true', $collection->when(true, function ($collection) { assertType('Illuminate\Support\Collection', $collection); return true; @@ -267,14 +275,14 @@ public function __invoke(): string assertType('Illuminate\Support\Collection|null', $collection->when(true, function ($collection) { assertType('Illuminate\Support\Collection', $collection); })); -assertType('Illuminate\Support\Collection|string', $collection->when(true, function ($collection) { +assertType("'string'|Illuminate\Support\Collection", $collection->when(true, function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 'string'; })); assertType('Illuminate\Support\Collection|null', $collection->when('Taylor', function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); })); assertType( 'Illuminate\Support\Collection|null', @@ -282,17 +290,17 @@ public function __invoke(): string 'Taylor', function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); }, function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); } ) ); assertType('Illuminate\Support\Collection|null', $collection->when(fn () => 'Taylor', function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); })); assertType( 'Illuminate\Support\Collection|null', @@ -304,11 +312,11 @@ function ($collection) { }, function ($collection, $count) { assertType('Illuminate\Support\Collection', $collection); - assertType('int', $count); + assertType('14', $count); }, function ($collection, $count) { assertType('Illuminate\Support\Collection', $collection); - assertType('int', $count); + assertType('14', $count); } ) ); @@ -318,7 +326,7 @@ function ($collection, $count) { assertType('Invokable', $param); })); -assertType('bool|Illuminate\Support\Collection', $collection->whenEmpty(function ($collection) { +assertType('Illuminate\Support\Collection|true', $collection->whenEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return true; @@ -326,13 +334,13 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection|null', $collection->whenEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); })); -assertType('Illuminate\Support\Collection|string', $collection->whenEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\Collection", $collection->whenEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\Collection', $collection->whenNotEmpty(function ($collection) { +assertType('Illuminate\Support\Collection|true', $collection->whenNotEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return true; @@ -340,13 +348,13 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection|null', $collection->whenNotEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); })); -assertType('Illuminate\Support\Collection|string', $collection->whenNotEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\Collection", $collection->whenNotEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\Collection', $collection->unless(true, function ($collection) { +assertType('Illuminate\Support\Collection|true', $collection->unless(true, function ($collection) { assertType('Illuminate\Support\Collection', $collection); return true; @@ -354,14 +362,14 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection|null', $collection->unless(true, function ($collection) { assertType('Illuminate\Support\Collection', $collection); })); -assertType('Illuminate\Support\Collection|string', $collection->unless(true, function ($collection) { +assertType("'string'|Illuminate\Support\Collection", $collection->unless(true, function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 'string'; })); assertType('Illuminate\Support\Collection|null', $collection->unless('Taylor', function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); })); assertType( 'Illuminate\Support\Collection|null', @@ -369,17 +377,17 @@ function ($collection, $count) { 'Taylor', function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); }, function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); } ) ); assertType('Illuminate\Support\Collection|null', $collection->unless(fn () => 'Taylor', function ($collection, $name) { assertType('Illuminate\Support\Collection', $collection); - assertType('string', $name); + assertType("'Taylor'", $name); })); assertType( 'Illuminate\Support\Collection|null', @@ -391,11 +399,11 @@ function ($collection) { }, function ($collection, $count) { assertType('Illuminate\Support\Collection', $collection); - assertType('int', $count); + assertType('14', $count); }, function ($collection, $count) { assertType('Illuminate\Support\Collection', $collection); - assertType('int', $count); + assertType('14', $count); } ) ); @@ -405,7 +413,7 @@ function ($collection, $count) { assertType('Invokable', $param); })); -assertType('bool|Illuminate\Support\Collection', $collection->unlessEmpty(function ($collection) { +assertType('Illuminate\Support\Collection|true', $collection->unlessEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return true; @@ -413,13 +421,13 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection|null', $collection->unlessEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); })); -assertType('Illuminate\Support\Collection|string', $collection->unlessEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\Collection", $collection->unlessEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\Collection', $collection->unlessNotEmpty(function ($collection) { +assertType('Illuminate\Support\Collection|true', $collection->unlessNotEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return true; @@ -427,7 +435,7 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection|null', $collection->unlessNotEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); })); -assertType('Illuminate\Support\Collection|string', $collection->unlessNotEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\Collection", $collection->unlessNotEmpty(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 'string'; @@ -479,27 +487,27 @@ function ($collection, $count) { return true; })); -assertType('string|User', $collection->first(function ($user) { +assertType("'string'|User", $collection->first(function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', $collection->first(null, function () { +assertType("'string'|User", $collection->first(null, function () { return 'string'; })); if ($collection->isNotEmpty()) { assertType('User', $collection->first()); - assertType('string|User', $collection->first(null, 'foo')); + assertType("'foo'|User", $collection->first(null, 'foo')); } else { assertType('null', $collection->first()); - assertType('string|User', $collection->first(null, 'foo')); + assertType("'foo'|User", $collection->first(null, 'foo')); } if ($collection->isEmpty()) { assertType('null', $collection->first()); - assertType('string|User', $collection->first(null, 'foo')); + assertType("'foo'|User", $collection->first(null, 'foo')); } else { assertType('User', $collection->first()); - assertType('string|User', $collection->first(null, 'foo')); + assertType("'foo'|User", $collection->first(null, 'foo')); } assertType('Illuminate\Support\Collection', $collection->flatten()); @@ -509,8 +517,8 @@ function ($collection, $count) { assertType('User|null', $collection->firstWhere('string', 'string', 'string')); assertType('User|null', $collection->value('string')); -assertType('string|User', $collection->value('string', 'string')); -assertType('string|User', $collection->value('string', fn () => 'string')); +assertType("'string'|User", $collection->value('string', 'string')); +assertType("'string'|User", $collection->value('string', fn () => 'string')); assertType('Illuminate\Support\Collection', $collection::make(['string'])->flip()); @@ -558,10 +566,10 @@ function ($collection, $count) { return true; })); -assertType('string|User', $collection->last(function () { +assertType("'string'|User", $collection->last(function () { return true; }, 'string')); -assertType('string|User', $collection->last(null, function () { +assertType("'string'|User", $collection->last(null, function () { return 'string'; })); @@ -684,47 +692,47 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection|int', $collection->make([1])->random(2)); assertType('Illuminate\Support\Collection|string', $collection->make(['string'])->random()); -assertType('int', $collection +assertType('1', $collection ->reduce(function ($null, $user) { assertType('User', $user); - assertType('int|null', $null); + assertType('1|null', $null); return 1; })); -assertType('int', $collection +assertType('1', $collection ->reduce(function ($int, $user) { assertType('User', $user); - assertType('int', $int); + assertType('0|1', $int); return 1; }, 0)); -assertType('int', $collection +assertType('1', $collection ->reduce(function ($int, $user, $key) { assertType('User', $user); - assertType('int', $int); + assertType('0|1', $int); assertType('int', $key); return 1; }, 0)); -assertType('int', $collection +assertType('1', $collection ->reduceWithKeys(function ($null, $user) { assertType('User', $user); - assertType('int|null', $null); + assertType('1|null', $null); return 1; })); -assertType('int', $collection +assertType('1', $collection ->reduceWithKeys(function ($int, $user) { assertType('User', $user); - assertType('int', $int); + assertType('0|1', $int); return 1; }, 0)); -assertType('int', $collection +assertType('1', $collection ->reduceWithKeys(function ($int, $user, $key) { assertType('User', $user); - assertType('int', $int); + assertType('0|1', $int); assertType('int', $key); return 1; @@ -897,7 +905,7 @@ function ($collection, $count) { return collect([1]); })); -assertType('int', $collection->make([1])->pipe(function ($collection) { +assertType('1', $collection->make([1])->pipe(function ($collection) { assertType('Illuminate\Support\Collection', $collection); return 1; @@ -974,13 +982,13 @@ function ($collection, $count) { assertType('array', $collection->all()); assertType('User|null', $collection->get(0)); -assertType('string|User', $collection->get(0, 'string')); -assertType('string|User', $collection->get(0, function () { +assertType("'string'|User", $collection->get(0, 'string')); +assertType("'string'|User", $collection->get(0, function () { return 'string'; })); -assertType('string|User', $collection->getOrPut(0, 'string')); -assertType('string|User', $collection->getOrPut(0, fn () => 'string')); +assertType("'string'|User", $collection->getOrPut(0, 'string')); +assertType("'string'|User", $collection->getOrPut(0, fn () => 'string')); assertType('Illuminate\Support\Collection', $collection->forget(1)); assertType('Illuminate\Support\Collection', $collection->forget([1, 2])); @@ -998,8 +1006,8 @@ function ($collection, $count) { assertType('Illuminate\Support\Collection', $collection->push(new User, new User)); assertType('User|null', $collection->pull(1)); -assertType('string|User', $collection->pull(1, 'string')); -assertType('string|User', $collection->pull(1, function () { +assertType("'string'|User", $collection->pull(1, 'string')); +assertType("'string'|User", $collection->pull(1, function () { return 'string'; })); diff --git a/types/Support/Fluent.php b/types/Support/Fluent.php index 92d223961091..02a6a81afb0f 100644 --- a/types/Support/Fluent.php +++ b/types/Support/Fluent.php @@ -6,17 +6,17 @@ $fluent = new Fluent(['name' => 'Taylor', 'age' => 25, 'user' => new User]); -assertType('Illuminate\Support\Fluent', $fluent); +assertType("Illuminate\Support\Fluent", $fluent); assertType('Illuminate\Support\Fluent', new Fluent(['name' => 'Taylor'])); assertType('Illuminate\Support\Fluent', new Fluent(['age' => 25])); assertType('Illuminate\Support\Fluent', new Fluent(['user' => new User])); -assertType('int|string|User|null', $fluent['name']); -assertType('int|string|User|null', $fluent['age']); -assertType('int|string|User|null', $fluent['age']); -assertType('int|string|User|null', $fluent->get('name')); -assertType('int|string|User|null', $fluent->get('foobar')); -assertType('int|string|User', $fluent->get('foobar', 'zonda')); -assertType('array', $fluent->getAttributes()); -assertType('array', $fluent->toArray()); -assertType('array', $fluent->jsonSerialize()); +assertType("25|'Taylor'|User|null", $fluent['name']); +assertType("25|'Taylor'|User|null", $fluent['age']); +assertType("25|'Taylor'|User|null", $fluent['age']); +assertType("25|'Taylor'|User|null", $fluent->get('name')); +assertType("25|'Taylor'|User|null", $fluent->get('foobar')); +assertType("25|'Taylor'|'zonda'|User", $fluent->get('foobar', 'zonda')); +assertType("array", $fluent->getAttributes()); +assertType("array", $fluent->toArray()); +assertType("array", $fluent->jsonSerialize()); diff --git a/types/Support/Helpers.php b/types/Support/Helpers.php index ad3ff56aa1fe..123cd3f8778e 100644 --- a/types/Support/Helpers.php +++ b/types/Support/Helpers.php @@ -19,19 +19,19 @@ assertType('User', object_get(new User(), '')); assertType('mixed', object_get(new User(), 'name')); -assertType('int', once(fn () => 1)); +assertType('1', once(fn () => 1)); assertType('null', once(function () { /** @phpstan-ignore function.void (testing void) */ })); assertType('Illuminate\Support\Optional', optional()); assertType('null', optional(null, fn () => 1)); -assertType('int', optional('foo', function ($value) { - assertType('string', $value); +assertType('1', optional('foo', function ($value) { + assertType("'foo'", $value); return 1; })); -assertType('int', retry(5, fn () => 1)); +assertType('1', retry(5, fn () => 1)); assertType('object', str()); assertType('Illuminate\Support\Stringable', str('foo')); @@ -44,7 +44,7 @@ function testThrowIf(float|int $foo, ?DateTime $bar = null): void { assertType('never', throw_if(true, Exception::class)); - assertType('bool', throw_if(false, Exception::class)); + assertType('false', throw_if(false, Exception::class)); assertType('false', throw_if(empty($foo))); throw_if(is_float($foo)); assertType('int', $foo); @@ -55,13 +55,13 @@ function testThrowIf(float|int $foo, ?DateTime $bar = null): void throw_if($bar); assertType('null', $bar); assertType('null', throw_if(null, Exception::class)); - assertType('string', throw_if('', Exception::class)); + assertType("''", throw_if('', Exception::class)); assertType('never', throw_if('foo', Exception::class)); } function testThrowUnless(float|int $foo, ?DateTime $bar = null): void { - assertType('bool', throw_unless(true, Exception::class)); + assertType('true', throw_unless(true, Exception::class)); assertType('never', throw_unless(false, Exception::class)); assertType('true', throw_unless(empty($foo))); throw_unless(is_int($foo)); @@ -74,18 +74,18 @@ function testThrowUnless(float|int $foo, ?DateTime $bar = null): void // Truthy/falsey argument assertType('never', throw_unless(null, Exception::class)); assertType('never', throw_unless('', Exception::class)); - assertType('string', throw_unless('foo', Exception::class)); + assertType("'foo'", throw_unless('foo', Exception::class)); } -assertType('int', transform('filled', fn () => 1, true)); -assertType('int', transform(['filled'], fn () => 1)); +assertType('1', transform('filled', fn () => 1, true)); +assertType('1', transform(['filled'], fn () => 1)); assertType('null', transform('', fn () => 1)); -assertType('bool', transform('', fn () => 1, true)); -assertType('bool', transform('', fn () => 1, fn () => true)); +assertType('true', transform('', fn () => 1, true)); +assertType('true', transform('', fn () => 1, fn () => true)); assertType('User', with(new User())); assertType('bool', with(new User())->save()); -assertType('int', with(new User(), function ($user) { +assertType('10', with(new User(), function ($user) { assertType('User', $user); return 10; diff --git a/types/Support/LazyCollection.php b/types/Support/LazyCollection.php index eabbc28984b5..d7cb75a6c8cb 100644 --- a/types/Support/LazyCollection.php +++ b/types/Support/LazyCollection.php @@ -6,21 +6,28 @@ use function PHPStan\Testing\assertType; +/** @implements Arrayable */ +class Users implements Arrayable +{ + public function toArray(): array + { + return [new User()]; + } +} + $collection = new LazyCollection([new User]); -/** @var Arrayable $arrayable */ -$arrayable = []; +$arrayable = new Users(); /** @var iterable $iterable */ -$iterable = []; +$iterable = [1]; /** @var Traversable $traversable */ -$traversable = []; -/** @var Closure(): Generator $generator */ +$traversable = new ArrayIterator(['string']); $generator = function () { yield new User(); }; assertType('Illuminate\Support\LazyCollection', $collection); -assertType('Illuminate\Support\LazyCollection', new LazyCollection(['string'])); +assertType("Illuminate\Support\LazyCollection", new LazyCollection(['string'])); assertType('Illuminate\Support\LazyCollection', new LazyCollection(['string' => new User])); assertType('Illuminate\Support\LazyCollection', new LazyCollection($arrayable)); assertType('Illuminate\Support\LazyCollection', new LazyCollection($iterable)); @@ -56,7 +63,7 @@ assertType('Illuminate\Support\LazyCollection<(int|string), string>', $collection->wrap(['string'])); assertType('Illuminate\Support\LazyCollection<(int|string), User>', $collection->wrap(['string' => new User])); -assertType('array', $collection->unwrap(['string'])); +assertType("array<0, 'string'>", $collection->unwrap(['string'])); assertType('array', $collection->unwrap( $collection )); @@ -246,7 +253,7 @@ return true; })); -assertType('bool|Illuminate\Support\LazyCollection', $collection->when(true, function ($collection) { +assertType('Illuminate\Support\LazyCollection|true', $collection->when(true, function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return true; @@ -254,13 +261,13 @@ assertType('Illuminate\Support\LazyCollection|null', $collection->when(true, function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection|string', $collection->when(true, function ($collection) { +assertType("'string'|Illuminate\Support\LazyCollection", $collection->when(true, function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\LazyCollection', $collection->whenEmpty(function ($collection) { +assertType('Illuminate\Support\LazyCollection|true', $collection->whenEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return true; @@ -268,13 +275,13 @@ assertType('Illuminate\Support\LazyCollection|null', $collection->whenEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection|string', $collection->whenEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\LazyCollection", $collection->whenEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\LazyCollection', $collection->whenNotEmpty(function ($collection) { +assertType('Illuminate\Support\LazyCollection|true', $collection->whenNotEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return true; @@ -282,13 +289,13 @@ assertType('Illuminate\Support\LazyCollection|null', $collection->whenNotEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection|string', $collection->whenNotEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\LazyCollection", $collection->whenNotEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\LazyCollection', $collection->unless(true, function ($collection) { +assertType('Illuminate\Support\LazyCollection|true', $collection->unless(true, function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return true; @@ -296,13 +303,13 @@ assertType('Illuminate\Support\LazyCollection|null', $collection->unless(true, function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection|string', $collection->unless(true, function ($collection) { +assertType("'string'|Illuminate\Support\LazyCollection", $collection->unless(true, function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\LazyCollection', $collection->unlessEmpty(function ($collection) { +assertType('Illuminate\Support\LazyCollection|true', $collection->unlessEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return true; @@ -310,13 +317,13 @@ assertType('Illuminate\Support\LazyCollection|null', $collection->unlessEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection|string', $collection->unlessEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\LazyCollection", $collection->unlessEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 'string'; })); -assertType('bool|Illuminate\Support\LazyCollection', $collection->unlessNotEmpty(function ($collection) { +assertType('Illuminate\Support\LazyCollection|true', $collection->unlessNotEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return true; @@ -324,7 +331,7 @@ assertType('Illuminate\Support\LazyCollection|null', $collection->unlessNotEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection|string', $collection->unlessNotEmpty(function ($collection) { +assertType("'string'|Illuminate\Support\LazyCollection", $collection->unlessNotEmpty(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 'string'; @@ -376,12 +383,12 @@ return true; })); -assertType('string|User', $collection->first(function ($user) { +assertType("'string'|User", $collection->first(function ($user) { assertType('User', $user); return false; }, 'string')); -assertType('string|User', $collection->first(null, function () { +assertType("'string'|User", $collection->first(null, function () { return 'string'; })); @@ -436,10 +443,10 @@ return true; })); -assertType('string|User', $collection->last(function () { +assertType("'string'|User", $collection->last(function () { return true; }, 'string')); -assertType('string|User', $collection->last(null, function () { +assertType("'string'|User", $collection->last(null, function () { return 'string'; })); @@ -560,17 +567,17 @@ assertType('Illuminate\Support\LazyCollection|int', $collection->make([1])->random(2)); assertType('Illuminate\Support\LazyCollection|string', $collection->make(['string'])->random()); -assertType('int', $collection +assertType('1', $collection ->reduce(function ($null, $user) { assertType('User', $user); - assertType('int|null', $null); + assertType('1|null', $null); return 1; })); -assertType('int', $collection +assertType('1', $collection ->reduce(function ($int, $user) { assertType('User', $user); - assertType('int', $int); + assertType('0|1', $int); return 1; }, 0)); @@ -739,12 +746,12 @@ assertType('Illuminate\Support\LazyCollection', $collection); })); -assertType('Illuminate\Support\LazyCollection', $collection->pipe(function ($collection) { +assertType('Illuminate\Support\LazyCollection', $collection->pipe(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return new LazyCollection([1]); })); -assertType('int', $collection->make([1])->pipe(function ($collection) { +assertType('1', $collection->make([1])->pipe(function ($collection) { assertType('Illuminate\Support\LazyCollection', $collection); return 1; @@ -818,8 +825,8 @@ assertType('array', $collection->all()); assertType('User|null', $collection->get(0)); -assertType('string|User', $collection->get(0, 'string')); -assertType('string|User', $collection->get(0, function () { +assertType("'string'|User", $collection->get(0, 'string')); +assertType("'string'|User", $collection->get(0, function () { return 'string'; })); diff --git a/types/Support/Timebox.php b/types/Support/Timebox.php index 39c94f896722..4cb7ae1090c2 100644 --- a/types/Support/Timebox.php +++ b/types/Support/Timebox.php @@ -4,7 +4,7 @@ use function PHPStan\Testing\assertType; -assertType('int', (new Timebox)->call(function ($timebox) { +assertType('1', (new Timebox)->call(function ($timebox) { assertType('Illuminate\Support\Timebox', $timebox); return 1;