Feature: Eloquent Builder types Illuminate\Database\Eloquent\Builder has type support Background: Given I have the following config """ """ And I have the following code preamble """ */ public function getNewQuery(): Builder { return User::query(); } /** * @return Builder */ public function getNewModelQuery(): Builder { return (new User())->newModelQuery(); } /** * @param Builder $builder */ public function firstOrFailFromBuilderInstance(Builder $builder): User { return $builder->firstOrFail(); } /** * @param Builder $builder */ public function findOrFailFromBuilderInstance(Builder $builder): User { return $builder->findOrFail(1); } /** * @param Builder $builder * @return Collection */ public function findMultipleOrFailFromBuilderInstance(Builder $builder): Collection { return $builder->findOrFail([1, 2]); } /** * @param Builder $builder */ public function findOne(Builder $builder): ?User { return $builder->find(1); } /** * @param Builder $builder */ public function findViaArray(Builder $builder): Collection { return $builder->find([1]); } /** * @return Builder */ public function getWhereBuilderViaInstance(array $attributes): Builder { return (new User())->where($attributes); } } """ When I run Psalm Then I see no errors Scenario: can call static methods on model Given I have the following code preamble """ */ public function getWhereBuilderViaStatic(array $attributes): Builder { return User::where($attributes); } /** * @psalm-return Collection */ public function getWhereViaStatic(array $attributes): Collection { return User::where($attributes)->get(); } } """ When I run Psalm Then I see no errors Scenario: Unknown Scenario Given I have the following code preamble """ */ public function test_failure(): Builder { return User::fakeQueryMethodThatDoesntExist(); } } """ When I run Psalm Then I see these errors | Type | Message | | MixedInferredReturnType | Could not verify return type 'Illuminate\Database\Eloquent\Builder' for UserRepository::test_failure | | MixedReturnStatement | Could not infer a return type | Scenario: can call methods on underlying query builder Given I have the following code """ /** * @psalm-param Builder $builder * @psalm-return Builder */ function test(Builder $builder): Builder { return $builder->orderBy('id', 'ASC'); } """ When I run Psalm Then I see no errors Scenario: cannot call firstOrNew and firstOrCreate without parameters in Laravel 6.x Given I have the "laravel/framework" package satisfying the "6.*" And I have the following code """ /** * @psalm-param Builder $builder * @psalm-return User */ function test_firstOrCreate(Builder $builder): User { return $builder->firstOrCreate(); } /** * @psalm-param Builder $builder * @psalm-return User */ function test_firstOrNew(Builder $builder): User { return $builder->firstOrNew(); } """ When I run Psalm Then I see these errors | Type | Message | | TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstorcreate saw 0 | | TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstornew saw 0 | Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x Given I have the "laravel/framework" package satisfying the ">= 8.0" And I have the following code """ /** * @psalm-param Builder $builder * @psalm-return User */ function test_firstOrCreate(Builder $builder): User { return $builder->firstOrCreate(); } /** * @psalm-param Builder $builder * @psalm-return User */ function test_firstOrNew(Builder $builder): User { return $builder->firstOrNew(); } """ When I run Psalm Then I see no errors Scenario: can call whereDate with \DateTimeInterface|string|null Given I have the following code """ /** * @psalm-param Builder $builder * @psalm-return Builder */ function test_whereDateWithDateTimeInterface(Builder $builder): Builder { return $builder->whereDate('created_at', '>', new \DateTimeImmutable()); } /** * @psalm-param Builder $builder * @psalm-return Builder */ function test_whereDateWithString(Builder $builder): Builder { return $builder->whereDate('created_at', '>', (new \DateTimeImmutable())->format('d/m/Y')); } /** * @psalm-param Builder $builder * @psalm-return Builder */ function test_whereDateWithNull(Builder $builder): Builder { return $builder->whereDate('created_at', '>', null); } """ When I run Psalm Then I see no errors Scenario: can not call whereDate with incompatible type Given I have the following code """ /** * @psalm-param Builder $builder * @psalm-return Builder */ function test_whereDateWithInt(Builder $builder): Builder { return $builder->whereDate('created_at', '>', 1); } """ When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 3 of Illuminate\Database\Eloquent\Builder::whereDate expects DateTimeInterface\|null\|string, 1 provided | Scenario: can call count on the builder instance Given I have the following code """ /** * @psalm-param Builder $builder * @psalm-return int */ function test_whereDateWithInt(Builder $builder): int { return $builder->count(); } """ When I run Psalm Then I see no errors