5

Does anyone know if PHPStorm has some builtin support for view helper autocompletion or a possibility to write a plugin for it. I don't want to use inline var definitions for this as this would be cumbersome to do if I use a lot of view helpers

$this->inlineScript()-> //I want some autocomplete here.

$this->translate('some translation')-> //Please give me autocompletion

If I use var definitions it will end up as something like this, but it will really clutter up my view:

/* @var $inlineScript \Zend\View\Helper\InlineScript */
$inlineScript = $this->inlineScript();
$inlineScript-> //Now I have autocompletion goodness

/* @var $translate \Zend\I18n\View\Helper\Translate */
$translate = $this->translate();
$translate('some translation')-> //Now I have autocompletion goodness
Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
  • How `inlineScript()` and/or `translate()` are declared? Maybe they simply do not have appropriate PHPDocs ? – LazyOne Feb 28 '13 at 15:09
  • 1
    @LazyOne, they are dynamicly created through the magic `__call` method :) – Bram Gerritsen Feb 28 '13 at 15:12
  • 2
    Something like https://github.com/zendframework/zf2/blob/release-2.1.3/library/Zend/Mvc/Controller/AbstractController.php#L31-L42 may be applied to the PHPRenderer... Then you would typehint `/* @var $this \Zend\View\Renderer\PhpRenderer */`. That could potentially work – Ocramius Feb 28 '13 at 15:15
  • @Ocramius, seems like a nice solution. That would imply I need to create a PR for ZF2 to accommodate for all the build in ZF2 helpers. Any chance this would be accepted? How about any custom view helpers than? Which editor are you using and don't you miss the autocompletion for view when you are editing views? – Bram Gerritsen Feb 28 '13 at 15:22
  • @BramGerritsen correct. For custom view helpers, you would have to typehint `$this` as a "fake" class that extends the `PhpRenderer` and has the additional hints. – Ocramius Feb 28 '13 at 15:33
  • @Ocramius, yep thought so. It's better than nothing, but it kinda sucks to extend the PhpRenderer for only annotate method hints. But it is ok for the time being. Maybe the good guys at jetbrain will add some ZF2 support in the future so we don't need this kind of 'hacks'. I'll create a PR for the buildin helpers tomorrow. – Bram Gerritsen Feb 28 '13 at 15:38
  • @BramGerritsen awesome! – Ocramius Feb 28 '13 at 15:40
  • What about declaring them via `@method` PHPDOc comment for that class? **P.S.** Complete side note: proper PHPDoc syntax is `/** @var [type] [varname] */` and not `/* @var [varname] [type] */` (notice the order and double star) -- both variants work in PhpStorm, but "proper" one is the first. – LazyOne Feb 28 '13 at 15:52
  • @Ocramius, You can create an answer of your first comment, than I can accept it. – Bram Gerritsen Feb 28 '13 at 15:52
  • @LazyOne, `@method` is indeed the way to go. The file ocramius referred to did it the same way. Thanks for the feedback for the proper syntax, I'm using the first variant mostly I think. – Bram Gerritsen Feb 28 '13 at 15:56

1 Answers1

12

NOTE I'm posting my method discussed in the comments as answer.

To typehint non-existing methods, the syntax is as following:

/**
 * @method \Zend\Mvc\Controller\Plugin\Url url(string $route = null, array $params = null)
 */
class MyClass
{
}

This allows us to use have a type-hint for method url on any variable recognized as MyClass:

/* @var $a \MyClass */
$a->// typehint!

You need such a "fake" class and then start your view scripts with:

/* @var $this \MyFakeClass */

That will give you type-hints on $this within your view script. You could ideally open a pull request against https://github.com/zendframework/zf2 with something similar to https://github.com/zendframework/zf2/pull/3438

Ocramius
  • 25,171
  • 7
  • 103
  • 107