diff options
author | Jean Baptiste Noblot <noblot.jb@gmail.com> | 2020-05-27 00:09:50 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-27 00:09:50 +0300 |
commit | 588dd53be0cdb46389157e6859e944578b2b4771 (patch) | |
tree | 116fe534f93763e6a59eaf6895fc136eb1a3bc6e | |
parent | fae60dbc6b4f68025967ba1a29a3458486404bbf (diff) |
Typing Core/Access (#15983)
-rw-r--r-- | core/Access/CapabilitiesProvider.php | 38 | ||||
-rw-r--r-- | core/Access/Capability.php | 16 | ||||
-rw-r--r-- | core/Access/Role.php | 8 | ||||
-rw-r--r-- | core/Access/Role/Admin.php | 10 | ||||
-rw-r--r-- | core/Access/Role/View.php | 10 | ||||
-rw-r--r-- | core/Access/Role/Write.php | 12 | ||||
-rw-r--r-- | core/Access/RolesProvider.php | 16 | ||||
m--------- | plugins/TagManager | 0 | ||||
-rw-r--r-- | plugins/UsersManager/tests/Integration/APITest.php | 30 | ||||
-rw-r--r-- | plugins/UsersManager/tests/Integration/UsersManagerTest.php | 2 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/AccessTest.php | 10 |
11 files changed, 87 insertions, 65 deletions
diff --git a/core/Access/CapabilitiesProvider.php b/core/Access/CapabilitiesProvider.php index 066bb28834..e0693b7972 100644 --- a/core/Access/CapabilitiesProvider.php +++ b/core/Access/CapabilitiesProvider.php @@ -17,14 +17,15 @@ class CapabilitiesProvider { /** * @return Capability[] + * @throws Exception */ - public function getAllCapabilities() + public function getAllCapabilities(): array { $cacheId = CacheId::siteAware(CacheId::languageAware('Capabilities')); $cache = PiwikCache::getTransientCache(); if (!$cache->contains($cacheId)) { - $capabilities = array(); + $capabilities = []; /** * Triggered to add new capabilities. @@ -60,7 +61,7 @@ class CapabilitiesProvider */ Piwik::postEvent('Access.Capability.filterCapabilities', array(&$capabilities)); - $capabilities = array_values($capabilities); + $capabilities = \array_values($capabilities); $this->checkCapabilityIds($capabilities); @@ -74,17 +75,24 @@ class CapabilitiesProvider /** * @param $capabilityId * @return Capability|null + * @throws Exception */ - public function getCapability($capabilityId) + public function getCapability(string $capabilityId): ?Capability { foreach ($this->getAllCapabilities() as $capability) { if ($capabilityId === $capability->getId()) { return $capability; } } + + return null; } - public function getAllCapabilityIds() + /** + * @return string[] + * @throws Exception + */ + public function getAllCapabilityIds(): array { $ids = array(); foreach ($this->getAllCapabilities() as $capability) { @@ -93,25 +101,35 @@ class CapabilitiesProvider return $ids; } - public function isValidCapability($capabilityId) + /** + * @param $capabilityId + * @return bool + * @throws Exception + */ + public function isValidCapability($capabilityId): bool { $capabilities = $this->getAllCapabilityIds(); - return in_array($capabilityId, $capabilities, true); + return \in_array($capabilityId, $capabilities, true); } - public function checkValidCapability($capabilityId) + /** + * @param $capabilityId + * @throws Exception + */ + public function checkValidCapability($capabilityId): void { if (!$this->isValidCapability($capabilityId)) { $capabilities = $this->getAllCapabilityIds(); - throw new Exception(Piwik::translate("UsersManager_ExceptionAccessValues", implode(", ", $capabilities))); + throw new \Exception(Piwik::translate("UsersManager_ExceptionAccessValues", implode(", ", $capabilities))); } } /** * @param Capability[] $capabilities + * @throws Exception */ - private function checkCapabilityIds($capabilities) + private function checkCapabilityIds(array $capabilities): void { foreach ($capabilities as $capability) { $id = $capability->getId(); diff --git a/core/Access/Capability.php b/core/Access/Capability.php index 6840c1b2b5..9f5d19f95c 100644 --- a/core/Access/Capability.php +++ b/core/Access/Capability.php @@ -10,20 +10,20 @@ namespace Piwik\Access; abstract class Capability { - abstract public function getId(); - abstract public function getName(); - abstract public function getCategory(); - abstract public function getDescription(); - abstract public function getIncludedInRoles(); + abstract public function getId(): string; + abstract public function getName(): string; + abstract public function getCategory(): string; + abstract public function getDescription(): string; + abstract public function getIncludedInRoles(): array; - public function getHelpUrl() + public function getHelpUrl(): string { return ''; } - public function hasRoleCapability($idRole) + public function hasRoleCapability(string $idRole): bool { - return in_array($idRole, $this->getIncludedInRoles(), true); + return \in_array($idRole, $this->getIncludedInRoles(), true); } } diff --git a/core/Access/Role.php b/core/Access/Role.php index 86ed757c88..e0328afd24 100644 --- a/core/Access/Role.php +++ b/core/Access/Role.php @@ -10,11 +10,11 @@ namespace Piwik\Access; abstract class Role { - abstract public function getName(); - abstract public function getId(); - abstract public function getDescription(); + abstract public function getName(): string; + abstract public function getId(): string; + abstract public function getDescription(): string; - public function getHelpUrl() + public function getHelpUrl(): string { return ''; } diff --git a/core/Access/Role/Admin.php b/core/Access/Role/Admin.php index eddc7ba9de..3b23ee298a 100644 --- a/core/Access/Role/Admin.php +++ b/core/Access/Role/Admin.php @@ -13,26 +13,26 @@ use Piwik\Piwik; class Admin extends Role { - const ID = 'admin'; + public const ID = 'admin'; - public function getName() + public function getName(): string { return Piwik::translate('UsersManager_PrivAdmin'); } - public function getId() + public function getId(): string { return self::ID; } - public function getDescription() + public function getDescription(): string { return Piwik::translate('UsersManager_PrivAdminDescription', array( Piwik::translate('UsersManager_PrivWrite') )); } - public function getHelpUrl() + public function getHelpUrl(): string { return 'https://matomo.org/faq/general/faq_69/'; } diff --git a/core/Access/Role/View.php b/core/Access/Role/View.php index d295504cce..915272b6fa 100644 --- a/core/Access/Role/View.php +++ b/core/Access/Role/View.php @@ -13,24 +13,24 @@ use Piwik\Piwik; class View extends Role { - const ID = 'view'; + public const ID = 'view'; - public function getName() + public function getName(): string { return Piwik::translate('UsersManager_PrivView'); } - public function getId() + public function getId(): string { return self::ID; } - public function getDescription() + public function getDescription(): string { return Piwik::translate('UsersManager_PrivViewDescription'); } - public function getHelpUrl() + public function getHelpUrl(): string { return 'https://matomo.org/faq/general/faq_70/'; } diff --git a/core/Access/Role/Write.php b/core/Access/Role/Write.php index 0e7eacc7d9..0e4a1505f4 100644 --- a/core/Access/Role/Write.php +++ b/core/Access/Role/Write.php @@ -13,26 +13,26 @@ use Piwik\Piwik; class Write extends Role { - const ID = 'write'; + public const ID = 'write'; - public function getName() + public function getName(): string { return Piwik::translate('UsersManager_PrivWrite'); } - public function getId() + public function getId(): string { return self::ID; } - public function getDescription() + public function getDescription(): string { return Piwik::translate('UsersManager_PrivWriteDescription'); } - public function getHelpUrl() + public function getHelpUrl(): string { - return ''; + return 'https://matomo.org/faq/general/faq_26910'; } } diff --git a/core/Access/RolesProvider.php b/core/Access/RolesProvider.php index f9f171b06f..4295e9b713 100644 --- a/core/Access/RolesProvider.php +++ b/core/Access/RolesProvider.php @@ -19,7 +19,7 @@ class RolesProvider /** * @return Role[] */ - public function getAllRoles() + public function getAllRoles(): array { return array( new View(), @@ -33,9 +33,9 @@ class RolesProvider * Useful when a given API method requests a given access Level. * We first check that the required access level exists. * - * @return array + * @return string[] */ - public function getAllRoleIds() + public function getAllRoleIds(): array { $ids = array(); foreach ($this->getAllRoles() as $role) { @@ -44,14 +44,18 @@ class RolesProvider return $ids; } - public function isValidRole($roleId) + public function isValidRole(string $roleId): bool { $roles = $this->getAllRoleIds(); - return in_array($roleId, $roles, true); + return \in_array($roleId, $roles, true); } - public function checkValidRole($roleId) + /** + * @param $roleId + * @throws Exception + */ + public function checkValidRole(string $roleId): void { if (!$this->isValidRole($roleId)) { $roles = $this->getAllRoleIds(); diff --git a/plugins/TagManager b/plugins/TagManager -Subproject bb8cc3b28952b6f3a7891b5b10737f603d46374 +Subproject 78cfc26a0178d0dc899b553bbb36b1a65dcc027 diff --git a/plugins/UsersManager/tests/Integration/APITest.php b/plugins/UsersManager/tests/Integration/APITest.php index 5dfc9f207b..3150f154b9 100644 --- a/plugins/UsersManager/tests/Integration/APITest.php +++ b/plugins/UsersManager/tests/Integration/APITest.php @@ -31,27 +31,27 @@ class TestCap1 extends Capability { const ID = 'test_cap1'; - public function getId() + public function getId(): string { return self::ID; } - public function getCategory() + public function getCategory(): string { return 'Test'; } - public function getName() + public function getName(): string { return 'Cap1'; } - public function getDescription() + public function getDescription(): string { return ''; } - public function getIncludedInRoles() + public function getIncludedInRoles(): array { return array( Admin::ID @@ -63,27 +63,27 @@ class TestCap2 extends Capability { const ID = 'test_cap2'; - public function getId() + public function getId(): string { return self::ID; } - public function getCategory() + public function getCategory(): string { return 'Test'; } - public function getName() + public function getName(): string { return 'Cap2'; } - public function getDescription() + public function getDescription(): string { return ''; } - public function getIncludedInRoles() + public function getIncludedInRoles(): array { return array( Write::ID, Admin::ID @@ -95,27 +95,27 @@ class TestCap3 extends Capability { const ID = 'test_cap3'; - public function getId() + public function getId(): string { return self::ID; } - public function getCategory() + public function getCategory(): string { return 'Test'; } - public function getName() + public function getName(): string { return 'Cap3'; } - public function getDescription() + public function getDescription(): string { return ''; } - public function getIncludedInRoles() + public function getIncludedInRoles(): array { return array(Admin::ID); } diff --git a/plugins/UsersManager/tests/Integration/UsersManagerTest.php b/plugins/UsersManager/tests/Integration/UsersManagerTest.php index 38a3ea9c87..bd8807f1c7 100644 --- a/plugins/UsersManager/tests/Integration/UsersManagerTest.php +++ b/plugins/UsersManager/tests/Integration/UsersManagerTest.php @@ -1039,7 +1039,7 @@ class UsersManagerTest extends IntegrationTestCase 'id' => 'write', 'name' => 'UsersManager_PrivWrite', 'description' => 'UsersManager_PrivWriteDescription', - 'helpUrl' => '' + 'helpUrl' => 'https://matomo.org/faq/general/faq_26910' ), array ( 'id' => 'admin', diff --git a/tests/PHPUnit/Integration/AccessTest.php b/tests/PHPUnit/Integration/AccessTest.php index 56072d2650..232581ffb6 100644 --- a/tests/PHPUnit/Integration/AccessTest.php +++ b/tests/PHPUnit/Integration/AccessTest.php @@ -22,19 +22,19 @@ use Piwik\Tests\Framework\TestCase\IntegrationTestCase; class TestCustomCap extends Access\Capability { const ID = 'testcustomcap'; - public function getId() { + public function getId(): string { return self::ID; } - public function getName() { + public function getName(): string { return 'customcap'; } - public function getCategory() { + public function getCategory(): string { return 'test'; } - public function getDescription() { + public function getDescription(): string { return 'lorem ipsum'; } - public function getIncludedInRoles() { + public function getIncludedInRoles(): array { return array(Access\Role\Admin::ID); } |