In Laravel's code are dynamic Facade classes, which are resolved at runtime https://laravel.com/docs/5.3/facades#how-facades-work
When I run phpstan on newly created laravel project, so I get this errors:
E:\htdocs\grumphp-test (master)
λ vendor\bin\phpstan.bat analyse -l 5 app
19/19 [============================] 100%
------ ---------------------------------------------------------------------------------------------------------------------------------------
Line app\Exceptions\Handler.php
------ ---------------------------------------------------------------------------------------------------------------------------------------
47 Method App\Exceptions\Handler::render() should return Illuminate\Http\Response but returns Symfony\Component\HttpFoundation\Response.
------ ---------------------------------------------------------------------------------------------------------------------------------------
------ ----------------------------------------------------------------------------------
Line app\Http\Controllers\Auth\RegisterController.php
------ ----------------------------------------------------------------------------------
50 Call to an undefined static method Illuminate\Support\Facades\Validator::make().
65 Call to an undefined static method App\User::create().
------ ----------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------
Line app\Http\Middleware\RedirectIfAuthenticated.php
------ ------------------------------------------------------------------------------
20 Call to an undefined static method Illuminate\Support\Facades\Auth::guard().
------ ------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------------
Line app\Providers\BroadcastServiceProvider.php
------ ------------------------------------------------------------------------------------
17 Call to an undefined static method Illuminate\Support\Facades\Broadcast::routes().
------ ------------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------------
Line app\Providers\RouteServiceProvider.php
------ ------------------------------------------------------------------------------------
54 Call to an undefined static method Illuminate\Support\Facades\Route::middleware().
68 Call to an undefined static method Illuminate\Support\Facades\Route::prefix().
------ ------------------------------------------------------------------------------------
[ERROR] Found 7 errors
Laravel's support is in the pipeline, it's possible to create it with class reflection extensions in PHPStan. But I don't think it should be an open issue here because it's not a bug in PHPStan per se.
Meanwhile, try to use Laravel's IDE Helper, some developers used it to compensate for magic Laravel methods: https://github.com/barryvdh/laravel-ide-helper
May be, we didn't understood a little, I didn't mean to add some support to phpstan itself, but to create something like phpstan/phpstan-doctrine, so if I start new laravel project I can install phpstan/phpstan-laravel and I will be ready to go, so 0 errors.
In phpstan/phpstan-laravel could be only slightly updated phpstan configuration, where will be ignored this rule for now PHPStan\Rules\Methods\CallStaticMethodsRule.
laravel-ide-helper isn't really right response for my problem ;)
Yep, feel free to create such Laravel extension, I will be happy to make it an official one if it adheres to some coding standards, has tests etc. Or you can support the development of PHPStan on Patreon and I will be able to dedicate more time to develop PHPStan, learn Laravel and create such an extension myself :)
@szachara And I think that Laravel IDE Helper can really solve your problem for now, the key is to autoload classes generated by it in PHPStan.
Ohh, I understand now what do you meant about laravel-ide-helper.
Do you know about some functional Extension which you are describing at Methods class reflection extensions which offers similar functionality?, may be you can put me on the right way.
It's all pretty straightforward. But there's some discussion about methods for Zend plugins here: https://github.com/phpstan/phpstan/issues/219
And you can find some extensions in the PHPStan organization on GitHub: https://github.com/phpstan
Ok, diky, mozno neskor napisem extension, ktora bude rozpoznavat Laravel Facades, nevyzera to zlozito, nateraz pouzijem ignoreErrors.
These errors you're seeing are actually good to look at and rewrite in a way that PHPStan can analyze. That way the code is more declarative which is easier to understand (compared to less reading.)
PHPStan fully supports Laravel as it is. It just doesn't handle the facades well which are a mess for anything really. That's why the IDE Helper exists, is to work around the facades and magic __call methods in Laravel.
Internally Laravel is open to having more docblocks added which help with the magic method calls. PRs just need to be made for them there.
@Garbee I don't use Laravel but I always thought there's a lot of magic in it that is not possible to describe just using phpDocs. And I also think it's not a way to not use its magic because once a developer is using Laravel, they should use it to the full "potential", whatever that means, although I personally would rather use a different framework. So I think the right way is to write extensions that describe every part of Laravel that uses some PHP magic using class reflection and dynamic return type extensions.
I tried 0.7.0 with level=2 but this won't be usable with Laravel. I'm using the ide helper Package and thanks to PhpStorm with the magic _ide_helper.php file, inside the IDE it's correctly auto-compelted but no luck with phpstan.
PHPStan fully supports Laravel as it is. It just doesn't handle the facades well which are a mess for anything really. That's why the IDE Helper exists, is to work around the facades and magic __call methods in Laravel.
I don't think it's just the Facades. I think the Service Locator is equally a problem, by calling app() or other means to which calls make or makeWith: it may return any hinted class passed as string and phpstan is not able to infer it. There are probably other issues but this one is the most striking one in my opinion.
Have you tried putting _ide_helper.php in autoload_files in phpstan.neon?
On Fri, 19 May 2017 at 20:07, Markus Fischer notifications@github.com
wrote:
I tried 0.7.0 with level=2 but this won't be usable with Laravel. I'm
using the ide helper Package and thanks to PhpStorm with the magic
_ide_helper.php file, inside the IDE it's correctly auto-compelted but no
luck with phpstan.PHPStan fully supports Laravel as it is. It just doesn't handle the
facades well which are a mess for anything really. That's why the IDE
Helper exists, is to work around the facades and magic __call methods in
Laravel.I don't think it's just the Facades. I think the Service Locator is
equally a problem, by calling app() or other means to which calls make or
makeWith: it may return any hinted class passed as string and phpstan is
not able to infer it. There are probably other issues but this one is the
most striking one in my opinion.—
You are receiving this because you modified the open/close state.Reply to this email directly, view it on GitHub
https://github.com/phpstan/phpstan/issues/239#issuecomment-302772649,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuA_1sCicKZFhCoLUI0kwjQcy_YMnks5r7dptgaJpZM4NJ7kv
.>
Ondřej Mirtes
Have you tried putting _ide_helper.php in autoload_files in phpstan.neon?
Interesting, no! I'm playing around with it now, thanks.
One problem of course is, the _ide_helper.php file contains this at the top 😄
namespace {
exit("This file should not be included, only analyzed by your IDE");
}
@mfn Interesting, so the person that said that they're using the IDE helper must have modified the file somehow 🤔 So the reasons to develop a fullfledged phpstan-laravel extension still stand...
So the reasons to develop a fullfledged phpstan-laravel extension still stand...
I haven' wrapped around my head of this, but in my (unfortunately) private project, there are ~100 errors less using this _ide_helper.php file. That is a lot.
If a phpstan laravel extension were to include this functionality and more ... I am wondering if that wouldn't need to duplicate a good part of the ide helper package to have a useful result => however, I'm not that acquainted with either phpstan nor ide-helper to make that judgement. Just some outsider observation 🤷♂️
@mfn Yep, extension for Laravel is in the queue, it'd be a killer feature for PHPStan :) Unfortunately I myself don't use Laravel myself so I don't know every corner of the framework, but I want to implement the extension one day, probably by starting to analyze Laravel itself and some open-source projects that depend on it.
I think, that this exit() fce can be simply cheated like this:
namespace {
if (empty($phpstanAnalyzing))
exit("This file should not be included, only analyzed by your IDE");
}
}
@ondrejmirtes Can we somehow check, that the _ide_laravel.php file was included by phpstan, or which global variable could be used for this scenario?
@szachara I think that if you're modifying the file, you can remove the exit altogether.
And if you want some kind of detection, check for class_exists(\PHPStan\Analyser\Analyser::class, false).
Sooo why did no one send a PR yet?
And does it actually fix it?
Nope. Running this command with the exit disabled on a fresh Laravel instance i still get errors:
vendor/bin/phpstan analyse --autoload-file=_ide_helper.php app
The errors:
------ ------------------------------------------------------------------------------------
Line app/Providers/RouteServiceProvider.php
------ ------------------------------------------------------------------------------------
54 Call to an undefined static method Illuminate\Support\Facades\Route::middleware().
68 Call to an undefined static method Illuminate\Support\Facades\Route::prefix().
------ ------------------------------------------------------------------------------------
@michielgerritsen have you checked if the internal Facade Route docs maybe are out of date?
I tried phpstan with laravel-ide-helper few weeks ago too and it's really better, many magic calls are right and resolved correctly, but there are still cases, which need PRs :)
Made a little shell script for Laravel which sets this up based on the above comment
https://gist.github.com/edmondscommerce/89695c9cd2584fefdf540fb1c528d2c2
Can confirm, using IDE Helper did work. Thanks @barryvdh
Have anyone met with this error while using $this->app->config->get('app.locales')?
With ide_helper.php loaded.
------ ---------------------------------------------------------------------------------------
Line app/Providers/RouteServiceProvider.php
------ ---------------------------------------------------------------------------------------
55 Access to an undefined property Illuminate\Contracts\Foundation\Application::$config.
58 Call to an undefined method Illuminate\Contracts\Foundation\Application::setLocale().
Big improvement waiting to be merged: https://github.com/barryvdh/laravel-ide-helper/pull/618
@ondrejmirtes Have you acquired the knowledge to support vanilla Laravel without laravel-ide-helper or weebly/phpstan-laravel?
Even using any of them does not produce error-free analysis.
@szepeviktor Unfortunately, I don't yet know Laravel enough to write an official extension. It would surely help if you reported those deficiencies to the authors of laravel-ide-helper and weebly/phpstan-laravel, whichever it is. If you think that something can be fixed in PHPStan itself, let me know.
Resolved: https://github.com/nunomaduro/larastan
Most helpful comment
I've removed the exit: https://github.com/barryvdh/laravel-ide-helper/commit/ce72ff46631401c2ca8466a730b492e048d01497