Practically / PsalmPluginYii2

Psalm plugin for a better typed yii2 application
BSD 3-Clause "New" or "Revised" License
9 stars 6 forks source link

Adding `$db` into `one` and `all` methods of *Query stubs #13

Open aywan opened 2 years ago

aywan commented 2 years ago

Without this have troubles when inherit Query object. Don't know how write tests)

gwynbox commented 2 years ago

Please can you provide an example of the issue encountered without adding $db to one() and all()

aywan commented 1 year ago

I usually override ActiveRecord::find with concrete class. It useful for typing.

<?php
/**
 * @method array|Profile              one($db = null)
 * @method array|Profile[]            all($db = null)
 * @method array|\Generator|Profile[] each($batchSize = 100, $db = null)
 */
class ProfileActiveQuery extends ActiveQuery
{
    public function __construct(array $config = [])
    {
        parent::__construct(Profile::class, $config);
    }
}

For example when id do $profiles = Profile::find()->where(...)->all() - everyone tool know $profiles is Profile[] type.

Inside Yii2 sources - interface also has $db attribute in this methods https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveQueryInterface.php#L40