PHPStorm autocomplete for CakePHP custom helpers in view files
Asked Answered
P

4

8

I use PhpStorm 6.0.2 and CakePHP 2.3.

In my controller file I define this and get autocomplete for my custom components:

/**
 * @property MysuperComponent $Mysuper
 */

Regarding to this, in my view files I define this to reach Cake's core helpers and this works:

/**
 * @var $this View
 */

I need autocomplete for custom helpers inside my views. I tried this but didn't work:

/**
 * @property Myelegant $MyelegantHelper 
 */

When I do this, this works partially:

/**
 * @var $Myelegant MyelegantHelper
 */

I get this $Myelegant-> autocomplete. But it isn't adequate. I need autocomplete like this: $this->Myelegant->

Notes: Autocomplete successfully works for core helpers inside view (ctp) files. But not for custom helpers.

Peignoir answered 3/6, 2013 at 20:19 Comment(4)
I don't think this has anything to do with Cake. Check PHPstorm settings/docs, it may not be activating autocomplete on .ctp files?Showroom
Edited question Costa.Peignoir
Have you checked your paths in PHPStorm? It may not be checking the path where your custom helpers are located.Showroom
I believe OP has his answer already: devnet.jetbrains.com/thread/445077?tstart=0 . If yes -- I suggest formulate the solution/experience and post it as an answer so it will be useful for other guys.Bastia
P
9

Add new file /app/View/HintView.php
Add your custom helpers' names on PHPDoc.

<?php

App::uses('View', 'View');

/**
 * @property MyelegantHelper $Myelegant
 * */

class HintView extends View {

}

Inside your layout files or View files (ctp files) add this code on top

/**
 * @var $this HintView
 */

Now inside your views you can see like this:

$this->MyElegant
     ->Blocks
     ->Cache
     ->Form

$this->MyElegant->somefunction()
                  anotherfunction()
                  oldfunction()

You don't have to extend your Views from HintView. It is only for PhpStorm's autocomplete.

(Note that you can make it faster with creating shortcuts to codes. For example goto Settins / IDE Settings / Live Templates. Add new template. For example "myeleg" for "$this->MyElegant->" So when you write "myeleg" and press Tab key it will write the class name automatically)

Peignoir answered 6/6, 2013 at 11:29 Comment(1)
Ugly as it is, it's always better than editing /lib/Cake/View/View.php. It's also worth noting that it's helpful too when you extend builtin helpers and use className to mask them.Boating
S
2

Have you tried looking at this article:

http://blog.hwarf.com/2011/08/configure-phpstorm-to-auto-complete.html

Look at the section "Setting Up Helper Auto-completion in Views". Hopefully this helps.

Striction answered 4/6, 2013 at 1:23 Comment(1)
As I noted in my question I tried this *@var $this View but it autocompletes only core helpers.Peignoir
I
1

I know this is an old post, but came across this having the same issue. Solved it this way:

For me, my custom helper is called StatusHelper, so needed the @property as follows:

App::uses('Helper', 'View');
/**
 * @property StatusHelper $Status
 * */
class StatusHelper extends AppHelper {

Then in the view .ctp file, I just needed the following at the top:

<?php /* @var $this View|StatusHelper */ ?>

Now the autocomplete works for my PHPstorm in that view for both core View vars as well as whatever ever methods are in my helper.. Happy days

Using Cake 2.5 - PHPstorm 10. Hope this helps someone else out there...

Ignescent answered 3/3, 2016 at 14:23 Comment(5)
Is the /** *@var $this View | StatusHelper */syntax correct?Boating
[This is for Cake 2.x] The pipe ` | ` maps both classes to the $this variable within the .CTP; Then PhpStorm will help with the autocomplete on both the standard view methods $this->Html->{method} and your custom helper methods $this->Status->{method} - I'm not sure about PHPDoc syntax correctness but PhpStorm likes it and gives me my autocomplete, I use it daily! Do you have a correction for me?Ignescent
Oh, sorry, I hadn't understood your intentions and tried the syntax verbatim. You have an extraneous asterisk (*@var), if you remove it you get code intelligence for $this->Status. Also, since we only need a type hint indicator and not a proper docblock we could also use /* ... */ instead of /** ... */ (this applies for all answers here).Boating
Right, gotcha! I'll edit my answer so no one else trips up over the *- thanksIgnescent
I had already edited the bogus asterisk out (/** *@var to /** @var). Both syntaxes (/** */ and /* */) work, it's just they are slightly different things (the second one will be ignored if you actually use phpDocumentor to render documentation).Boating
P
0

Is Easy, Test in CakePHP 3.x to PHPStrom, supports namespace.

Add in to file views.ctp type PHPDoc

<?php /** @var $this Cake\View\View  */  ?>
Patrimony answered 16/11, 2016 at 2:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.