Always use Permission
objects instead of string
#6536
Labels
BC break
Breaks API compatibility
Category: API
Related to the plugin API
Easy task
Probably really easy to do, good task for first-time contributors
Type: Enhancement
Contributes features or other improvements to PocketMine-MP
Milestone
Problem description
There are various places where strings have to be looked up as permissions before using them:
PocketMine-MP/src/permission/PermissibleInternal.php
Line 142 in 8cdc7d7
PocketMine-MP/src/permission/PermissibleInternal.php
Line 191 in 8cdc7d7
PocketMine-MP/src/plugin/PluginManager.php
Line 191 in 8cdc7d7
PocketMine-MP/src/command/Command.php
Line 106 in 8cdc7d7
This is problematic because it requires redundant checks for null when working with always-defined core permissions, and also allows passing in strings that can't be checked statically, allowing typos that only crash when the code is run.
Proposed solution
In all of these places, we should just use
Permission
objects directly. Using strings presents the possibility that the permission may not exist or may not have been registered. (To be fair, usingPermission
objects also doesn't guarantee registration. We should probably fix that too.)Referencing permissions by their string IDs should only be done when loading stuff from configs, like in a permission management plugin. The IDs are intended for user use, not code use. Ergo,
PermissionManager
should only be interacted with to look up a permission by its ID.DefaultPermissions
should turn into aRegistryTrait
user (or perhaps an enum with metadata) to remove the need forDefaultPermissionNames
(which should never have been needed to begin with).Alternative solutions that don't require API changes
The text was updated successfully, but these errors were encountered: