1

I have a project that utilised MVC where the view file is inherting $this which refers to a view class attached to the controller.

Helper classes have been attached in some of the views and are used like follows:

<?=$this->someHelper->renderSomething()?>

I was hoping to help devs and the IDE out by doing this:

/** @var SomeHelper $this->someHelper */

It's not supported, seemingly. Is there a way to achieve this?

I can only find a workaround at the moment, to declare the helper as a new variable and include a @var statement for that.

bcmcfc
  • 25,966
  • 29
  • 109
  • 181

1 Answers1

3

It's not possible, you are supposed to type hint the $this instead. If $this is not any concrete class you can type hint, create a fake class/interface instead which will act as a helper to the IDE:

// somewhere outside of your code base, but accessible by the IDE
// use the name of your choice
interface CodeIgniterMvc
{
    /**
     * @return string
     */
    function renderSomething(): string;

    /**
     * @param array $filter Filtering conditions
     * @return \Your\App\Models\User[]
     */
    function getUsers(array $filter): array;
}

and in the views:

/** @var $this CodeIgniterMvc **/

Of course include in the repository so every team member can gain such benefits.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • Is there a way for the IDE to know it applies to every file with a phtml extension? – bcmcfc Feb 28 '20 at 10:52
  • 1
    I guess it is possible somehow using phpstorm's internals, but I'm not aware how to do that. I have excellent support for Symfony, I don't need any type hints. – Mike Doe Feb 28 '20 at 10:55
  • *"Is there a way for the IDE to know it applies to every file with a phtml extension?"* If you will write such plugin, then yes, you can inject/declare some stuff for ALL such files. But PhpStorm does not have such functionality for user to use in any project out of the box (so you have to have `/** @var $this MyClass */` kind of comment in every .phtml file). – LazyOne Feb 28 '20 at 11:57