Phpstorm 9 EAP方法"独特"在类Illuminate\Support\Fluent中找不到

Yae*_*kay 4 php phpstorm laravel-5

我正在开发一个网站项目,我正在使用Laravel 5和PHPStorm 9 EAP.

我创建了一个迁移并使用此代码$table->string('name')->unique();,IDE突出显示unique()并显示一条消息Method "unique" not found in class Illuminate\Support\Fluent.

这是我的迁移:

class CreateProductsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('products', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name')->unique();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('products');
}

}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

max*_*lms 13

调用$table->integer('user_id')返回一个新的实例Illuminate\Support\Fluent.但是Fluent类没有提供unique()方法.相反,它使用PHP魔术方法__call.这就是PHPStorm抱怨的原因.

选项一是告诉PHPStorm这unique()是一种有效的方法.为此我们可以添加一些PHPDoc vendor/laravel/framework/src/Illuminate/Support/Fluent.php并告诉PHPStorm这unique()是一个有效的方法:

/**
 * @method unique
 */
class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable 
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是,我不建议修改/ vendor /文件夹中的文件.而是在Laravel上创建错误报告/拉取请求.有人已经为此创建了一个拉取请求(https://github.com/illuminate/support/pull/25).

选项二是修改您的create方法并尝试避免Fluent界面上的魔术方法:

Schema::create('products', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('name');
    $table->unique('name');
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)


Sky*_*dev 5

我个人使用帮助PHP类文件(如_ide_helper.php)来告诉PHPStorm方法。

对于Fluent,我创建了以下文件,以供PHPStorm索引,但未包含在实际的php文件中:

<?php

namespace Illuminate\Support;

/**
 * @method Fluent first()
 * @method Fluent after($column)
 * @method Fluent change()
 * @method Fluent nullable()
 * @method Fluent unsigned()
 * @method Fluent unique()
 * @method Fluent index()
 * @method Fluent primary()
 * @method Fluent default($value)
 * @method Fluent onUpdate($value)
 * @method Fluent onDelete($value)
 * @method Fluent references($value)
 * @method Fluent on($value)
 */
class Fluent {}
Run Code Online (Sandbox Code Playgroud)

附带说明,然后应禁用有关多重定义的类的警告,但是,如果使用的是IDE Helper模块,则无论如何都应禁用它们。