Skip to content

Commit

Permalink
Minor fixes and improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Dec 6, 2023
1 parent 713fdbf commit b884076
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@
- Bug #175: Use rule factory for creating rule instances in `CompositeRule` (@arogachev)
- Enh #202: Add `getRole()`, `getPermission()` and `hasChildren()` methods to `ManagerInterface` (@arogachev)
- Chg #202: Rename `$permissionName` parameter to `$name` in `ManagerInterface::removePermission()` method (@arogachev)
- Enh #203: Add `getByNames()` method to `ItemsStorageInterface` (@arogachev)
- Enh #203: Add `getByNames()` and `getAccessTree` methods to `ItemsStorageInterface` (@arogachev)
- Enh #203: Add `filterUserItemNames()` method to `AssignmentsStorageInterface` (@arogachev)
- Enh #203: Add `getItemsByUserId()` method to `ManagerInterface` (@arogachev)
- Bug #203: Remove duplicated code for checking permission in `Manager::userHasPermission()` (@arogachev)
- Bug #203: Execute rules for parent items and for guests in `Manager::userHasPermission()` (@arogachev)
- Bug #203: Do not limit child items by only direct ones for guests in `Manager::userHasPermission()` (@arogachev)
- Bug #203: Fix `Manager::getRolesByUserId()` to include child roles (@arogachev)
- Chg #203: Verify that every passed role name is a string in `Manager::setDefaultRoleNames()` (@arogachev)
- Enh #203: Add `getAccessTree()` method to `ItemsStorageInterface` (@arogachev)
- Enh #203: Add `getGuestRoleName()` and `getGuestRole()` methods to `Manager` (@arogachev)
- Chg #203: Throw `RuntimeException` in the case with implicit guest and non-existing guest role in
`Manager::userHasPermission()` (@arogachev)
- Chg #203: Throw `RuntimeException` in the case with implicit guest and non-existing guest role in
`Manager::userHasPermission()` (@arogachev)
- Enh #206: Optimize calls for getting child items within the loops (@arogachev)
- Chg #206: Rename `$name` argument to `$names` and allow array type for it in `getAllChildren()`, `getAllChildRoles()`,
`getAllChildPermissions()` methods of `ItemsStorageInterface` (@arogachev)
Expand Down
10 changes: 5 additions & 5 deletions src/ItemsStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* A storage for RBAC roles and permissions used in {@see Manager}.
*
* @psalm-type ItemsIndexedByName = array<string, Permission|Role>
* @psalm-type PermissionTree = non-empty-array<non-empty-string, array{
* @psalm-type AccessTree = non-empty-array<non-empty-string, array{
* item: Permission|Role,
* children: array<non-empty-string, Permission|Role>
* }>
Expand Down Expand Up @@ -173,7 +173,7 @@ public function getParents(string $name): array;
*
* @return array A mapping between parent names and according items with all their children (references to other
* parents found).
* @psalm-return PermissionTree
* @psalm-return AccessTree
*/
public function getAccessTree(string $name): array;

Expand All @@ -190,7 +190,7 @@ public function getDirectChildren(string $name): array;
/**
* Returns all child permissions and/or roles.
*
* @param array|string $names The parent name / names.
* @param string|string[] $names The parent name / names.
*
* @return array The child permissions and/or roles.
* @psalm-return ItemsIndexedByName
Expand All @@ -200,7 +200,7 @@ public function getAllChildren(string|array $names): array;
/**
* Returns all child roles.
*
* @param array|string $names The parent name / names.
* @param string|string[] $names The parent name / names.
*
* @return Role[] The child roles.
* @psalm-return array<string, Role>
Expand All @@ -210,7 +210,7 @@ public function getAllChildRoles(string|array $names): array;
/**
* Returns all child permissions.
*
* @param array|string $names The parent name / names.
* @param string|string[] $names The parent name / names.
*
* @return Permission[] The child permissions.
* @psalm-return array<string, Permission>
Expand Down
14 changes: 3 additions & 11 deletions tests/Support/FakeItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,32 +312,24 @@ private function fillChildrenRecursive(string $name, array &$result): void
}

/**
* @param Item[] $array
* @psalm-param array<string, Item> $array
*
* @return Role[]
* @psalm-return array<string, Role>
*/
private function filterRoles(array $array): array
private function filterRoles(array $items): array
{
return array_filter(
$this->getRoles(),
static fn (Role $roleItem): bool => array_key_exists($roleItem->getName(), $array),
static fn (Role $item): bool => array_key_exists($item->getName(), $items),
);
}

/**
* @param Item[] $items
* @psalm-param array<string, Item> $items
*
* @return Permission[]
* @psalm-return array<string, Permission>
*/
private function filterPermissions(array $items): array
{
return array_filter(
$this->getPermissions(),
static fn (Permission $permissionItem): bool => array_key_exists($permissionItem->getName(), $items),
static fn (Permission $item): bool => array_key_exists($item->getName(), $items),
);
}
}

0 comments on commit b884076

Please sign in to comment.