Ban foreach(arrayWithStringKeys as k => v)

this is not as good as phpstan/phpstan-src#769 (e.g. array_key_first()/array_key_last() aren't covered by this, nor is array_rand()) but it does eliminate the most infuriating cases where this usually crops up.
This commit is contained in:
Dylan K. Taylor
2021-11-15 22:52:05 +00:00
parent f2d5455c5e
commit 269231c228
24 changed files with 150 additions and 33 deletions

View File

@@ -27,6 +27,7 @@ use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginException;
use pocketmine\timings\Timings;
use pocketmine\utils\ObjectSet;
use pocketmine\utils\Utils;
use function count;
use function spl_object_id;
@@ -143,7 +144,7 @@ class PermissibleInternal implements Permissible{
$oldPermissions = $this->permissions;
$this->permissions = [];
foreach($this->rootPermissions as $name => $isGranted){
foreach(Utils::stringifyKeys($this->rootPermissions) as $name => $isGranted){
$perm = $permManager->getPermission($name);
if($perm === null){
throw new \LogicException("Unregistered root permission $name");
@@ -187,11 +188,12 @@ class PermissibleInternal implements Permissible{
}
/**
* @param bool[] $children
* @param bool[] $children
* @phpstan-param array<string, bool> $children
*/
private function calculateChildPermissions(array $children, bool $invert, ?PermissionAttachment $attachment, ?PermissionAttachmentInfo $parent) : void{
$permManager = PermissionManager::getInstance();
foreach($children as $name => $v){
foreach(Utils::stringifyKeys($children) as $name => $v){
$perm = $permManager->getPermission($name);
$value = ($v xor $invert);
$this->permissions[$name] = new PermissionAttachmentInfo($name, $attachment, $value, $parent);

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\permission;
use pocketmine\utils\Utils;
use function is_bool;
use function strtolower;
@@ -83,7 +84,7 @@ class PermissionParser{
*/
public static function loadPermissions(array $data, string $default = self::DEFAULT_FALSE) : array{
$result = [];
foreach($data as $name => $entry){
foreach(Utils::stringifyKeys($data) as $name => $entry){
$desc = null;
if(isset($entry["default"])){
$default = PermissionParser::defaultFromString($entry["default"]);