From 0873d0745bfaeebab6acd0c06f3047994375a1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= <63721828+max-s-lab@users.noreply.github.com> Date: Tue, 8 Jul 2025 13:35:12 +0500 Subject: [PATCH] Fix #20434: Added PHPStan/Psalm annotations for `hasMany` and `hasOne` methods --- framework/CHANGELOG.md | 1 + framework/db/ActiveRecord.php | 39 ++++++++++++++++++++++++++++--- framework/db/BaseActiveRecord.php | 12 ++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6bfa12524f..326207dd7c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.54 under development ------------------------ +- Enh #20434: Added PHPStan/Psalm annotations for `hasMany` and `hasOne` methods (max-s-lab) - Enh #20433: Added PHPStan/Psalm annotations for some controllers methods: `beforeAction`, `afterAction` and `bindActionParams` (max-s-lab) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 3a0f367429..1df1b4de83 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -70,9 +70,6 @@ use yii\helpers\StringHelper; * * For more details and usage information on ActiveRecord, see the [guide article on ActiveRecord](guide:db-active-record). * - * @method ActiveQuery hasMany($class, array $link) See [[BaseActiveRecord::hasMany()]] for more info. - * @method ActiveQuery hasOne($class, array $link) See [[BaseActiveRecord::hasOne()]] for more info. - * * @author Qiang Xue * @author Carsten Brandt * @since 2.0 @@ -595,6 +592,42 @@ class ActiveRecord extends BaseActiveRecord } } + /** + * {@inheritdoc} + * + * @return ActiveQuery + * + * @template T + * + * @phpstan-param class-string $class + * @psalm-param class-string $class + * + * @phpstan-return ActiveQuery + * @psalm-return ActiveQuery + */ + public function hasMany($class, $link) + { + return parent::hasMany($class, $link); + } + + /** + * {@inheritdoc} + * + * @return ActiveQuery + * + * @template T + * + * @phpstan-param class-string $class + * @psalm-param class-string $class + * + * @phpstan-return ActiveQuery + * @psalm-return ActiveQuery + */ + public function hasOne($class, $link) + { + return parent::hasOne($class, $link); + } + /** * Inserts an ActiveRecord into DB without considering transaction. * @param array|null $attributes list of attributes that need to be saved. Defaults to `null`, diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 021f83a511..846734dde4 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -389,6 +389,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface * the attributes of the record associated with the `$class` model, while the values of the * array refer to the corresponding attributes in **this** AR class. * @return ActiveQueryInterface the relational query object. + * + * @phpstan-param class-string $class + * @psalm-param class-string $class + * + * @phpstan-param array $link + * @psalm-param array $link */ public function hasOne($class, $link) { @@ -424,6 +430,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface * the attributes of the record associated with the `$class` model, while the values of the * array refer to the corresponding attributes in **this** AR class. * @return ActiveQueryInterface the relational query object. + * + * @phpstan-param class-string $class + * @psalm-param class-string $class + * + * @phpstan-param array $link + * @psalm-param array $link */ public function hasMany($class, $link) {