PhpStorm - Laravel外墙的一些警告

Kry*_*ska 1 php phpstorm laravel phpstorm-2017.1

在此输入图像描述

  1. 我正确使用了Laravel的外墙,PhpStorm给了我警告,为什么呢?

  2. 在图像上,我指出"x"表示某些......数据类型?在我使用的功能中,我为什么要这些?如何删除它们?

Emi*_*ron 12

使用Laravel外墙

卢克韦特是对的:

你没有使用外墙.您已导入类,并在第一个类别上,IDE告诉您get方法不是静态方法.

只需导入外观(如果存在).

请参阅有关Facades文档,以了解有关如何使用可用外墙以及如何定义自己的外墙更多信息.

Facade类应如下所示:

use Illuminate\Support\Facades\Facade;

class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() 
    { 
        return 'cache';
    }
}
Run Code Online (Sandbox Code Playgroud)

其中'cache'字符串是服务容器绑定的名称并在服务提供者中定义,如下所示:

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();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

使用Facades修复警告

话虽这么说,我已经厌倦了警告和失踪的自动完成并突出了立面,所以我也搜索找到解决这些问题的方法.

我找到了laravel-ide-helper,它添加了Laravel CLI命令,生成只能由IDE解析的php文件.

安装

使用以下命令将此包与composer一起使用:

composer require barryvdh/laravel-ide-helper
Run Code Online (Sandbox Code Playgroud)

更新composer后,将服务提供者添加到providers数组中 config/app.php

Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,要仅在开发系统上安装此程序包,请将--dev标志添加到composer命令:

composer require --dev barryvdh/laravel-ide-helper
Run Code Online (Sandbox Code Playgroud)

在Laravel,而不是在添加服务提供商 config/app.php的文件,你可以将下面的代码添加到您的 app/Providers/AppServiceProvider.php文件时,中register() 方法:

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

这将允许您的应用程序在非生产环境中加载Laravel IDE Helper.

Laravel Facades自动生成phpDoc

您现在可以自己重新生成文档(以便将来更新)

php artisan ide-helper:generate
Run Code Online (Sandbox Code Playgroud)

注意: bootstrap/compiled.php必须先清除,因此php artisan clear-compiled在生成之前运行(及php artisan optimize之后).

您可以配置composer.json在每次提交后执行此操作:

"scripts":{
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan ide-helper:generate",
        "php artisan ide-helper:meta",
        "php artisan optimize"
    ]
},
Run Code Online (Sandbox Code Playgroud)

.phpstorm.meta.php_ide_helper.php文件将被产生,并应添加到您的.gitignore,你不想犯这些.