BeetleJuice
BeetleJuice

Reputation: 40936

PHPDoc and PhpStorm: indicate the return type of a magic method

In general, this is how I get my IDE (PhpStorm 2017) to recognize the type of a variable that I don't get to formally declare:

/*
 * @var User $user
*/
foreach($users as $user){}

The IDE knows then that $user is of class User. Now I'm facing a situation with this line:

Yii::app()->user->login()

Specifically, the IDE has no idea what type user is. I get the warning:

Field accessed via magic method

So I tried to specify with:

/**
 * @var User Yii::app()->user
 */
Yii::app()->user->login();

But that doesn't make a difference. How can I help the IDE resolve the type?

Upvotes: 2

Views: 1354

Answers (2)

Dmitry
Dmitry

Reputation: 136

Create in project phpdoc.php file and put next:

<?php

/**
 * Class Yii
 * @method static CApplication app()
 */
class Yii extends YiiBase
{
}

/**
 * Class CApplication
 *
 * @property User $user
 */
class CApplication extends CModule
{
}

Upvotes: 6

Nima
Nima

Reputation: 3409

You need to assign Yii::app()->user (or any other magic property) to a new variable and tell your IDE about that variable. Like:

/**
* @var User $user
*/
$user = Yii::app()->user;
$user->login();

Upvotes: 3

Related Questions