Coc.nvim: Optimal PHP set up?

Created on 25 Mar 2019  路  19Comments  路  Source: neoclide/coc.nvim

Hi,

I'm new to PHP and am trying to get my wonderful nvim setup that works with TS, JS & Go to work as well as for PHP.

Could someone share which LS they are using, and maybe some additional plugins, .vimrc config, or coc.vim config to get a better experience? Thanks.

Most helpful comment

@codeclem

It complains about ->tags(). But it works if I replace auth()->user() with Auth::user()

I see. The laravel-ide-helper package should generate documentation to make auth() autocomplete just as using Auth:: would. The only suggestion I can come up with on top of my head is to try re-generating the phpDoc for Laravel Facades.

Edit: I actually have to do this in order for it to work as expected:

        /** @var \App\User */
        $user = auth()->user(); // Or `Auth::user`
        $user->someRelation()

All 19 comments

Have you tried these https://github.com/neoclide/coc.nvim/wiki/Language-servers#php ?

I'm using this one https://www.npmjs.com/package/coc-phpls and it seems to work very well for most projects. Having some timeout issues, but I'm thinking this has something to do with permissions or something like that becuase I have vendor stuff living in a docker volume.

@andersevenrud - cheers, those have helped, unfortunately it does not seem to play well with Laravel. I am learning the framework so it would be extremely helpful to have intelligent auto complete.

Do you have any experience with that?

I've noticed this as well. It seems that I never can get auto-completion deeper than one or two levels at most.

Slim and Symfony seems to work a lot better, so there must be something in the dependency chain that breaks something. I also get a couple of timeouts every now and then.

A couple of things of note is that type hinting in the sources is critical for things to work properly. You can also add phpdoc:

class MyClass
{
  /** @var \Namespace\Some\Other\Class $reference */
  private $reference;
}

This is probably one of the reasons why it's a bit of a struggle on Laravel, because there's a lot of "magic methods" and such.

Has anyone figured out how to properly handle *.blade.php files? I was using prettier for code formatting, but its doing a horrible job. Will coc-phpls format?

@jonleopard I'm using https://github.com/jwalton512/vim-blade with https://github.com/alvan/vim-closetag

let g:closetag_filenames = "*.html,*.xhtml,*.phtml,*.blade.php"
autocmd Filetype php setlocal tabstop=4 softtabstop=4 shiftwidth=4
autocmd BufNewFile,BufRead *.blade.php set ft=blade

Just expanding on my previous comment

A couple of things of note is that type hinting in the sources is critical for things to work properly. You can also add phpdoc:

Using the same docblocks that phpStorm generates, autocompletion in models works pretty good.

Example:

/**
 * App\Article
 *
 * @property integer $id
 * @property string $type
 * @property string $title
 * @property string $body
 * @property \Illuminate\Database\Eloquent\Collection|\App\Publisher[] $publishers
 * @property \Illuminate\Database\Eloquent\Collection|\App\Tag[] $tags
 * @property-read \Carbon\Carbon $created_at
 * @property-read \Carbon\Carbon $updated_at
 * @property-read \Carbon\Carbon $deleted_at
 * @mixin \Eloquent
 * @package App
 */
class Article extends Model
{
    use SoftDeletes;

    /**
     * @var array
     */
    protected $fillable = [
        'type',
        'title',
        'body'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function publishers()
    {
        return $this->belongsToMany(Publisher::class)
            ->orderBy('order')
            ->withPivot('type');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

Is there an easy way to generate these blocks within vim?

I found this while researching this subject https://github.com/barryvdh/laravel-ide-helper . Been a bit busy until today, so haven't tested it yet -- but it looks like it will do the job :)

The ide-helper can probably be combined with https://github.com/noahfrederick/vim-laravel , but I don't really mind writing :!php artisan when doing things, heh.

Been a bit busy until today, so haven't tested it yet

I just did and it adds the docblock for the Model Class just as advertised :raised_hands:

One issue I had with using the php intelephense with coc was the it would autocomplete an extra $ for variables

example:

$message = "";
$hello = $mes<tab>
// gave me 
$hello = $$message;

I fixed this with the following autocommand in my init.nvim
autocmd BufNewFile,BufRead *.php set iskeyword+=$

in case its helpful

Use autocmd FileType php set iskeyword+=$

I'm using laravel-ide-helper and coc-phpls but it still complains about relationship methods (like $article->tags()) not existing. Any ideas?

@codeclem Are you using docblocks on your class methods ?

Edit:

The @mixin Eloquent on the class, and @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|YourClassName on the methods should be anough to make it work.

Like this?

/**
 * App\Models\User
 *
 * @property int $id
 * @property string $name
[snip]
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'api_token'
    ];

    /**
     * Comments
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany|\App\Models\Comment
     */
    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }

Still doesn't work.

@codeclem Indeed. That should be enough. Does anything else work ?

Have you tried to troubleshooting tips ? Might be some errors occurring on the back end.

Seems to have started working now. Not sure why it wasn't before, thank you.

Sorry.. it looks like specifically it's not working when using helpers. Like in this line:
$tags = auth()->user()->tags()->with('posts', 'posts.user', 'posts.tags')->get();

It complains about ->tags(). But it works if I replace auth()->user() with Auth::user()

@codeclem

It complains about ->tags(). But it works if I replace auth()->user() with Auth::user()

I see. The laravel-ide-helper package should generate documentation to make auth() autocomplete just as using Auth:: would. The only suggestion I can come up with on top of my head is to try re-generating the phpDoc for Laravel Facades.

Edit: I actually have to do this in order for it to work as expected:

        /** @var \App\User */
        $user = auth()->user(); // Or `Auth::user`
        $user->someRelation()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

aareman picture aareman  路  3Comments

tom-james-watson picture tom-james-watson  路  3Comments

FrankLA0203 picture FrankLA0203  路  3Comments

rkulla picture rkulla  路  3Comments

cvlmtg picture cvlmtg  路  3Comments