Skip to content

Commit

Permalink
Psalm 1 + Update StyleCI configuration (#34)
Browse files Browse the repository at this point in the history
* ItemsStorage.php

* AssignmentsStorage.php

* improve

* Apply fixes from StyleCI

* Update StyleCI config

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
vjik and StyleCIBot committed Mar 2, 2023
1 parent 875056b commit b795aa1
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Expand Up @@ -10,6 +10,10 @@ indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.php]
ij_php_space_before_short_closure_left_parenthesis = false
ij_php_space_after_type_cast = true

[*.md]
trim_trailing_whitespace = false

Expand Down
16 changes: 7 additions & 9 deletions .styleci.yml
@@ -1,20 +1,12 @@
preset: psr12
risky: true

version: 8
version: 8.1

finder:
exclude:
- docs
- vendor
- resources
- views
- public
- templates
not-name:
- UnionCar.php
- TimerUnionTypes.php
- schema1.php

enabled:
- alpha_ordered_traits
Expand Down Expand Up @@ -86,3 +78,9 @@ enabled:
- trailing_comma_in_multiline_array
- unalign_double_arrow
- unalign_equals
- empty_loop_body_braces
- integer_literal_case
- union_type_without_spaces

disabled:
- function_declaration
4 changes: 3 additions & 1 deletion psalm.xml
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
58 changes: 43 additions & 15 deletions src/AssignmentsStorage.php
Expand Up @@ -11,6 +11,13 @@
use Yiisoft\Rbac\Assignment;
use Yiisoft\Rbac\AssignmentsStorageInterface;

/**
* @psalm-type RawAssignment = array{
* itemName: string,
* userId: string,
* createdAt: int|string,
* }
*/
final class AssignmentsStorage implements AssignmentsStorageInterface
{
private DatabaseInterface $database;
Expand All @@ -30,12 +37,18 @@ public function __construct(
*/
public function getAll(): array
{
/** @psalm-var RawAssignment[] $rows */
$rows = $this->database
->select()
->from($this->tableName)
->fetchAll();

$assignments = [];
foreach ($this->database->select()->from($this->tableName)->fetchAll() as $item) {
$assignments[$item['userId']][$item['itemName']] = new Assignment(
$item['userId'],
$item['itemName'],
(int)$item['createdAt']
foreach ($rows as $row) {
$assignments[$row['userId']][$row['itemName']] = new Assignment(
$row['userId'],
$row['itemName'],
(int) $row['createdAt']
);
}

Expand All @@ -47,13 +60,18 @@ public function getAll(): array
*/
public function getByUserId(string $userId): array
{
$assignments = $this->database->select()->from($this->tableName)->where(['userId' => $userId])->fetchAll();
/** @psalm-var RawAssignment[] $rows */
$rows = $this->database
->select()
->from($this->tableName)
->where(['userId' => $userId])
->fetchAll();

return array_combine(
array_column($assignments, 'itemName'),
array_column($rows, 'itemName'),
array_map(
static fn (array $item) => new Assignment($userId, $item['itemName'], (int)$item['createdAt']),
$assignments
static fn(array $row) => new Assignment($userId, $row['itemName'], (int) $row['createdAt']),
$rows
)
);
}
Expand All @@ -63,14 +81,15 @@ public function getByUserId(string $userId): array
*/
public function get(string $itemName, string $userId): ?Assignment
{
$assignment = $this->database
/** @psalm-var RawAssignment|null $row */
$row = $this->database
->select()
->from($this->tableName)
->where(['itemName' => $itemName, 'userId' => $userId])
->run()
->fetch();

return empty($assignment) ? null : new Assignment($userId, $itemName, (int)$assignment['createdAt']);
return empty($row) ? null : new Assignment($userId, $itemName, (int) $row['createdAt']);
}

/**
Expand All @@ -95,6 +114,7 @@ public function add(string $itemName, string $userId): void
*/
public function hasItem(string $name): bool
{
/** @var mixed $result */
$result = $this
->database
->select([new Fragment('1')])
Expand All @@ -115,31 +135,39 @@ public function renameItem(string $oldName, string $newName): void
if ($oldName === $newName) {
return;
}
$this->database->update($this->tableName, ['itemName' => $newName], ['itemName' => $oldName])->run();
$this->database
->update($this->tableName, ['itemName' => $newName], ['itemName' => $oldName])
->run();
}

/**
* @inheritDoc
*/
public function remove(string $itemName, string $userId): void
{
$this->database->delete($this->tableName, ['itemName' => $itemName, 'userId' => $userId])->run();
$this->database
->delete($this->tableName, ['itemName' => $itemName, 'userId' => $userId])
->run();
}

/**
* @inheritDoc
*/
public function removeByUserId(string $userId): void
{
$this->database->delete($this->tableName, ['userId' => $userId])->run();
$this->database
->delete($this->tableName, ['userId' => $userId])
->run();
}

/**
* @inheritDoc
*/
public function removeByItemName(string $itemName): void
{
$this->database->delete($this->tableName, ['itemName' => $itemName])->run();
$this->database
->delete($this->tableName, ['itemName' => $itemName])
->run();
}

/**
Expand Down

0 comments on commit b795aa1

Please sign in to comment.