phplaravelphpstormphpstorm-2017.1

PhpStorm - Some warnings on Laravel facades


enter image description here

  1. I make right usage of Laravel's facades and PhpStorm gives me warnings, why is that?

  2. And on image I pointed "x" for some...types of data? In functions I use, why do I have these? How to remove them?


Solution

  • Using facades with Laravel

    Luke Waite is right:

    You're not using facades. You've imported the classes, and on the first, Categories, the IDE is telling you that the get method is not a static method.

    Just import the facade instead (if it exist).

    See the documentation on Facades to learn more on how to use the available facades and how to define your own.

    A facade class should look like this:

    use Illuminate\Support\Facades\Facade;
    
    class Cache extends Facade
    {
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor() 
        { 
            return 'cache';
        }
    }
    

    Where the 'cache' string is the name of a service container binding and defined in a service provider, something like this:

    use App\Cache\MyCache;
    use Illuminate\Support\ServiceProvider;
    
    class CacheServiceProvider extends ServiceProvider
    {
        /**
         * Register bindings in the container.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton('cache', function ($app) {
                return new MyCache();
            });
        }
    }
    

    Fixing the warnings with Facades

    That being said, I was tired of the warnings and the missing auto-completion and highlighting with facades so I also searched to find a way to fix these.

    I came upon laravel-ide-helper which adds Laravel CLI commands that generates php files that only serves to be parsed by your IDE.

    Install

    Require this package with composer using the following command:

    composer require barryvdh/laravel-ide-helper
    

    After updating composer, add the service provider to the providers array in config/app.php

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class, To install this package on only development systems, add the --dev flag to your composer command:

    composer require --dev barryvdh/laravel-ide-helper
    

    In Laravel, instead of adding the service provider in the config/app.php file, you can add the following code to your app/Providers/AppServiceProvider.php file, within the register() method:

    public function register()
    {
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
        // ...
    }
    

    This will allow your application to load the Laravel IDE Helper on non-production enviroments.

    Automatic phpDoc generation for Laravel Facades

    You can now re-generate the docs yourself (for future updates)

    php artisan ide-helper:generate
    

    Note: bootstrap/compiled.php has to be cleared first, so run php artisan clear-compiled before generating (and php artisan optimize after).

    You can configure your composer.json to do this after each commit:

    "scripts":{
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan ide-helper:generate",
            "php artisan ide-helper:meta",
            "php artisan optimize"
        ]
    },
    

    The .phpstorm.meta.php and _ide_helper.php files will be generated and should be added to your .gitignore as you don't want to commit these.