Yii2 And Phpstorm

If I create a component, such as ‘MyComponent’ and set it in ‘config/main.php’, how can I do to complete the PhpStorm ‘\yii::$app->myComponent’ and their attributes and methods?

Thanks.

Not sure how you can do it directly, but this is a possible workaround:




/** @var \app\components\MyComponent $myComponent */

$myComponent = Yii::$app->myComponent;



@var is the only way, otherwise you would have to edit the PHPDoc of the framework, which is bad practice. It can’t be done dynamically.

You can trick IDE:

  1. Exclude framework’s Yii.php from indexing.

  2. Create your own Yii.php and add some @property in its phpdoc.

  3. Profit!

It seems all app’s autocomplete logic is taken from basic/vendor/yiisoft/yii2/base/Application.php

Are you sure we can alter the autocomplete recommendations with just Yii.php ? Shouldn’t you do the trick with Application instead?

Can you give us a small example?

Thanks.

Yes, I’m sure:




<?php

/**

 * Yii bootstrap file.

 *

 * @link http://www.yiiframework.com/

 * @copyright Copyright (c) 2008 Yii Software LLC

 * @license http://www.yiiframework.com/license/

 */


require(__DIR__ . '/BaseYii.php');


/**

 * Yii is a helper class serving common framework functionalities.

 *

 * It extends from [[\yii\BaseYii]] which provides the actual implementation.

 * By writing your own Yii class, you can customize some functionalities of [[\yii\BaseYii]].

 *

 * @author Qiang Xue <qiang.xue@gmail.com>

 * @since 2.0

 */

class Yii extends \yii\BaseYii

{

    /**

     * @var \yii\web\Application the application instance

     */

    public static $app;

}


spl_autoload_register(['Yii', 'autoload'], true, true);

Yii::$classMap = include(__DIR__ . '/classes.php');

Yii::$container = new yii\di\Container;



Samdark, thanks. Does the example above treats custom Application’s components? In other words, will it help in AndDan,s case?

I also have a question - how do you gracefully trick ide to autocomplete in views?

I do it like $this=…\View. Then it starts to autocomplete. I do my work then remove the line. I believe there is a better option.

Nope. For custom application components you have to add @property declarations to the class phpdoc. For views see what’s generated by Gii ;)

Tebazil, in your view, add something like:




<?php

/* @var $this yii\web\View */

/* @var $object app\common\Object */

?>



Now PHPStorm knows that $object it is an Object.

Now I do not understand again. Can you tell the benefits of the above? $app autocompletion works out of the box, it seems.

You’re telling IDE that Yii::$app is web application not just any application. Some IDEs do recognize when you’re using two or more types for variable other don’t. I think recent PhpStorm builds are able to deal with it.

check this link out:

yii2-cookbook.readthedocs.org/ide-autocompletion/

How do I implement this for 1.1 without adding


 @property

to CApplication.php?

I’ve created another CDbComponent, say $db2, in config/main.php and would like to get auto-complete for


Yii::app()->db2->...

I’ve created stub generator for components autocomplete, you can find it on packagist bazilio/yii2-stubs-generator (i can’t post links yet). Feel free to leave feedback\feature request on github.

Hello everybody,

I’m starting my first yii2 app atm, so I’m newbie here, but I have very serious knowledge in yii1 yet, because we still use yii1 at work.

My problem is related to the class type hinting in ActiveRecord Classes.

In yii1 I had a template for generating ar classes, which added me some of the following @method declarations into the class phpdoc:




/**

 * (...)

 * @method User findByPk(mixed $pk, mixed $condition='', array $params=array ( ))

 * (...)

 */

class User extends CActiveRecord

{ 

    (...)

    /**

     * @return User

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }

    (...)

}






$users = User::model()

    ->findAll($criteria);


foreach ($users as $user) {

    // PHPStorm knows here that $user is a User instance without doing any further doctyping

}



So the huge benefit here was in PHPStorm to have full type hinting support for method params and return values for all the inherited ar methods even in foreach loops without using @var docblocks in every second line of your code.

Now the problem with yii2 I’m facing with is the fact, that while AR classes don’t implement the find*-methods itself, but using ActiveQuery instead I don’t see any solution for doctyping the AR classes to get the same comfortably integration of yii2 into PHPStorm as with yii1.

Example:




$user = User::find()

    ->where(['email' => $email])

    ->one();



So even if I doctyping the one method in the class phpdoc of the User class (like in yii1 AR classes), PHPStorm thinks I’m dealing with a (null|array|ActiveRecord) type in the $user variable.

The only possibility I see is to use @var doctypes here, what is very uncomfortably.

Am I overseeing any other possibility? Help & suggestions would be very appreciated.

Thanks,

Christian

https://plugins.jetbrains.com/plugin/9388-yii2-support help a lot.

1 Like