Skip to content

Commit

Permalink
Various fixes (#72)
Browse files Browse the repository at this point in the history
* Various fixes

* Apply fixes from StyleCI

* Max length, trailing slashes, static

* Revert static

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Alexey Rogachev <arogachev90@gmail.com>
  • Loading branch information
3 people committed Feb 26, 2024
1 parent 4b98e73 commit 4b7ca7b
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 43 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -38,7 +38,7 @@ Detailed build statuses:
- [Microsoft SQL Server](https://github.com/yiisoft/db-mssql)
- [Oracle](https://github.com/yiisoft/db-oracle)
- `PDO` PHP extension for the selected driver.
- In case of using with SQL Server, minimal required version of PDO is 5.11.1.
- In the case of using with SQL Server, a minimal required version of PDO is 5.11.1.

## Installation

Expand Down Expand Up @@ -76,7 +76,7 @@ More comprehensive examples can be found at
### Working with migrations

This package uses [Yii DB Migration](https://github.com/yiisoft/db-migration) for managing database tables required for
storages. There are 3 tables in total (`yii_rbac_` prefix is used).
storages. There are three tables in total (`yii_rbac_` prefix is used).

Items storage:

Expand Down Expand Up @@ -127,8 +127,8 @@ return [
];
```

Because item and assignment storages are completely indepedent, migrations are separated as well in order to prevent
creation of unused tables. So, for example, if you only want to use assignment storage, add only
Because item and assignment storages are completely independent, migrations are separated as well to prevent the
creation of unused tables. So, for example, if you only want to use assignment storage, add only
[migrations/assignments](./migrations/assignments) to source paths.

Other ways of using migrations are covered [here](https://github.com/yiisoft/db-migration#usage).
Expand Down Expand Up @@ -175,17 +175,17 @@ $manager = new TransactionalManagerDecorator(
new Manager(
itemsStorage: $itemsStorage,
assignmentsStorage: $assignmentsStorage,
// Requires https://github.com/yiisoft/rbac-rules-container or other compatible factory.
// Requires https://github.com/yiisoft/rbac-rules-container or another compatible factory.
ruleFactory: $rulesContainer,
),
);
$manager->addPermission(new Permission('posts.create'));
```

> Note wrapping manager with decorator - it additionally provides database transactions to guarantee data integrity.
> Note wrapping manager with decoratorit additionally provides database transactions to guarantee data integrity.
> Note that it's not necessary to use both DB storages. Combining different implementations is possible. A quite popular
> case is to manage items via [PHP files](https://github.com/yiisoft/rbac-php) while store assignments in database.
> case is to manage items via [PHP files](https://github.com/yiisoft/rbac-php) while storing assignments in a database.
More examples can be found in [Yii RBAC](https://github.com/yiisoft/rbac) documentation.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -14,7 +14,7 @@
"issues": "https://github.com/yiisoft/rbac-db/issues?state=open",
"forum": "https://www.yiiframework.com/forum/",
"wiki": "https://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"irc": "ircs://irc.libera.chat:6697/yii",
"chat": "https://t.me/yii3en",
"source": "https://github.com/yiisoft/rbac-db"
},
Expand Down
4 changes: 2 additions & 2 deletions src/AssignmentsStorage.php
Expand Up @@ -29,8 +29,8 @@ final class AssignmentsStorage implements AssignmentsStorageInterface
* @psalm-param non-empty-string $tableName
*/
public function __construct(
private ConnectionInterface $database,
private string $tableName = 'yii_rbac_assignment',
private readonly ConnectionInterface $database,
private readonly string $tableName = 'yii_rbac_assignment',
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/SeparatorCollisionException.php
Expand Up @@ -10,7 +10,7 @@

class SeparatorCollisionException extends RuntimeException implements FriendlyExceptionInterface
{
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
public function __construct(int $code = 0, ?Throwable $previous = null)
{
parent::__construct('Separator collision has been detected.', $code, $previous);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ItemTreeTraversal/CteItemTreeTraversal.php
Expand Up @@ -14,8 +14,8 @@

/**
* A RBAC item tree traversal strategy based on CTE (common table expression). Uses `WITH` expression to form a
* recursive query. The base queries are unified as much possible to work for all RDBMS supported by Yii Database with
* minimal differences.
* recursive query. The base queries are unified as much as possible to work for all RDBMS supported by Yii Database
* with minimal differences.
*
* @internal
*
Expand Down Expand Up @@ -61,7 +61,7 @@ public function getHierarchy(string $name): array
->innerJoin('parent_of', [
'item_child_recursive.child' => new Expression('{{parent_of}}.[[child_name]]'),
]);
/** @infection-ignore-all FalseValuem, union */
/** @infection-ignore-all FalseValue, union */
$cteSelectItemQuery = (new Query($this->database))
->select(['name', new Expression($this->getEmptyChildrenExpression())])
->from($this->tableName)
Expand Down
7 changes: 4 additions & 3 deletions src/ItemTreeTraversal/ItemTreeTraversalFactory.php
Expand Up @@ -28,8 +28,9 @@ class ItemTreeTraversalFactory
* @param string $namesSeparator Separator used for joining item names.
* @psalm-param non-empty-string $namesSeparator
*
* @throws RuntimeException When a database was configured with unknown driver, either because it is not supported
* by Yii Database out of the box or newly added by Yii Database and not supported / tested yet in this package.
* @throws RuntimeException When a database was configured with an unknown driver, either because it is not
* supported by Yii Database out of the box or newly added by Yii Database and not supported / tested yet in this
* package.
* @return ItemTreeTraversalInterface Item tree traversal strategy.
*/
public static function getItemTreeTraversal(
Expand All @@ -41,7 +42,7 @@ public static function getItemTreeTraversal(
$arguments = [$database, $tableName, $childrenTableName, $namesSeparator];
$driver = $database->getDriverName();

// default - ignored due to a complexity of testing and preventing splitting of database argument.
// default - ignored due to the complexity of testing and preventing splitting of database argument.
// @codeCoverageIgnoreStart
return match ($driver) {
'sqlite' => new SqliteCteItemTreeTraversal(...$arguments),
Expand Down
4 changes: 2 additions & 2 deletions src/ItemTreeTraversal/ItemTreeTraversalInterface.php
Expand Up @@ -81,12 +81,12 @@ public function getChildPermissionRows(string|array $names): array;
public function getChildRoleRows(string|array $names): array;

/**
* Whether a selected parent has specific child.
* Whether a selected parent has a specific child.
*
* @param string $parentName Parent item name.
* @param string $childName Child item name.
*
* @return bool Whether a selected parent has specific child.
* @return bool Whether a selected parent has a specific child.
*/
public function hasChild(string $parentName, string $childName): bool;
}
2 changes: 1 addition & 1 deletion src/ItemTreeTraversal/MssqlCteItemTreeTraversal.php
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Rbac\Db\ItemTreeTraversal;

/**
* A RBAC item tree traversal strategy based on CTE (common table expression) for SQL Server.
* An RBAC item tree traversal strategy based on CTE (common table expression) for SQL Server.
*
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ItemTreeTraversal/MysqlItemTreeTraversal.php
Expand Up @@ -13,7 +13,7 @@
use Yiisoft\Rbac\Item;

/**
* A RBAC item tree traversal strategy based on specific functionality for MySQL 5, without support for CTE (Common
* An RBAC item tree traversal strategy based on specific functionality for MySQL 5, without support for CTE (Common
* Table Expressions).
*
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/ItemTreeTraversal/OracleCteItemTreeTraversal.php
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Rbac\Db\ItemTreeTraversal;

/**
* A RBAC item tree traversal strategy based on CTE (common table expression) for Oracle.
* An RBAC item tree traversal strategy based on CTE (common table expression) for Oracle.
*
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ItemTreeTraversal/PostgresCteItemTreeTraversal.php
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Rbac\Db\ItemTreeTraversal;

/**
* A RBAC item tree traversal strategy based on CTE (common table expression) for PostgreSQL.
* An RBAC item tree traversal strategy based on CTE (common table expression) for PostgreSQL.
*
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ItemTreeTraversal/SqliteCteItemTreeTraversal.php
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Rbac\Db\ItemTreeTraversal;

/**
* A RBAC item tree traversal strategy based on CTE (common table expression) for SQLite. Should be used only with
* An RBAC item tree traversal strategy based on CTE (common table expression) for SQLite. Should be used only with
* versions >= 3.8.3 (lower versions don't support this functionality).
*
* @internal
Expand Down
28 changes: 14 additions & 14 deletions src/ItemsStorage.php
Expand Up @@ -54,7 +54,7 @@ final class ItemsStorage implements ItemsStorageInterface
* @var string Separator used for joining and splitting item names.
* @psalm-var non-empty-string
*/
private string $namesSeparator = ',';
private string $namesSeparator;
/**
* @var ItemTreeTraversalInterface|null Lazily created RBAC item tree traversal strategy.
*/
Expand All @@ -73,9 +73,9 @@ final class ItemsStorage implements ItemsStorageInterface
* @param string $namesSeparator Separator used for joining and splitting item names.
*/
public function __construct(
private ConnectionInterface $database,
private string $tableName = 'yii_rbac_item',
private string $childrenTableName = 'yii_rbac_item_child',
private readonly ConnectionInterface $database,
private readonly string $tableName = 'yii_rbac_item',
private readonly string $childrenTableName = 'yii_rbac_item_child',
string $namesSeparator = ',',
) {
$this->assertNamesSeparator($namesSeparator);
Expand Down Expand Up @@ -419,7 +419,7 @@ public function removeChildren(string $parentName): void
}

/**
* Gets either all existing roles or permissions, depending on specified type.
* Gets either all existing roles or permissions, depending on a specified type.
*
* @param string $type Either {@see Item::TYPE_ROLE} or {@see Item::TYPE_PERMISSION}.
* @psalm-param Item::TYPE_* $type
Expand All @@ -439,13 +439,13 @@ private function getItemsByType(string $type): array
}

/**
* Gets single item by its type and name.
* Gets a single item by its type and name.
*
* @param string $type Either {@see Item::TYPE_ROLE} or {@see Item::TYPE_PERMISSION}.
* @psalm-param Item::TYPE_* $type
*
* @return Permission|Role|null Either role or permission, depending on initial type specified. `null` is returned
* when no item was found by given condition.
* @return Permission|Role|null Either role or permission, depending on an initial type specified. `null` is
* returned when no item was found by given condition.
* @psalm-return ($type is Item::TYPE_PERMISSION ? Permission : Role)|null
*/
private function getItemByTypeAndName(string $type, string $name): Permission|Role|null
Expand All @@ -468,7 +468,7 @@ private function getItemByTypeAndName(string $type, string $name): Permission|Ro
*
* @psalm-param RawPermission|RawRole $rawItem
*
* @return Permission|Role Either role or permission, depending on initial type specified.
* @return Permission|Role Either role or permission, depending on an initial type specified.
*/
private function createItem(array $rawItem): Permission|Role
{
Expand All @@ -489,12 +489,12 @@ private function createItem(array $rawItem): Permission|Role
}

/**
* A basic factory method for creating single item with name only.
* A basic factory method for creating a single item with name only.
*
* @param string $type Either {@see Item::TYPE_ROLE} or {@see Item::TYPE_PERMISSION}.
* @psalm-param Item::TYPE_* $type
*
* @return Permission|Role Either role or permission, depending on initial type specified.
* @return Permission|Role Either role or permission, depending on an initial type specified.
* @psalm-return ($type is Item::TYPE_PERMISSION ? Permission : Role)
*/
private function createItemByTypeAndName(string $type, string $name): Permission|Role
Expand All @@ -517,7 +517,7 @@ private function removeRelatedItemsChildren(ConnectionInterface $database, strin
}

/**
* Removes all existing items of specified type.
* Removes all existing items of a specified type.
*
* @param string $type Either {@see Item::TYPE_ROLE} or {@see Item::TYPE_PERMISSION}.
* @psalm-param Item::TYPE_* $type
Expand Down Expand Up @@ -567,8 +567,8 @@ private function clearItemsByType(string $type): void
}

/**
* Creates RBAC item tree traversal strategy and returns it. In case it was already created, just retrieves
* previously saved instance.
* Creates RBAC item tree traversal strategy and returns it.
* In case it was already created, it just retrieves previously saved instance.
*/
private function getTreeTraversal(): ItemTreeTraversalInterface
{
Expand Down
6 changes: 4 additions & 2 deletions src/TransactionalManagerDecorator.php
Expand Up @@ -13,8 +13,10 @@

final class TransactionalManagerDecorator implements ManagerInterface
{
public function __construct(private ManagerInterface $manager, private ConnectionInterface $database)
{
public function __construct(
private readonly ManagerInterface $manager,
private readonly ConnectionInterface $database,
) {
}

public function userHasPermission(
Expand Down
2 changes: 1 addition & 1 deletion tests/Base/ItemsStorageTest.php
Expand Up @@ -21,7 +21,7 @@ abstract class ItemsStorageTest extends TestCase
setUp as protected traitSetUp;
tearDown as protected traitTearDown;
testClear as protected traitTestClear;
dataRemove as protected traitDataRemove;
dataRemove as public traitDataRemove;
testRemove as protected traitTestRemove;
testClearPermissions as protected traitTestClearPermissions;
testClearRoles as protected traitTestClearRoles;
Expand Down
4 changes: 2 additions & 2 deletions tests/Common/Exception/SeparatorCollisionExceptionTest.php
Expand Up @@ -11,13 +11,13 @@ final class SeparatorCollisionExceptionTest extends TestCase
{
public function testGetCode(): void
{
$exception = new SeparatorCollisionException('test');
$exception = new SeparatorCollisionException();
$this->assertSame(0, $exception->getCode());
}

public function testReturnTypes(): void
{
$exception = new SeparatorCollisionException('test');
$exception = new SeparatorCollisionException();
$this->assertIsString($exception->getName());
$this->assertIsString($exception->getSolution());
}
Expand Down

0 comments on commit 4b7ca7b

Please sign in to comment.