UnsafeForeachArrayOfStringRule: use statically analysable function reference

this will ensure that it get automatically updated during refactors.
This commit is contained in:
Dylan K. Taylor 2022-01-18 00:23:29 +00:00
parent 8da27ea0aa
commit f1723acfd3
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -34,6 +34,7 @@ use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\VerbosityLevel;
use pocketmine\utils\Utils;
use function sprintf;
/**
@ -77,11 +78,12 @@ final class UnsafeForeachArrayOfStringRule implements Rule{
return $type;
});
if($hasCastableKeyTypes && !$expectsIntKeyTypes){
$func = \Closure::fromCallable([Utils::class, 'stringifyKeys']);
return [
RuleErrorBuilder::message(sprintf(
"Unsafe foreach on array with key type %s (they might be casted to int).",
$iterableType->getIterableKeyType()->describe(VerbosityLevel::value())
))->tip("Use Utils::stringifyKeys() for a safe Generator-based iterator.")->build()
))->tip(sprintf("Use %s() for a safe Generator-based iterator.", Utils::getNiceClosureName($func)))->build()
];
}
return [];