Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compatibility of item's type with Yii 2 #271

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
abstract class Item
{
public const TYPE_ROLE = 'role';
public const TYPE_PERMISSION = 'permission';
public const TYPE_ROLE = 1;

Check failure on line 13 in src/Item.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MissingClassConstType

src/Item.php:13:18: MissingClassConstType: Class constant "Yiisoft\Rbac\Item::TYPE_ROLE" should have a declared type. (see https://psalm.dev/359)

Check failure on line 13 in src/Item.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MissingClassConstType

src/Item.php:13:18: MissingClassConstType: Class constant "Yiisoft\Rbac\Item::TYPE_ROLE" should have a declared type. (see https://psalm.dev/359)
public const TYPE_PERMISSION = 2;

Check failure on line 14 in src/Item.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MissingClassConstType

src/Item.php:14:18: MissingClassConstType: Class constant "Yiisoft\Rbac\Item::TYPE_PERMISSION" should have a declared type. (see https://psalm.dev/359)

Check failure on line 14 in src/Item.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MissingClassConstType

src/Item.php:14:18: MissingClassConstType: Class constant "Yiisoft\Rbac\Item::TYPE_PERMISSION" should have a declared type. (see https://psalm.dev/359)

/**
* @var string The item description.
Expand Down Expand Up @@ -41,9 +41,10 @@
}

/**
* @return string Type of the item.
* @return int Type of the item.
* @psalm-return Item::TYPE_*
*/
abstract public function getType(): string;
abstract public function getType(): int;

/**
* @return string Authorization item name.
Expand Down Expand Up @@ -139,7 +140,7 @@
* name: string,
* description: string,
* rule_name: string|null,
* type: string,
* type: Item::TYPE_*,
* updated_at: int|null,
* created_at: int|null,
* }
Expand Down
2 changes: 1 addition & 1 deletion src/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Permission extends Item
{
public function getType(): string
public function getType(): int
{
return self::TYPE_PERMISSION;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Role extends Item
{
public function getType(): string
public function getType(): int
{
return self::TYPE_ROLE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private function updateItemName(string $name, Item $item): void
*
* @psalm-return ($type is Item::TYPE_PERMISSION ? array<string, Permission> : array<string, Role>)
*/
private function getItemsByType(string $type): array
private function getItemsByType(int $type): array
{
return array_filter(
$this->getAll(),
Expand All @@ -250,7 +250,7 @@ private function getItemsByType(string $type): array
/**
* @psalm-param Item::TYPE_* $type
*/
private function removeItemsByType(string $type): void
private function removeItemsByType(int $type): void
{
foreach ($this->getItemsByType($type) as $item) {
$this->remove($item->getName());
Expand Down
5 changes: 3 additions & 2 deletions tests/Common/ManagerLogicTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Yiisoft\Rbac\Exception\ItemAlreadyExistsException;
use Yiisoft\Rbac\Exception\RuleInterfaceNotImplementedException;
use Yiisoft\Rbac\Exception\RuleNotFoundException;
use Yiisoft\Rbac\Item;
use Yiisoft\Rbac\Permission;
use Yiisoft\Rbac\Role;
use Yiisoft\Rbac\RuleInterface;
Expand Down Expand Up @@ -727,7 +728,7 @@ public function testAddRole(): void
'name' => 'new role',
'description' => 'new role description',
'rule_name' => TrueRule::class,
'type' => 'role',
'type' => Item::TYPE_ROLE,
'updated_at' => 1_642_026_148,
'created_at' => 1_642_026_147,
],
Expand Down Expand Up @@ -792,7 +793,7 @@ public function testAddPermission(): void
'name' => 'edit post',
'description' => 'edit a post',
'rule_name' => null,
'type' => 'permission',
'type' => Item::TYPE_PERMISSION,
'updated_at' => 1_642_026_148,
'created_at' => 1_642_026_147,
],
Expand Down
Loading