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.

trante
  • 33,518
  • 47
  • 192
  • 272
  • I don't think this has anything to do with Cake. Check PHPstorm settings/docs, it may not be activating autocomplete on .ctp files? – Costa Jun 04 '13 at 00:21
  • Edited question Costa. – trante Jun 04 '13 at 05:27
  • Have you checked your paths in PHPStorm? It may not be checking the path where your custom helpers are located. – Costa Jun 04 '13 at 20:57
  • I believe OP has his answer already: http://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. – LazyOne Jun 05 '13 at 14:13

4 Answers4

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)

trante
  • 33,518
  • 47
  • 192
  • 272
  • 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. – Álvaro González Jun 06 '17 at 12:25
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.

jimiyash
  • 2,494
  • 2
  • 20
  • 29
  • 1
    As I noted in my question I tried this ```*@var $this View``` but it autocompletes only core helpers. – trante Jun 04 '13 at 05:28
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...

david.philip
  • 420
  • 2
  • 13
  • Is the `/** *@var $this View | StatusHelper */`syntax correct? – Álvaro González Jun 06 '17 at 12:13
  • [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? – david.philip Jun 14 '17 at 21:28
  • 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). – Álvaro González Jun 15 '17 at 07:48
  • Right, gotcha! I'll edit my answer so no one else trips up over the `*`- thanks – david.philip Jun 16 '17 at 12:38
  • 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). – Álvaro González Jun 16 '17 at 13:44
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  */  ?>
Sergio
  • 2,369
  • 1
  • 15
  • 22