From 6480f56a3ab8281f0be92b79df4d36610c5fd22b Mon Sep 17 00:00:00 2001 From: rotimi Date: Wed, 13 Mar 2024 17:26:11 -0600 Subject: [PATCH] PHP 8.1 minimum version prep --- .github/workflows/php.yml | 6 +-- composer.json | 4 +- phpunit.xml.dist | 12 +++--- rector.php | 10 +---- src/GenericPermission.php | 7 +--- src/GenericPermissionableEntity.php | 2 +- src/GenericPermissionsCollection.php | 5 +-- src/VersatileAcl.php | 38 ++++++++----------- src/interfaces/PermissionInterface.php | 4 +- .../PermissionsCollectionInterface.php | 2 +- 10 files changed, 36 insertions(+), 54 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index e057000..2f2e122 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,17 +16,13 @@ jobs: strategy: matrix: - php: [8.3, 8.2, 8.1, 8.0, 7.4] + php: [8.3, 8.2, 8.1] # prefer-lowest is causing unit tests to fail when php 7.2 is run against PHPunit 7.x, # PHPUnit 8.x is the latest stable release that supports PHP 7.2 and that runs fine # dependency-version: [prefer-lowest, prefer-stable] dependency-version: [prefer-stable] os: [ubuntu-20.04, ubuntu-22.04] include: - - os: ubuntu-20.04 - php: 7.4 - - os: ubuntu-20.04 - php: 8.0 - os: ubuntu-20.04 php: 8.1 - os: ubuntu-22.04 diff --git a/composer.json b/composer.json index 8fe747f..8ad7470 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,10 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": ">=7.4.0" + "php": ">=8.1.0" }, "require-dev": { - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "^10.0", "php-coveralls/php-coveralls": "^2.0", "vimeo/psalm": "^5.4", "rector/rector": "^1.0.0" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4baf441..f085ba9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,10 +1,6 @@ - + - - ./src - - @@ -18,4 +14,10 @@ + + + ./src + + + diff --git a/rector.php b/rector.php index c1688ca..9842a2b 100644 --- a/rector.php +++ b/rector.php @@ -23,10 +23,9 @@ $rectorConfigurator->import(SetList::PHP_72); $rectorConfigurator->import(SetList::PHP_73); $rectorConfigurator->import(SetList::PHP_74); - //$containerConfigurator->import(SetList::PHP_80); - //$containerConfigurator->import(SetList::PHP_81); + $rectorConfigurator->import(SetList::PHP_80); + $rectorConfigurator->import(SetList::PHP_81); $rectorConfigurator->import(SetList::DEAD_CODE); - //$rectorConfigurator->import(SetList::PSR_4); $rectorConfigurator->import(SetList::TYPE_DECLARATION); $skipables = [ @@ -36,10 +35,5 @@ \Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector::class, ]; - if(PHP_MAJOR_VERSION < 8) { - - $skipables[] = \Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector::class; - } - $rectorConfigurator->skip($skipables); }; diff --git a/src/GenericPermission.php b/src/GenericPermission.php index 8f0f0da..0c7507c 100644 --- a/src/GenericPermission.php +++ b/src/GenericPermission.php @@ -17,8 +17,6 @@ class GenericPermission implements PermissionInterface { protected string $resource = ''; - protected bool $allowActionOnResource = true; - /** * * @var callable|null @@ -40,11 +38,10 @@ class GenericPermission implements PermissionInterface { * You must add default values for each parameter the callback accepts. * @param mixed ...$argsForCallback zero or more arguments to be used to invoke $additionalAssertions */ - public function __construct(string $action, string $resource, bool $allowActionOnResource = true, callable $additionalAssertions = null, ...$argsForCallback) { + public function __construct(string $action, string $resource, protected bool $allowActionOnResource = true, callable $additionalAssertions = null, mixed ...$argsForCallback) { $this->action = Utils::strToLower($action); $this->resource = Utils::strToLower($resource); - $this->allowActionOnResource = $allowActionOnResource; $this->additionalAssertions = $additionalAssertions; $this->argsForCallback = $argsForCallback; } @@ -158,7 +155,7 @@ public function setAllowActionOnResource(bool $allowActionOnResource): Permissio * * @return bool return true if an instance of this class signifies that a specified action can be performed on a specified resource, or false otherwise */ - public function isAllowed(string $action, string $resource, callable $additionalAssertions = null, ...$argsForCallback): bool { + public function isAllowed(string $action, string $resource, callable $additionalAssertions = null, mixed ...$argsForCallback): bool { if( $additionalAssertions === null && $this->additionalAssertions !== null ) { diff --git a/src/GenericPermissionableEntity.php b/src/GenericPermissionableEntity.php index 743f479..4306ff6 100644 --- a/src/GenericPermissionableEntity.php +++ b/src/GenericPermissionableEntity.php @@ -40,7 +40,7 @@ public function __construct(string $id, PermissionsCollectionInterface $perms = if( $trimmedId === '' ) { throw new EmptyEntityIdException( - "An instance of " . get_class($this) + "An instance of " . static::class . " cannot be created with an empty `Id`." ); } diff --git a/src/GenericPermissionsCollection.php b/src/GenericPermissionsCollection.php index 08d2f35..a312125 100644 --- a/src/GenericPermissionsCollection.php +++ b/src/GenericPermissionsCollection.php @@ -6,7 +6,6 @@ use VersatileAcl\Interfaces\PermissionInterface; use VersatileAcl\Interfaces\PermissionsCollectionInterface; use function array_key_exists; -use function get_class; use function uasort; /** @@ -64,13 +63,13 @@ public function hasPermission(PermissionInterface $permission): bool { * * @return bool return true if one or more items in an instance of this class signifies that a specified action can be performed on a specified resource, or false otherwise */ - public function isAllowed(string $action, string $resource, callable $additionalAssertions = null, ...$argsForCallback): bool { + public function isAllowed(string $action, string $resource, callable $additionalAssertions = null, mixed ...$argsForCallback): bool { /** @var PermissionInterface $permission */ foreach ($this->storage as $permission) { /** @var PermissionInterface $permissionClass */ - $permissionClass = get_class($permission); // for late static binding + $permissionClass = $permission::class; // for late static binding if( ( diff --git a/src/VersatileAcl.php b/src/VersatileAcl.php index a9da5f1..72b4f3d 100644 --- a/src/VersatileAcl.php +++ b/src/VersatileAcl.php @@ -16,7 +16,6 @@ use function array_key_exists; use function array_shift; use function func_get_args; -use function get_class; use function gettype; use function is_object; use function is_subclass_of; @@ -108,28 +107,28 @@ public function __construct( if(!is_subclass_of($permissionableEntityInterfaceClassName, PermissionableEntityInterface::class)) { $this->throwInvalidArgExceptionDueToWrongClassName( - get_class($this), __FUNCTION__, $permissionableEntityInterfaceClassName, PermissionableEntityInterface::class, 'first' + static::class, __FUNCTION__, $permissionableEntityInterfaceClassName, PermissionableEntityInterface::class, 'first' ); } if(!is_subclass_of($permissionInterfaceClassName, PermissionInterface::class)) { $this->throwInvalidArgExceptionDueToWrongClassName( - get_class($this), __FUNCTION__, $permissionInterfaceClassName, PermissionInterface::class, 'second' + static::class, __FUNCTION__, $permissionInterfaceClassName, PermissionInterface::class, 'second' ); } if(!is_subclass_of($permissionableEntitiesCollectionInterfaceClassName, PermissionableEntitiesCollectionInterface::class)) { $this->throwInvalidArgExceptionDueToWrongClassName( - get_class($this), __FUNCTION__, $permissionableEntitiesCollectionInterfaceClassName, PermissionableEntitiesCollectionInterface::class, 'third' + static::class, __FUNCTION__, $permissionableEntitiesCollectionInterfaceClassName, PermissionableEntitiesCollectionInterface::class, 'third' ); } if(!is_subclass_of($permissionsCollectionInterfaceClassName, PermissionsCollectionInterface::class)) { $this->throwInvalidArgExceptionDueToWrongClassName( - get_class($this), __FUNCTION__, $permissionsCollectionInterfaceClassName, PermissionsCollectionInterface::class, 'fourth' + static::class, __FUNCTION__, $permissionsCollectionInterfaceClassName, PermissionsCollectionInterface::class, 'fourth' ); } @@ -164,10 +163,10 @@ public function addEntity(string $entityId): self { $this->auditActivities && $this->logActivity( - "Initialized " . get_class($this) + "Initialized " . static::class . '::entitiesCollection to a new empty instance of ' - . get_class($this->entitiesCollection), - "Initialized " . get_class($this) . '::entitiesCollection' + . $this->entitiesCollection::class, + "Initialized " . static::class . '::entitiesCollection' ); } @@ -285,7 +284,7 @@ public function addParentEntity(string $entityId, string $parentEntityId): self // We should never really get here in most cases. // Something weird happened, we could not create or retrieve // the entity to which a parent is to be added - $class = get_class($this); + $class = static::class; $function = __FUNCTION__; $msg = "Error [{$class}::{$function}(...)]:" . " Could not create or retrieve the entity with an ID of `{$entityId}`" @@ -413,13 +412,12 @@ public function getAllEntities() : ?PermissionableEntitiesCollectionInterface { } /** - * Add a permission to an entity with the ID value of $entityId. + * Add a permission to an entity with the ID value of $entityId. * This entity will be created and added to the instance of this class upon * which this method is being invoked if the entity does not exist. * * @param string $entityId the ID of the entity to which the permission is to be added * @param callable|null $additionalAssertions - * @param mixed $argsForCallback * @noinspection PhpUnhandledExceptionInspection * @see PermissionInterface::__construct($action, $resource, $allowActionOnResource, $additionalAssertions, ...$argsForCallback) * for definitions of all but the first parameter @@ -432,7 +430,7 @@ public function addPermission( string $resource, bool $allowActionOnResource = true, callable $additionalAssertions = null, - ...$argsForCallback + mixed ...$argsForCallback ): self { $this->auditActivities @@ -493,7 +491,7 @@ public function addPermission( // We should never really get here in most cases. Something weird happened, // we could not create or retrieve the entity to which a parent is to be added - $class = get_class($this); + $class = static::class; $function = __FUNCTION__; $msg = "Error [{$class}::{$function}(...)]:" . " Could not create or retrieve the entity with an ID of `{$entityId}`" @@ -522,7 +520,6 @@ public function addPermission( * or return null if either the entity and / or permission do not exist. * * @param callable|null $additionalAssertions - * @param mixed $argsForCallback * * @noinspection PhpUnhandledExceptionInspection * @see PermissionInterface::__construct($action, $resource, $allowActionOnResource, $additionalAssertions, $argsForCallback) @@ -536,7 +533,7 @@ public function removePermission( string $resource, bool $allowActionOnResource = true, callable $additionalAssertions = null, - ...$argsForCallback + mixed ...$argsForCallback ): ?PermissionInterface { $this->auditActivities @@ -630,7 +627,7 @@ public function removePermission( * @noinspection PhpUnhandledExceptionInspection * @noinspection PhpDocMissingThrowsInspection */ - public function isAllowed(string $entityId, string $action, string $resource, callable $additionalAssertions = null, ...$argsForCallback): bool { + public function isAllowed(string $entityId, string $action, string $resource, callable $additionalAssertions = null, mixed ...$argsForCallback): bool { $this->auditActivities && ++$this->numTabsForIndentingAudit // increment on first call to logActivity within a method @@ -824,12 +821,11 @@ public function createEntity(string $entityId): PermissionableEntityInterface { * for definitions of all the parameters of this method * * @param callable|null $additionalAssertions - * @param mixed ...$argsForCallback * * @psalm-suppress MoreSpecificReturnType */ public function createPermission( - string $action, string $resource, bool $allowActionOnResource = true, callable $additionalAssertions = null, ...$argsForCallback + string $action, string $resource, bool $allowActionOnResource = true, callable $additionalAssertions = null, mixed ...$argsForCallback ): PermissionInterface { $permissionClassName = $this->permissionInterfaceClassName; @@ -976,11 +972,9 @@ protected function getMethodParameterNamesAndVals(string $methodName, array $par /** * Helper method to describe the value in $returnVal * - * @param mixed $returnVal - * * @return string description of the value in $returnVal */ - protected function formatReturnValueForAudit($returnVal): string { + protected function formatReturnValueForAudit(mixed $returnVal): string { $returnType = gettype($returnVal); $formattedSentence = @@ -991,7 +985,7 @@ protected function formatReturnValueForAudit($returnVal): string { $formattedSentence = " with a return type of `object` that is an instance of" - . " `" . get_class($returnVal) . "` with the following" + . " `" . $returnVal::class . "` with the following" . " string representation: " . PHP_EOL . diff --git a/src/interfaces/PermissionInterface.php b/src/interfaces/PermissionInterface.php index 30d5b54..426862e 100644 --- a/src/interfaces/PermissionInterface.php +++ b/src/interfaces/PermissionInterface.php @@ -17,7 +17,7 @@ interface PermissionInterface * @param callable|null $additionalAssertions an optional callback function that must return a boolean further indicating whether or not an action can be performed on the resource. * @param mixed ...$argsForCallback zero or more arguments to be used to invoke $additionalAssertions */ - public function __construct(string $action, string $resource, bool $allowActionOnResource=true, callable $additionalAssertions=null, ...$argsForCallback); + public function __construct(string $action, string $resource, bool $allowActionOnResource=true, callable $additionalAssertions=null, mixed ...$argsForCallback); /** * Create a new and empty collection that is meant to house one or more instances of PermissionInterface @@ -103,7 +103,7 @@ public function setAllowActionOnResource(bool $allowActionOnResource): self; * * @return bool return true if an instance of this interface signifies that a specified action can be performed on a specified resource, or false otherwise */ - public function isAllowed(string $action, string $resource, callable $additionalAssertions=null, ...$argsForCallback): bool; + public function isAllowed(string $action, string $resource, callable $additionalAssertions=null, mixed ...$argsForCallback): bool; /** * Checks whether the specified permission object has an equal value to the current instance. diff --git a/src/interfaces/PermissionsCollectionInterface.php b/src/interfaces/PermissionsCollectionInterface.php index 9e66b70..bcb7c3a 100644 --- a/src/interfaces/PermissionsCollectionInterface.php +++ b/src/interfaces/PermissionsCollectionInterface.php @@ -87,7 +87,7 @@ public function hasPermission(PermissionInterface $permission): bool; * * @return bool return true if one or more items in an instance of this interface signifies that a specified action can be performed on a specified resource, or false otherwise */ - public function isAllowed(string $action, string $resource, callable $additionalAssertions=null, ...$argsForCallback): bool; + public function isAllowed(string $action, string $resource, callable $additionalAssertions=null, mixed ...$argsForCallback): bool; /** * Sort the collection.