From df400b6a1958fcb8fb2f69eb67ca2a7b601fa8df Mon Sep 17 00:00:00 2001 From: Maksim Spirkov <63721828+mspirkov@users.noreply.github.com> Date: Mon, 13 Oct 2025 10:04:49 +0300 Subject: [PATCH] Fix #20591: Add PHPStan/Psalm annotations for `yii\rbac\BaseManager::getItems()` --- framework/CHANGELOG.md | 1 + framework/rbac/BaseManager.php | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e373c443f7..01a8ca968e 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -49,6 +49,7 @@ Yii Framework 2 Change Log - Bug #20583: Fix return value in `Request::getServerPort` (mspirkov) - Bug #20587: Fix `@var` annotation for `yii\rbac\Item::$ruleName` (mspirkov) - Bug #20589: Fix `@var` annotations for `yii\rbac\DbManager` properties (mspirkov) +- Enh #20591: Add PHPStan/Psalm annotations for `yii\rbac\BaseManager::getItems()` (mspirkov) - Bug #20594: Fix `@return` annotation for `Instance::get()` (mspirkov) - Bug #20595: Fix `@return` annotation for `BaseHtml::getAttributeValue()` (mspirkov) diff --git a/framework/rbac/BaseManager.php b/framework/rbac/BaseManager.php index 36daef1c6a..29ae0b3b1d 100644 --- a/framework/rbac/BaseManager.php +++ b/framework/rbac/BaseManager.php @@ -44,6 +44,12 @@ abstract class BaseManager extends Component implements ManagerInterface * Returns the items of the specified type. * @param int $type the auth item type (either [[Item::TYPE_ROLE]] or [[Item::TYPE_PERMISSION]] * @return Item[] the auth items of the specified type. + * + * @phpstan-param Item::TYPE_ROLE|Item::TYPE_PERMISSION $type + * @psalm-param Item::TYPE_ROLE|Item::TYPE_PERMISSION $type + * + * @phpstan-return ($type is Item::TYPE_ROLE ? Role[] : Permission[]) + * @psalm-return ($type is Item::TYPE_ROLE ? Role[] : Permission[]) */ abstract protected function getItems($type); @@ -177,7 +183,12 @@ abstract class BaseManager extends Component implements ManagerInterface public function getRole($name) { $item = $this->getItem($name); - return $item instanceof Item && $item->type == Item::TYPE_ROLE ? $item : null; + if ($item instanceof Item && $item->type == Item::TYPE_ROLE) { + /** @var Role $item */ + return $item; + } + + return null; } /** @@ -186,7 +197,12 @@ abstract class BaseManager extends Component implements ManagerInterface public function getPermission($name) { $item = $this->getItem($name); - return $item instanceof Item && $item->type == Item::TYPE_PERMISSION ? $item : null; + if ($item instanceof Item && $item->type == Item::TYPE_PERMISSION) { + /** @var Permission $item */ + return $item; + } + + return null; } /**