Skip to content

Commit

Permalink
Merge pull request #1207: Apply Spiral Code Style
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 24, 2025
1 parent 9485a3c commit 753e40c
Show file tree
Hide file tree
Showing 29 changed files with 155 additions and 213 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
],
"require": {
"php": ">=8.1",
"spiral/core": "^3.14.10",
"spiral/hmvc": "^3.14.10"
"spiral/core": "^3.15",
"spiral/hmvc": "^3.15"
},
"autoload": {
"psr-4": {
"Spiral\\Security\\": "src"
}
},
"require-dev": {
"spiral/console": "^3.14.10",
"spiral/console": "^3.15",
"phpunit/phpunit": "^10.1",
"mockery/mockery": "^1.5",
"vimeo/psalm": "^5.9"
Expand Down
5 changes: 2 additions & 3 deletions src/Actor/Actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
class Actor implements ActorInterface
{
public function __construct(
private readonly array $roles
) {
}
private readonly array $roles,
) {}

public function getRoles(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Actor/Guest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ final class Guest implements ActorInterface

public function getRoles(): array
{
return [static::ROLE];
return [self::ROLE];
}
}
4 changes: 1 addition & 3 deletions src/Command/RolePermissionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ class RolePermissionsCommand extends Command
{
protected const NAME = 'security:role:permissions';
protected const DESCRIPTION = 'Get Role(s) Permissions';

protected const ARGUMENTS = [
['role', InputArgument::OPTIONAL, 'Role to get permissions'],
];

private const TABLE_HEADERS = ['role', 'permission', 'rule', 'allowed'];

/**
Expand All @@ -41,7 +39,7 @@ protected function perform(PermissionsInterface $rbac): int
/** @noinspection SlowArrayOperationsInLoopInspection */
$rows = \array_merge(
$this->getRolePermissions($role, $rbac),
$rows
$rows,
);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/GuardException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Generic RBAC exception.
*/
class GuardException extends SecurityException
{
}
class GuardException extends SecurityException {}
4 changes: 1 addition & 3 deletions src/Exception/PermissionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* When permission format is invalid or permission does not exists at moment of removal.
*/
class PermissionException extends GuardException
{
}
class PermissionException extends GuardException {}
4 changes: 1 addition & 3 deletions src/Exception/RoleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Exceptions relation to role management.
*/
class RoleException extends GuardException
{
}
class RoleException extends GuardException {}
4 changes: 1 addition & 3 deletions src/Exception/RuleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Rule specific exception.
*/
class RuleException extends GuardException
{
}
class RuleException extends GuardException {}
4 changes: 1 addition & 3 deletions src/Exception/SecurityException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Security\Exception;

class SecurityException extends \RuntimeException
{
}
class SecurityException extends \RuntimeException {}
5 changes: 2 additions & 3 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ final class Guard implements GuardInterface
public function __construct(
private readonly PermissionsInterface $permissions,
private ?ActorInterface $actor = null,
private array $roles = []
) {
}
private array $roles = [],
) {}

public function allows(string $permission, array $context = []): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function matches(string $string, string $pattern): bool
return match (true) {
$string === $pattern => true,
!$this->isPattern($pattern) => false,
default => (bool) \preg_match($this->getRegex($pattern), $string)
default => (bool) \preg_match($this->getRegex($pattern), $string),
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/PermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ final class PermissionManager implements PermissionsInterface
* Roles associated with their permissions.
*/
private array $permissions = [];

private readonly Matcher $matcher;

public function __construct(
private readonly RulesInterface $rules,
private readonly string $defaultRule = ForbidRule::class
private readonly string $defaultRule = ForbidRule::class,
) {
$this->matcher = new Matcher();
}
Expand Down Expand Up @@ -117,7 +118,6 @@ public function deassociate(string $role, string $permission): PermissionManager
}

/**
*
* @throws PermissionException
*/
private function findRule(string $role, string $permission): string
Expand Down
2 changes: 1 addition & 1 deletion src/PermissionsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public function getRule(string $role, string $permission): RuleInterface;
public function associate(
string $role,
string $permission,
string $rule = 'Spiral\Security\Rules\AllowRule'
string $rule = 'Spiral\Security\Rules\AllowRule',
);
}
5 changes: 2 additions & 3 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ abstract class Rule implements RuleInterface
];

public function __construct(
protected ResolverInterface $resolver
) {
}
protected ResolverInterface $resolver,
) {}

/**
* @throws RuleException
Expand Down
5 changes: 2 additions & 3 deletions src/Rule/CompositeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ abstract class CompositeRule implements RuleInterface
protected const RULES = [];

public function __construct(
private readonly RulesInterface $repository
) {
}
private readonly RulesInterface $repository,
) {}

public function allows(ActorInterface $actor, string $permission, array $context): bool
{
Expand Down
9 changes: 4 additions & 5 deletions src/RuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ final class RuleManager implements RulesInterface
private array $rules = [];

public function __construct(
private readonly ContainerInterface $container
) {
}
private readonly ContainerInterface $container,
) {}

public function set(string $name, mixed $rule = null): RuleManager
{
Expand Down Expand Up @@ -55,7 +54,7 @@ public function has(string $name): bool
// We are allowing to use class names without direct registration
\class_exists($name) => true,
// Relying on container binding
default => $this->container->has($name)
default => $this->container->has($name),
};
}

Expand Down Expand Up @@ -84,7 +83,7 @@ public function get(string $name): RuleInterface
throw new RuleException(\sprintf(
"Rule '%s' must point to RuleInterface, '%s' given",
$name,
!empty($rule) ? $rule::class : 'null'
!empty($rule) ? $rule::class : 'null',
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/GuardedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getGuard(): GuardInterface
$container = ContainerScope::getContainer();
if (empty($container) || !$container->has(GuardInterface::class)) {
throw new ScopeException(
'Unable to get `GuardInterface`, binding is missing or container scope is not set'
'Unable to get `GuardInterface`, binding is missing or container scope is not set',
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Actors/ActorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function testGetRoles(): void
/** @var ActorInterface $actor */
$actor = new Actor($roles);

$this->assertEquals($roles, $actor->getRoles());
self::assertEquals($roles, $actor->getRoles());
}
}
2 changes: 1 addition & 1 deletion tests/Actors/GuestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function testGetRoles(): void
/** @var ActorInterface $actor */
$actor = new Guest();

$this->assertEquals([Guest::ROLE], $actor->getRoles());
self::assertEquals([Guest::ROLE], $actor->getRoles());
}
}
2 changes: 1 addition & 1 deletion tests/Actors/NullActorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function testGetRoles(): void
/** @var ActorInterface $actor */
$actor = new NullActor();

$this->assertEquals([], $actor->getRoles());
self::assertEquals([], $actor->getRoles());
}
}
42 changes: 16 additions & 26 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Tests\Security;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Spiral\Security\ActorInterface;
use Spiral\Security\Exception\GuardException;
Expand All @@ -16,26 +17,9 @@ class GuardTest extends TestCase
public const OPERATION = 'test';
public const CONTEXT = [];

/**
* @var \PHPUnit_Framework_MockObject_MockObject|PermissionsInterface
*/
private $permission;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface
*/
private $actor;

/**
* @var array
*/
private $roles = ['user', 'admin'];

public function setUp(): void
{
$this->permission = $this->createMock(PermissionsInterface::class);
$this->actor = $this->createMock(ActorInterface::class);
}
private MockObject&PermissionsInterface $permission;
private MockObject&ActorInterface $actor;
private array $roles = ['user', 'admin'];

public function testAllows(): void
{
Expand All @@ -61,15 +45,15 @@ public function testAllows(): void
->willReturn($rule);

$guard = new Guard($this->permission, $this->actor, $this->roles);
$this->assertTrue($guard->allows(static::OPERATION, static::CONTEXT));
self::assertTrue($guard->allows(static::OPERATION, static::CONTEXT));
}

public function testAllowsPermissionsHasNoRole(): void
{
$this->permission->method('hasRole')->with($this->anything())->willReturn(false);

$guard = new Guard($this->permission, $this->actor, $this->roles);
$this->assertFalse($guard->allows(static::OPERATION, static::CONTEXT));
self::assertFalse($guard->allows(static::OPERATION, static::CONTEXT));
}

public function testAllowsNoActor(): void
Expand All @@ -85,16 +69,22 @@ public function testWithActor(): void
$guard = new Guard($this->permission);
$guardWithActor = $guard->withActor($this->actor);

$this->assertEquals($this->actor, $guardWithActor->getActor());
$this->assertNotEquals($guard, $guardWithActor);
self::assertEquals($this->actor, $guardWithActor->getActor());
self::assertNotEquals($guard, $guardWithActor);
}

public function testWithRoles(): void
{
$guard = new Guard($this->permission, $this->actor);
$guardWithRoles = $guard->withRoles($this->roles);

$this->assertEquals($this->roles, $guardWithRoles->getRoles());
$this->assertNotEquals($guard, $guardWithRoles);
self::assertEquals($this->roles, $guardWithRoles->getRoles());
self::assertNotEquals($guard, $guardWithRoles);
}

protected function setUp(): void
{
$this->permission = $this->createMock(PermissionsInterface::class);
$this->actor = $this->createMock(ActorInterface::class);
}
}
Loading

0 comments on commit 753e40c

Please sign in to comment.