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
*/
1
  • 1
    Have a look [ here ]. phpStorm is problematic. Commented May 24, 2016 at 13:04

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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');

3 Comments

Good idea, sadly extract needs an array to work, and with (array) $vew isn't working good.
Oh, i thought $view was an array. What type is it? Some kind of collection?
Yeah, it's a symfony Templating component PhpEngine object. symfony.com/doc/current/components/templating/introduction.html
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

1 Comment

but it won't work because it says that very key in $view array is FormHelper type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.