mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 19:02:59 +00:00
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:
@ -571,6 +571,22 @@ final class Utils{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generator which forces array keys to string during iteration.
|
||||
* This is necessary because PHP has an anti-feature where it casts numeric string keys to integers, leading to
|
||||
* various crashes.
|
||||
*
|
||||
* @phpstan-template TKeyType of string
|
||||
* @phpstan-template TValueType
|
||||
* @phpstan-param array<TKeyType, TValueType> $array
|
||||
* @phpstan-return \Generator<TKeyType, TValueType, void, void>
|
||||
*/
|
||||
public static function stringifyKeys(array $array) : \Generator{
|
||||
foreach($array as $key => $value){ // @phpstan-ignore-line - this is where we fix the stupid bullshit with array keys :)
|
||||
yield (string) $key => $value;
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkUTF8(string $string) : void{
|
||||
if(!mb_check_encoding($string, 'UTF-8')){
|
||||
throw new \InvalidArgumentException("Text must be valid UTF-8");
|
||||
|
Reference in New Issue
Block a user