Skip to content

Commit

Permalink
Merge pull request #31 from gsteel/psalm-fixes
Browse files Browse the repository at this point in the history
Improve internal types
  • Loading branch information
Ocramius committed Apr 6, 2023
2 parents fb05968 + e7c3a03 commit 34b2cb5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
26 changes: 0 additions & 26 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.9.0@8b9ad1eb9e8b7d3101f949291da2b9f7767cd163">
<file src="src/LaminasRbac.php">
<MixedArgument>
<code>$routeName</code>
</MixedArgument>
<MixedAssignment>
<code>$routeName</code>
<code>$routeResult</code>
</MixedAssignment>
<MixedMethodCall>
<code>getMatchedRouteName</code>
<code>isFailure</code>
</MixedMethodCall>
<UndefinedInterfaceMethod>
<code>setRequest</code>
</UndefinedInterfaceMethod>
</file>
<file src="src/LaminasRbacAssertionInterface.php">
<PossiblyUnusedMethod>
<code>setRequest</code>
</PossiblyUnusedMethod>
</file>
<file src="src/LaminasRbacFactory.php">
<MixedArgument>
<code>$assertion</code>
Expand All @@ -33,13 +12,8 @@
<code>$role</code>
<code>$role</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$config['permissions']]]></code>
<code><![CDATA[$config['roles']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$assertion</code>
<code>$config</code>
<code>$parents</code>
<code>$permission</code>
<code>$permissions</code>
Expand Down
20 changes: 8 additions & 12 deletions src/LaminasRbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@

namespace Mezzio\Authorization\Rbac;

use Laminas\Permissions\Rbac\AssertionInterface;
use Laminas\Permissions\Rbac\Rbac;
use Mezzio\Authorization\AuthorizationInterface;
use Mezzio\Authorization\Exception;
use Mezzio\Router\RouteResult;
use Psr\Http\Message\ServerRequestInterface;

use function assert;
use function is_string;
use function sprintf;

class LaminasRbac implements AuthorizationInterface
{
/** @var Rbac */
private $rbac;

/** @var null|AssertionInterface */
private $assertion;

public function __construct(Rbac $rbac, ?LaminasRbacAssertionInterface $assertion = null)
{
$this->rbac = $rbac;
$this->assertion = $assertion;
public function __construct(
private Rbac $rbac,
private ?LaminasRbacAssertionInterface $assertion = null
) {
}

/**
Expand All @@ -35,7 +30,7 @@ public function __construct(Rbac $rbac, ?LaminasRbacAssertionInterface $assertio
public function isGranted(string $role, ServerRequestInterface $request): bool
{
$routeResult = $request->getAttribute(RouteResult::class, false);
if (false === $routeResult) {
if (! $routeResult instanceof RouteResult) {
throw new Exception\RuntimeException(sprintf(
'The %s attribute is missing in the request; cannot perform authorizations',
RouteResult::class
Expand All @@ -48,6 +43,7 @@ public function isGranted(string $role, ServerRequestInterface $request): bool
}

$routeName = $routeResult->getMatchedRouteName();
assert(is_string($routeName));
if (null !== $this->assertion) {
$this->assertion->setRequest($request);
}
Expand Down
4 changes: 3 additions & 1 deletion src/LaminasRbacFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Mezzio\Authorization\Rbac;

use ArrayAccess;
use Laminas\Permissions\Rbac\Exception\ExceptionInterface as RbacExceptionInterface;
use Laminas\Permissions\Rbac\Rbac;
use Mezzio\Authorization\AuthorizationInterface;
use Mezzio\Authorization\Exception;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface;

use function is_array;
use function sprintf;

class LaminasRbacFactory
Expand All @@ -21,7 +23,7 @@ class LaminasRbacFactory
public function __invoke(ContainerInterface $container): AuthorizationInterface
{
$config = $container->get('config')['mezzio-authorization-rbac'] ?? null;
if (null === $config) {
if (! is_array($config) && ! $config instanceof ArrayAccess) {
throw new Exception\InvalidConfigException(sprintf(
'Cannot create %s instance; no "mezzio-authorization-rbac" config key present',
LaminasRbac::class
Expand Down

0 comments on commit 34b2cb5

Please sign in to comment.