psalm-plugin-laravel
psalm-plugin-laravel copied to clipboard
InvalidArgumentException Could not get class storage for eloquent
Describe the bug
InvalidArgumentException Could not get class storage for eloquent
Emitted in /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Provider/ClassLikeStorageProvider.php:45
Stack trace in the forked worker:
#0 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/ClassAnalyzer.php(783): Psalm\Internal\Provider\ClassLikeStorageProvider->get()
#1 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/ClassAnalyzer.php(416): Psalm\Internal\Analyzer\ClassAnalyzer::addContextProperties()
#2 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/FileAnalyzer.php(203): Psalm\Internal\Analyzer\ClassAnalyzer->analyze()
#3 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Analyzer.php(1581): Psalm\Internal\Analyzer\FileAnalyzer->analyze()
#4 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Fork/Pool.php(209): Psalm\Internal\Codebase\Analyzer->analysisWorker()
#5 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Analyzer.php(373): Psalm\Internal\Fork\Pool->__construct()
#6 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Analyzer.php(272): Psalm\Internal\Codebase\Analyzer->doAnalysis()
#7 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php(651): Psalm\Internal\Codebase\Analyzer->analyzeFiles()
#8 /var/www/vendor/vimeo/psalm/src/Psalm/Internal/Cli/Psalm.php(380): Psalm\Internal\Analyzer\ProjectAnalyzer->check()
#9 /var/www/vendor/vimeo/psalm/psalm(9): Psalm\Internal\Cli\Psalm::run()
#10 /var/www/vendor/bin/psalm(120): include('...')
#11 {main}
at vendor/vimeo/psalm/src/Psalm/Internal/Fork/Pool.php:397
393▕ * @psalm-suppress MixedArgument
394▕ */
395▕ posix_kill($child_pid, SIGTERM);
396▕ }
➜ 397▕ throw new Exception($message->message);
398▕ } else {
399▕ error_log('Child should return ForkMessage - response type=' . gettype($message));
400▕ $this->did_have_error = true;
401▕ }
Impacted Versions
php -v
PHP 8.1.14 (cli) (built: Jan 13 2023 10:43:50) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.14, Copyright (c) Zend Technologies
with Zend OPcache v8.1.14, Copyright (c), by Zend Technologies
./vendor/bin/psalm -v
Psalm 5.4.0@62db5d4f6a7ae0a20f7cc5a4952d730272fc0863
composer show | grep -E 'psalm|laravel'
barryvdh/laravel-debugbar v3.7.0 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.
eusonlito/laravel-meta v3.2.3 A package to manage Header Meta Tags
eusonlito/laravel-packer v2.2.6 A package for pack css and javascript files
laravel/framework v9.48.0 The Laravel Framework.
laravel/helpers v1.6.0 Provides backwards compatibility for helpers in the latest Laravel release.
laravel/pint v1.4.0 An opinionated code formatter for PHP.
laravel/serializable-closure v1.2.2 Laravel Serializable Closure provides an easy and secure way to serialize closures in PHP.
psalm/plugin-laravel v2.2.0 A Laravel plugin for Psalm
sentry/sentry-laravel 3.1.3 Laravel SDK for Sentry (https://sentry.io)
spatie/laravel-ignition 1.6.4 A beautiful error page for Laravel applications.
spatie/laravel-ray 1.32.0 Easily debug Laravel apps
vimeo/psalm 5.4.0 A static analysis tool for finding errors in PHP applications
Additional context
Error is not always in same file with --debug-by-line
parameter.
Steps
- Clone https://github.com/eusonlito/GPS-Tracker.git
- Install composer packages
composer install
- Launch
./vendor/bin/psalm
@eusonlito
Thanks for reporting!
Do you have ide-helper files locally when you run psalm?
No, I don't use IDE helpers.
@eusonlito
From what I found, the issue caused by vendor/psalm/plugin-laravel/cache/models.stubphp
file (available after running psalm). Previously this file was empty, but we fixed it in https://github.com/psalm/psalm-plugin-laravel/releases/tag/v2.1.1, so the latest working version for you will be 2.1.0, but it has an issue of not detecting Model's properties (this is what we have fixed in 2.1.1).
This is all what I know ATM. I'm working on this issue
I've always been confused why ide-helper uses an alias of Model
named Eloquent
(src: https://github.com/barryvdh/laravel-ide-helper/blob/f055d42b3917bf067b8316f27ce538245574cc84/src/Console/ModelsCommand.php#L1011 ). I think that might be the root cause of this.
Perhaps we may need to manually alias Eloquent
to Model
for psalm to know?
I was able to get psalm to run on your project when manually adding this line to your config/app.php
file
'aliases' => [
...
'Eloquent' => Illuminate\Database\Eloquent\Model::class,
],
We should fix this internally, however we have a few options:
- Always add the
Eloquent
alias at runtime in psalm plugin - Make a change upstream in
ide-helper
to reference the actualIlluminate\Database\Eloquent\Model::class
rather than using theEloquent
alias - one day... remove our reliance on ide-helper entirely
https://github.com/barryvdh/laravel-ide-helper/issues/517 for reference
Maybe the first option would be the easiest to implement at this moment. It would solve this problem with minimal changes. Then you could ask the IDE for the necessary changes to correctly resolve the class :)
yep! Are you up for a PR @eusonlito ?
I think off the top of my head, you may be able to adjust the app aliases at runtime here? https://github.com/psalm/psalm-plugin-laravel/blob/master/src/Providers/ApplicationProvider.php#L112-L121
edit: errr, actually that would only work for plugins. I think you may be able to adjust the config in ApplicationProvider::getApp
...
@mr-feek it's fine? https://github.com/psalm/psalm-plugin-laravel/pull/273/files