psalm / psalm-plugin-laravel

A Psalm plugin for Laravel
MIT License
307 stars 72 forks source link

InvalidReturnStatement #233

Closed mshamaseen closed 1 year ago

mshamaseen commented 2 years ago

Describe the bug psalm returns InvalidReturnType on:

public function notifications(): MorphMany
    {
        return $this->morphMany(Notification::class, 'notifiable')
            ->latest();
    }

the full error is:

ERROR: InvalidReturnType - Modules/Admin/Auth/Models/Admin.php:126:38 - The declared return type 'Illuminate\Database\Eloquent\Relations\MorphMany' for Modules\Admin\Auth\Models\Admin::notifications is incorrect, got 'Illuminate\Database\Eloquent\Builder<TRelatedModel:Illuminate\Database\Eloquent\Relations\MorphMany as Illuminate\Database\Eloquent\Model>' (see https://psalm.dev/011)

Impacted Versions

barryvdh/laravel-debugbar             v3.6.7    PHP Debugbar integration for Laravel
barryvdh/laravel-ide-helper           v2.12.3   Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.
fruitcake/laravel-cors                v2.2.0    Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
laravel/framework                     v9.8.1    The Laravel Framework.
laravel/sail                          v1.13.9   Docker files for running a basic Laravel application.
laravel/sanctum                       v2.15.1   Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.
laravel/serializable-closure          v1.1.1    Laravel Serializable Closure provides an easy and secure way to serialize closures in PHP.
laravel/telescope                     v4.8.3    An elegant debug assistant for the Laravel framework.
laravel/tinker                        v2.7.2    Powerful REPL for the Laravel framework.
laravel/ui                            v3.4.5    Laravel UI utilities and presets.
psalm/plugin-laravel                  v2.0.1    A Laravel plugin for Psalm
shamaseen/laravel-files-generator     0.5.1     Generate any kind of File/s from stubs with only a single command, literally, ANY TYPE.
shamaseen/laravel-repositories        1.1.2     repository pattern files generator
spatie/laravel-backup                 8.1.2     A Laravel package to backup your application
spatie/laravel-ignition               1.2.2     A beautiful error page for Laravel applications.
spatie/laravel-package-tools          1.11.3    Tools for creating Laravel packages
spatie/laravel-permission             5.5.2     Permission handling for Laravel 6.0 and up
spatie/laravel-ray                    1.29.7    Easily debug Laravel apps
spatie/laravel-signal-aware-command   1.2.0     Handle signals in artisan commands
vimeo/psalm                           4.22.0    A static analysis tool for finding errors in PHP a
alies-dev commented 1 year ago

UPD: a new error (v2.5.0) is like:

ERROR: InvalidReturnStatement - app/Models/User.php:49:16 - The inferred type 'Illuminate\Database\Eloquent\Builder<TRelatedModel:Illuminate\Database\Eloquent\Relations\MorphMany as Illuminate\Database\Eloquent\Model>' does not match the declared return type 'Illuminate\Database\Eloquent\Relations\MorphMany' for App\Models\User::notifications (see https://psalm.dev/128)
        return $this->morphMany(Notification::class, 'notifiable')
            ->latest();