Skip to content

Commit

Permalink
Merge pull request #38 from samsonasik/apply-php80
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius authored Oct 17, 2022
2 parents a97a786 + e63af47 commit 0d88f43
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 53 deletions.
3 changes: 1 addition & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@
<code>$value</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="3">
<code>function ($aclArg, $roleArg, $resourceArg, $privilegeArg) use ($value) {</code>
<code>function ($value) {</code>
<code>static fn($value)</code>
<code>function () {</code>
</MissingClosureReturnType>
<TooManyArguments occurrences="1">
Expand Down
2 changes: 1 addition & 1 deletion src/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public function isAllowed($role = null, $resource = null, $privilege = null)
// query on all privileges
do {
// depth-first search on $role if it is not 'allRoles' pseudo-parent
if (null !== $role && null !== ($result = $this->roleDFSAllPrivileges($role, $resource, $privilege))) {
if (null !== $role && null !== ($result = $this->roleDFSAllPrivileges($role, $resource))) {
return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Assertion/AssertionAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function assert(
if ($manager = $this->getAssertionManager()) {
try {
$assertion = $manager->get($assertion);
} catch (Exception $e) {
} catch (Exception) {
throw new InvalidAssertionException(
'assertion "'
. $assertion
Expand Down
3 changes: 1 addition & 2 deletions src/Assertion/AssertionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ public function validate($instance)
*
* @deprecated Please use {@see AssertionManager::validate()} instead.
*
* @param mixed $instance
* @throws InvalidArgumentException
* @psalm-assert AssertionInterface $instance
*/
public function validatePlugin($instance)
public function validatePlugin(mixed $instance)
{
try {
$this->validate($instance);
Expand Down
26 changes: 6 additions & 20 deletions src/Assertion/ExpressionAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
use function preg_match;
use function property_exists;
use function sprintf;
use function str_contains;
use function str_replace;
use function strpos;
use function strtolower;
use function ucwords;

Expand Down Expand Up @@ -68,7 +68,7 @@ final class ExpressionAssertion implements AssertionInterface
public const OPERATOR_NSAME = '!==';

/** @var list<string> */
private static $validOperators = [
private static array $validOperators = [
self::OPERATOR_EQ,
self::OPERATOR_NEQ,
self::OPERATOR_LT,
Expand All @@ -83,15 +83,6 @@ final class ExpressionAssertion implements AssertionInterface
self::OPERATOR_NSAME,
];

/** @var mixed */
private $left;

/** @var string */
private $operator;

/** @var mixed */
private $right;

/**
* Constructor
*
Expand All @@ -102,11 +93,8 @@ final class ExpressionAssertion implements AssertionInterface
* @param string $operator One of the OPERATOR constants (or their values)
* @param mixed|array $right See the class description for valid values.
*/
private function __construct($left, $operator, $right)
private function __construct(private $left, private $operator, private $right)
{
$this->left = $left;
$this->operator = $operator;
$this->right = $right;
}

/**
Expand Down Expand Up @@ -247,7 +235,7 @@ private function resolveOperandValue($operand, array $context)

$contextProperty = $operand[self::OPERAND_CONTEXT_PROPERTY];

if (strpos($contextProperty, '.') !== false) { // property path?
if (str_contains($contextProperty, '.')) { // property path?
[$objectName, $objectField] = explode('.', $contextProperty, 2);
return $this->getObjectFieldValue($context, $objectName, $objectField);
}
Expand Down Expand Up @@ -282,7 +270,7 @@ private function getObjectFieldValue(array $context, $objectName, $field)

$object = $context[$objectName];
$accessors = ['get', 'is'];
$fieldAccessor = false === strpos($field, '_')
$fieldAccessor = ! str_contains($field, '_')
? $field
: str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));

Expand All @@ -306,13 +294,11 @@ private function getObjectFieldValue(array $context, $objectName, $field)
}

/**
* @param mixed $left
* @param string $operator
* @param mixed $right
* @return bool|void
* @throws RuntimeException If operand is not supported.
*/
private static function evaluateExpression($left, $operator, $right)
private static function evaluateExpression(mixed $left, $operator, mixed $right)
{
// phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators
switch ($operator) {
Expand Down
8 changes: 4 additions & 4 deletions src/Resource/GenericResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Laminas\Permissions\Acl\Resource;

class GenericResource implements ResourceInterface
use Stringable;

class GenericResource implements ResourceInterface, Stringable
{
/**
* Unique id of Resource
Expand Down Expand Up @@ -36,10 +38,8 @@ public function getResourceId()
/**
* Defined by ResourceInterface; returns the Resource identifier
* Proxies to getResourceId()
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->getResourceId();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Role/GenericRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Laminas\Permissions\Acl\Role;

class GenericRole implements RoleInterface
use Stringable;

class GenericRole implements RoleInterface, Stringable
{
/**
* Unique id of Role
Expand Down Expand Up @@ -36,10 +38,8 @@ public function getRoleId()
/**
* Defined by RoleInterface; returns the Role identifier
* Proxies to getRoleId()
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->getRoleId();
}
Expand Down
11 changes: 7 additions & 4 deletions test/AclTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function testRoleRegistryRemoveOne(): void
*/
public function testRoleRegistryRemoveOneNonExistent(): void
{
$this->expectException(InvalidArgumentException::class, 'not found');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('not found');
$this->acl->removeRole('nonexistent');
}

Expand Down Expand Up @@ -235,7 +236,8 @@ public function testRoleRegistryDuplicate(): void
{
$roleGuest = new GenericRole('guest');
$roleRegistry = new Role\Registry();
$this->expectException(InvalidArgumentException::class, 'already exists');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('already exists');
$roleRegistry
->add($roleGuest)
->add($roleGuest);
Expand All @@ -249,7 +251,8 @@ public function testRoleRegistryDuplicateId(): void
$roleGuest1 = new GenericRole('guest');
$roleGuest2 = new GenericRole('guest');
$roleRegistry = new Role\Registry();
$this->expectException(InvalidArgumentException::class, 'already exists');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('already exists');
$roleRegistry
->add($roleGuest1)
->add($roleGuest2);
Expand Down Expand Up @@ -1208,7 +1211,7 @@ public function testAclPassesPrivilegeToAssertClass(): void
$acl->addRole('role');
$acl->addResource('resource');
$acl->allow('role', null, null, $assertion);
$allowed = $acl->isAllowed('role', 'resource', 'privilege', $assertion);
$allowed = $acl->isAllowed('role', 'resource', 'privilege');

$this->assertTrue($allowed);
}
Expand Down
16 changes: 5 additions & 11 deletions test/Assertion/CallbackAssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class CallbackAssertionTest extends TestCase
*/
public function testConstructorThrowsExceptionIfNotCallable(): void
{
$this->expectException(
InvalidArgumentException::class,
'Invalid callback provided; not callable'
);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid callback provided; not callable');
new CallbackAssertion('I am not callable!');
}

Expand All @@ -30,7 +28,7 @@ public function testConstructorThrowsExceptionIfNotCallable(): void
*/
public function testCallbackIsSet(): void
{
$callback = function () {
$callback = static function (): void {
};
$assert = new CallbackAssertion($callback);
$this->assertSame($callback, $assert->peakCallback());
Expand All @@ -44,7 +42,7 @@ public function testAssertMethodPassArgsToCallback(): void
$acl = new Acl\Acl();
$that = $this;
$assert = new CallbackAssertion(
function ($aclArg, $roleArg, $resourceArg, $privilegeArg) use ($that, $acl) {
static function ($aclArg, $roleArg, $resourceArg, $privilegeArg) use ($that, $acl): bool {
$that->assertSame($acl, $aclArg);
$that->assertInstanceOf(RoleInterface::class, $roleArg);
$that->assertEquals('guest', $roleArg->getRoleId());
Expand All @@ -68,11 +66,7 @@ public function testAssertMethod(): void
{
$acl = new Acl\Acl();
$roleGuest = new Acl\Role\GenericRole('guest');
$assertMock = function ($value) {
return function ($aclArg, $roleArg, $resourceArg, $privilegeArg) use ($value) {
return $value;
};
};
$assertMock = static fn($value) => static fn($aclArg, $roleArg, $resourceArg, $privilegeArg) => $value;
$acl->addRole($roleGuest);
$acl->allow($roleGuest, null, 'somePrivilege', new CallbackAssertion($assertMock(true)));
$this->assertTrue($acl->isAllowed($roleGuest, null, 'somePrivilege'));
Expand Down
5 changes: 1 addition & 4 deletions test/TestAsset/MockAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

class MockAssertion implements Acl\Assertion\AssertionInterface
{
protected bool $returnValue;

public function __construct(bool $returnValue)
public function __construct(protected bool $returnValue)
{
$this->returnValue = $returnValue;
}

/** @inheritDoc */
Expand Down

0 comments on commit 0d88f43

Please sign in to comment.