Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: laminas coding standard 2.2.0 #12

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.phpcs-cache
/.phpunit.result.cache
/clover.xml
/coveralls-upload.json
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"psr/http-message": "^1.0.1"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"laminas/laminas-coding-standard": "~2.2.0",
"laminas/laminas-servicemanager": "^3.3",
"phpspec/prophecy-phpunit": "^2.0",
"phpspec/prophecy": "^1.12",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.3"
},
"conflict": {
Expand Down
373 changes: 300 additions & 73 deletions composer.lock

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Laminas Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
</ruleset>
10 changes: 6 additions & 4 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

namespace Mezzio\Authorization\Rbac;

use Zend\Expressive\Authorization\Rbac\ZendRbac;

class ConfigProvider
{
public function __invoke() : array
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

public function getDependencies() : array
public function getDependencies(): array
{
return [
// Legacy Zend Framework aliases
'aliases' => [
\Zend\Expressive\Authorization\Rbac\ZendRbac::class => LaminasRbac::class,
'aliases' => [
ZendRbac::class => LaminasRbac::class,
],
'factories' => [
LaminasRbac::class => LaminasRbacFactory::class,
Expand Down
14 changes: 5 additions & 9 deletions src/LaminasRbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@

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

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

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

Expand All @@ -36,7 +32,7 @@ public function __construct(Rbac $rbac, LaminasRbacAssertionInterface $assertion
*
* @throws Exception\RuntimeException
*/
public function isGranted(string $role, ServerRequestInterface $request) : bool
public function isGranted(string $role, ServerRequestInterface $request): bool
{
$routeResult = $request->getAttribute(RouteResult::class, false);
if (false === $routeResult) {
Expand Down
2 changes: 1 addition & 1 deletion src/LaminasRbacAssertionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface LaminasRbacAssertionInterface extends AssertionInterface
{
public function setRequest(ServerRequestInterface $request) : void;
public function setRequest(ServerRequestInterface $request): void;
}
11 changes: 6 additions & 5 deletions src/LaminasRbacFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mezzio\Authorization\AuthorizationInterface;
use Mezzio\Authorization\Exception;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface;

use function sprintf;

Expand All @@ -17,7 +18,7 @@ class LaminasRbacFactory
/**
* @throws Exception\InvalidConfigException
*/
public function __invoke(ContainerInterface $container) : AuthorizationInterface
public function __invoke(ContainerInterface $container): AuthorizationInterface
{
$config = $container->get('config')['mezzio-authorization-rbac'] ?? null;
if (null === $config) {
Expand Down Expand Up @@ -45,8 +46,8 @@ public function __invoke(ContainerInterface $container) : AuthorizationInterface

$assertion = $container->has(LaminasRbacAssertionInterface::class)
? $container->get(LaminasRbacAssertionInterface::class)
: ($container->has(\Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface::class)
? $container->get(\Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface::class)
: ($container->has(ZendRbacAssertionInterface::class)
? $container->get(ZendRbacAssertionInterface::class)
: null);

return new LaminasRbac($rbac, $assertion);
Expand All @@ -55,7 +56,7 @@ public function __invoke(ContainerInterface $container) : AuthorizationInterface
/**
* @throws Exception\InvalidConfigException
*/
private function injectRoles(Rbac $rbac, array $roles) : void
private function injectRoles(Rbac $rbac, array $roles): void
{
$rbac->setCreateMissingRoles(true);

Expand All @@ -72,7 +73,7 @@ private function injectRoles(Rbac $rbac, array $roles) : void
/**
* @throws Exception\InvalidConfigException
*/
private function injectPermissions(Rbac $rbac, array $specification) : void
private function injectPermissions(Rbac $rbac, array $specification): void
{
foreach ($specification as $role => $permissions) {
foreach ($permissions as $permission) {
Expand Down
10 changes: 5 additions & 5 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class ConfigProviderTest extends TestCase
/** @var ConfigProvider */
private $provider;

protected function setUp() : void
protected function setUp(): void
{
$this->provider = new ConfigProvider();
}

public function testInvocationReturnsArray()
public function testInvocationReturnsArray(): array
{
$config = ($this->provider)();
$this->assertIsArray($config);
Expand Down Expand Up @@ -56,14 +56,14 @@ public function testServicesDefinedInConfigProvider()
foreach ($json['packages'] as $package) {
if (isset($package['extra']['laminas']['config-provider'])) {
$configProvider = new $package['extra']['laminas']['config-provider']();
$config = array_merge_recursive($config, $configProvider());
$config = array_merge_recursive($config, $configProvider());
}
}

$config['dependencies']['services']['config'] = [
'mezzio-authorization-rbac' => ['roles' => [], 'permissions' => []],
];
$container = $this->getContainer($config['dependencies']);
$container = $this->getContainer($config['dependencies']);

$dependencies = $this->provider->getDependencies();
foreach ($dependencies['factories'] as $name => $factory) {
Expand All @@ -75,7 +75,7 @@ public function testServicesDefinedInConfigProvider()
}
}

private function getContainer(array $dependencies) : ServiceManager
private function getContainer(array $dependencies): ServiceManager
{
return new ServiceManager($dependencies);
}
Expand Down
57 changes: 29 additions & 28 deletions test/LaminasRbacFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface;

class LaminasRbacFactoryTest extends TestCase
{
Expand All @@ -20,7 +21,7 @@ class LaminasRbacFactoryTest extends TestCase
/** @var ContainerInterface|ObjectProphecy */
private $container;

protected function setUp() : void
protected function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
}
Expand Down Expand Up @@ -51,8 +52,8 @@ public function testFactoryWithoutPermissions()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => []
]
'roles' => [],
],
]);

$factory = new LaminasRbacFactory();
Expand All @@ -66,14 +67,14 @@ public function testFactoryWithEmptyRolesPermissionsWithoutAssertion()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => [],
'permissions' => []
]
'roles' => [],
'permissions' => [],
],
]);
$this->container->has(LaminasRbacAssertionInterface::class)->willReturn(false);
$this->container->has(\Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface::class)->willReturn(false);
$this->container->has(ZendRbacAssertionInterface::class)->willReturn(false);

$factory = new LaminasRbacFactory();
$factory = new LaminasRbacFactory();
$laminasRbac = $factory($this->container->reveal());
$this->assertInstanceOf(LaminasRbac::class, $laminasRbac);
}
Expand All @@ -82,16 +83,16 @@ public function testFactoryWithEmptyRolesPermissionsWithAssertion()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => [],
'permissions' => []
]
'roles' => [],
'permissions' => [],
],
]);

$assertion = $this->prophesize(LaminasRbacAssertionInterface::class);
$this->container->has(LaminasRbacAssertionInterface::class)->willReturn(true);
$this->container->get(LaminasRbacAssertionInterface::class)->willReturn($assertion->reveal());

$factory = new LaminasRbacFactory();
$factory = new LaminasRbacFactory();
$laminasRbac = $factory($this->container->reveal());
$this->assertInstanceOf(LaminasRbac::class, $laminasRbac);
}
Expand All @@ -100,17 +101,17 @@ public function testFactoryWithoutAssertion()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => [
'roles' => [
'administrator' => [],
'editor' => ['administrator'],
'contributor' => ['editor'],
],
'permissions' => [
'contributor' => [
'contributor' => [
'admin.dashboard',
'admin.posts',
],
'editor' => [
'editor' => [
'admin.publish',
],
'administrator' => [
Expand All @@ -120,9 +121,9 @@ public function testFactoryWithoutAssertion()
],
]);
$this->container->has(LaminasRbacAssertionInterface::class)->willReturn(false);
$this->container->has(\Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface::class)->willReturn(false);
$this->container->has(ZendRbacAssertionInterface::class)->willReturn(false);

$factory = new LaminasRbacFactory();
$factory = new LaminasRbacFactory();
$laminasRbac = $factory($this->container->reveal());
$this->assertInstanceOf(LaminasRbac::class, $laminasRbac);
}
Expand All @@ -131,17 +132,17 @@ public function testFactoryWithAssertion()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => [
'roles' => [
'administrator' => [],
'editor' => ['administrator'],
'contributor' => ['editor'],
],
'permissions' => [
'contributor' => [
'contributor' => [
'admin.dashboard',
'admin.posts',
],
'editor' => [
'editor' => [
'admin.publish',
],
'administrator' => [
Expand All @@ -154,7 +155,7 @@ public function testFactoryWithAssertion()
$this->container->has(LaminasRbacAssertionInterface::class)->willReturn(true);
$this->container->get(LaminasRbacAssertionInterface::class)->willReturn($assertion->reveal());

$factory = new LaminasRbacFactory();
$factory = new LaminasRbacFactory();
$laminasRbac = $factory($this->container->reveal());
$this->assertInstanceOf(LaminasRbac::class, $laminasRbac);
}
Expand All @@ -163,14 +164,14 @@ public function testFactoryWithInvalidRole()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => [
'roles' => [
1 => [],
],
'permissions' => [],
],
]);
$this->container->has(LaminasRbacAssertionInterface::class)->willReturn(false);
$this->container->has(\Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface::class)->willReturn(false);
$this->container->has(ZendRbacAssertionInterface::class)->willReturn(false);

$factory = new LaminasRbacFactory();

Expand All @@ -182,19 +183,19 @@ public function testFactoryWithUnknownRole()
{
$this->container->get('config')->willReturn([
'mezzio-authorization-rbac' => [
'roles' => [
'roles' => [
'administrator' => [],
],
'permissions' => [
'contributor' => [
'admin.dashboard',
'admin.posts',
]
]
]
],
],
],
]);
$this->container->has(LaminasRbacAssertionInterface::class)->willReturn(false);
$this->container->has(\Zend\Expressive\Authorization\Rbac\ZendRbacAssertionInterface::class)->willReturn(false);
$this->container->has(ZendRbacAssertionInterface::class)->willReturn(false);

$factory = new LaminasRbacFactory();

Expand Down
4 changes: 2 additions & 2 deletions test/LaminasRbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class LaminasRbacTest extends TestCase
/** @var LaminasRbacAssertionInterface|ObjectProphecy */
private $assertion;

protected function setUp() : void
protected function setUp(): void
{
$this->rbac = $this->prophesize(Rbac::class);
$this->rbac = $this->prophesize(Rbac::class);
$this->assertion = $this->prophesize(LaminasRbacAssertionInterface::class);
}

Expand Down