Autocompletion for ZF2 view helpers in PhpStorm
Asked Answered
B

1

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
Barto answered 28/2, 2013 at 15:1 Comment(10)
How inlineScript() and/or translate() are declared? Maybe they simply do not have appropriate PHPDocs ?Aftercare
@LazyOne, they are dynamicly created through the magic __call method :)Barto
Something like github.com/zendframework/zf2/blob/release-2.1.3/library/Zend/… may be applied to the PHPRenderer... Then you would typehint /* @var $this \Zend\View\Renderer\PhpRenderer */. That could potentially workOverarch
@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?Barto
@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.Overarch
@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.Barto
@BramGerritsen awesome!Overarch
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.Aftercare
@Ocramius, You can create an answer of your first comment, than I can accept it.Barto
@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.Barto
O
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

Overarch answered 28/2, 2013 at 15:55 Comment(2)
How does this help for user defined view helpers, though? You cannot edit AbstractController so user defined helpers will still be unknown.Although
@QuolonelQuestions you can always subclass if it's worth itOverarch

© 2022 - 2024 — McMap. All rights reserved.