Avoid dodgy array_flip hash building

the conventional way is using array_keys and array_fill_keys. Behaviour is more predictable & also avoids benevolent union fuckery from PHPStan.
This commit is contained in:
Dylan K. Taylor 2025-01-08 01:45:28 +00:00
parent e34f34f9f4
commit 0a16daa619
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 17 additions and 7 deletions

View File

@ -25,18 +25,22 @@ namespace pocketmine\item\enchantment;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\lang\Translatable;
use function array_flip;
use function array_fill_keys;
use function floor;
class ProtectionEnchantment extends Enchantment{
protected float $typeModifier;
/** @var int[]|null */
/**
* @var true[]|null
* @phpstan-var array<int, true>
*/
protected ?array $applicableDamageTypes = null;
/**
* ProtectionEnchantment constructor.
*
* @phpstan-param null|(\Closure(int $level) : int) $minEnchantingPower
* @phpstan-param list<int>|null $applicableDamageTypes
*
* @param int $primaryItemFlags @deprecated
* @param int $secondaryItemFlags @deprecated
@ -48,7 +52,7 @@ class ProtectionEnchantment extends Enchantment{
$this->typeModifier = $typeModifier;
if($applicableDamageTypes !== null){
$this->applicableDamageTypes = array_flip($applicableDamageTypes);
$this->applicableDamageTypes = array_fill_keys($applicableDamageTypes, true);
}
}

View File

@ -24,7 +24,8 @@ declare(strict_types=1);
namespace pocketmine\plugin;
use pocketmine\utils\Utils;
use function array_flip;
use function array_fill_keys;
use function array_keys;
use function is_array;
use function is_float;
use function is_int;
@ -32,23 +33,28 @@ use function is_string;
class PluginGraylist{
/** @var string[] */
/**
* @var true[]
* @phpstan-var array<string, true>
*/
private array $plugins;
private bool $isWhitelist = false;
/**
* @param string[] $plugins
* @phpstan-param list<string> $plugins
*/
public function __construct(array $plugins = [], bool $whitelist = false){
$this->plugins = array_flip($plugins);
$this->plugins = array_fill_keys($plugins, true);
$this->isWhitelist = $whitelist;
}
/**
* @return string[]
* @phpstan-return list<string>
*/
public function getPlugins() : array{
return array_flip($this->plugins);
return array_keys($this->plugins);
}
public function isWhitelist() : bool{