0

Simplified code:

Model:

/**
* @return array{name:string,age:int,group:int}
*/

public function fetchStudent($id){
   //fetch $id student from db and return array
   (...)
}

My controller:

$students = $myStudentsModel->fetchStudent(123);

Now in the controller when I try to use code completion in $students variable, the returned array structure is automatically suggested as defined in PHPDoc.

However, when I finally pass it to a view:

return $this->render('student', [
   'student' => $student
]);

in the view file the $student variable knows nothing about the array structure it knew when in controller.

I tried different PHPDoc tags (e.g. uses), changing array structure to [#ArrayShape] attribute, pulling params in the view from $this->context - nothing works. PhpStorm 'loses track' of a variable once passed from controller to view.

Do you have any idea how to pass docs alongside variables to views?

9
  • 2
    I usually just declare passed variables in php doc somewhere at the start of view template. For example like this * @var array{...} $student. To get passed variables in view templates automatically you will probably need some plugin. I don't know about any plugin that would be able to do that. Commented Apr 20, 2023 at 11:06
  • Thanks! I was thinking about it but the issue I see is that whenever you edit the returned array from your model method, all the places in views where you used the fetched data need to be updated as well. Editing the source signature would be less cumbersome and error prone. Commented Apr 20, 2023 at 11:24
  • 2
    @Joey you can always use some object instead of array as return value from that method. Then you won't need to update definitions in templates when its structure is changed. And using object is even safer because it can prevent states where array structure was updated but it's definition in method's doc block wasn't. Commented Apr 20, 2023 at 11:39
  • @LazyOne looks like I was expecting too much of IDE but since PhpStorm (+Yii plugin) already does know that views have parameters missing (not pushed) and raises an error in the controller the feature I was hoping to find seemed perfectly reasonable.. Commented Apr 20, 2023 at 11:59
  • 1
    LazyOne yes, I thought the same - the plugin would be the most reasonable place to seek such an option. I've just been to the 'Yii2 Support' plugin author's website to see if there's any mention of the feature, silent for now alas.. Michal & LazyOne As to returning objects the problem is my model method returns an array of them. I can just decide to return an array of objects or array of arrays (each table row). Guess I'll try using one of your hints - a custom class or a declaration at the top of the view file. Thanks guys! Commented Apr 20, 2023 at 12:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.