1

Question is about phpDocs, and describing array parameter. For example i have code like this

<?= $view['form']->getElement('number_of_doors_id'); ?>

And phpStorm doesn't understand what 'form' key is.

I've tried

/**
* @var $view['form'] \\Framework\Templating\Helper\FormHelper
*/

/**
* @params $view['form'] \\Framework\Templating\Helper\FormHelper
*/
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Itsmeromka
  • 3,621
  • 9
  • 46
  • 79
  • 1
    Have a look [\[ here \]](http://stackoverflow.com/questions/34703744/post-method-is-not-passing-the-values-while-get-method-works-fine). phpStorm is problematic. – sjsam May 24 '16 at 13:04

3 Answers3

1

The only solution that comes to my mind is to use multiple type for array like this:

/**
* @var (\Framework\Templating\Helper\FormHelper|int)[] $view
*/

Where int is another type for array key

From PHPDOC:

specified containing multiple types, the Type definition informs the reader of the type of each array element. Each element can be of any of the given types. Example: @return (int|string)[]

The other option is to assign this array variable to normal variable.

I think there is no other possibility so far.

Robert
  • 19,800
  • 5
  • 55
  • 85
1

In a similar situation i ended up extracting the variables from the array and having individual @var blocks:

/**
* @var \Framework\Templating\Helper\FormHelper $form
* @var \Framework\Templating\Helper\UrlHelper $url
*/

extract($view);
$form->getElement('number_of_doors_id');

An alternative i have seen is having a 'viewModel' class per view:

class AboutViewModel
{
    /**
    * @var \Framework\Templating\Helper\FormHelper $form
    * @var \Framework\Templating\Helper\UrlHelper $url
    */
    public $form,
           $url;

}

and in your view

/**
* @var \Framework\Views\AboutViewModel $vm
*/
$vm->form->getElement('number_of_doors_id');
Steve
  • 20,703
  • 5
  • 41
  • 67
  • Good idea, sadly extract needs an array to work, and with **(array) $vew** isn't working good. – Itsmeromka May 25 '16 at 07:49
  • 1
    Oh, i thought `$view` was an array. What type is it? Some kind of collection? – Steve May 25 '16 at 09:23
  • Yeah, it's a symfony Templating component PhpEngine object. http://symfony.com/doc/current/components/templating/introduction.html – Itsmeromka May 25 '16 at 09:52
0

Modified @Robert answer and solved this problem like this

<?php
    /**
    * @var $view \Framework\Templating\Helper\FormHelper[]
    */
?>

And if you need two different types just do

/**
 * @var $view \Framework\Templating\Helper\FormHelper[]|\Framework\Templating\Helper\TranslatorHelper[]
 */

And so on

Itsmeromka
  • 3,621
  • 9
  • 46
  • 79