Vscode-intelephense: Show suggestions when inside Trait

Created on 23 Dec 2019  路  5Comments  路  Source: bmewburn/vscode-intelephense

Problem

When I'm writing a Trait that is to be used in some class(es) 鈥斅營'd like to have methods/properties of that class to be suggested.

Example:
```php
// User.php

class User
{
use NotifiableTrait;

protected $email;

public function ban()
{
    $this->notify("You've been banned."); // "notify" is suggested here, which is Good.
}

}

// NotifiableTrait.php
trait NotifiableTrait
{
public function notify($text)
{
Mail::to($this->email)->send(new GenericMessage($text)); // "email" was not suggested here. Which would be nice to do.
}
}
```

Solution

Intelephence to "deduct" in what classes the current trait is used and suggests that classes' props and methods.

At least for the case, when a trait is used in one class only. For "a trait used in many classes" 鈥斅爉aybe some visual grouping of suggestions by classes from which they're taken.

All 5 comments

People usually don't like to hear that, but in that case you should use abstract methods in trait. Similar issue here: https://github.com/bmewburn/vscode-intelephense/issues/829#issuecomment-563932070

As for suggestions itself, would be nice, but it's probably pain to implement and I wouldn't count on that. Also list of every method where most of them are not relevant to you, would be pretty annoying.

People usually don't like to hear that, but in that case you should use abstract methods in trait. Similar issue here: #829 (comment)

As for suggestions itself, would be nice, but it's probably pain to implement and I wouldn't count on that. Also list of every method where most of them are not relevant to you, would be pretty annoying.

Usually, it's done by setting up @mixin phpdoc, which references the class it has to extend. Unfortunately, intelephense still doesn't support mixins. And there's probably no pain in implementing it.

@bmewburn I see that people keep asking for the feature. Any plans on implementing this? That's the only thing that I miss from phpstorm.

IMO traits shouldn't need knowledge of the consuming class. Though, if @mixin will make this work for users that want this functionality then that's reasonable, let's track it in #123

Another workaround is to type hint $this using @var.

<?php
// User.php

class User
{
    use NotifiableTrait;

    protected $email;

    public function ban()
    {
        $this->notify("You've been banned."); // "notify" is suggested here, which is Good.
    }
}

// NotifiableTrait.php
trait NotifiableTrait
{
    public function notify($text)
    {
        /** @var User $this */
        Mail::to($this->email)->send(new GenericMessage($text)); // "email" was not suggested here. Which would be nice to do.
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nn-hh picture nn-hh  路  3Comments

zlianon picture zlianon  路  3Comments

ghost picture ghost  路  3Comments

zlianon picture zlianon  路  4Comments

aleksandervines picture aleksandervines  路  3Comments