Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimal PHP set up? #590

Closed
tomspeak opened this issue Mar 25, 2019 · 19 comments
Closed

Optimal PHP set up? #590

tomspeak opened this issue Mar 25, 2019 · 19 comments

Comments

@tomspeak
Copy link

tomspeak commented Mar 25, 2019

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.

@andersevenrud
Copy link

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.

@tomspeak
Copy link
Author

@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?

@andersevenrud
Copy link

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.

@jonleopard
Copy link

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?

@andersevenrud
Copy link

andersevenrud commented Apr 4, 2019

@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

@andersevenrud
Copy link

andersevenrud commented Apr 9, 2019

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);
    }
}

@tomspeak
Copy link
Author

tomspeak commented Apr 9, 2019

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

@andersevenrud
Copy link

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 :)

@andersevenrud
Copy link

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.

@andersevenrud
Copy link

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 🙌

@aareman
Copy link

aareman commented Jun 24, 2019

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

@chemzqm
Copy link
Member

chemzqm commented Jun 24, 2019

Use autocmd FileType php set iskeyword+=$

@codeclem
Copy link

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

@andersevenrud
Copy link

andersevenrud commented Dec 30, 2019

@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.

@codeclem
Copy link

codeclem commented Dec 31, 2019

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.

@andersevenrud
Copy link

@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.

@codeclem
Copy link

codeclem commented Jan 1, 2020

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

@codeclem
Copy link

codeclem commented Jan 1, 2020

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()

@andersevenrud
Copy link

andersevenrud commented Jan 1, 2020

@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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants