TextFormat: wrap all preg_replace() usages in a type-safe exception-throwing version

fixes 3 phpstan level 8 errors
This commit is contained in:
Dylan K. Taylor 2020-06-15 23:31:46 +01:00
parent 1fb5043eb1
commit 23b97d8e2d
2 changed files with 15 additions and 15 deletions

View File

@ -72,6 +72,17 @@ abstract class TextFormat{
throw new \InvalidArgumentException("$info: Encountered PCRE error " . preg_last_error() . " during regex operation");
}
/**
* @throws \InvalidArgumentException
*/
private static function preg_replace(string $pattern, string $replacement, string $string, string $errorContext) : string{
$result = preg_replace($pattern, $replacement, $string);
if($result === null){
throw self::makePcreError($errorContext);
}
return $result;
}
/**
* Splits the string by Format tokens
*
@ -90,12 +101,11 @@ abstract class TextFormat{
*/
public static function clean(string $string, bool $removeFormat = true) : string{
$string = mb_scrub($string, 'UTF-8');
$string = preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console)
if($string === null) throw self::makePcreError("Failed to strip private-area characters");
$string = self::preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string, "Stripping private-area characters"); //remove unicode private-use-area characters (they might break the console)
if($removeFormat){
$string = str_replace(TextFormat::ESCAPE, "", preg_replace("/" . TextFormat::ESCAPE . "[0-9a-fk-or]/u", "", $string));
$string = str_replace(TextFormat::ESCAPE, "", self::preg_replace("/" . TextFormat::ESCAPE . "[0-9a-fk-or]/u", "", $string, "Removing color codes"));
}
return str_replace("\x1b", "", preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string));
return str_replace("\x1b", "", self::preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string, "Removing special characters"));
}
/**
@ -104,7 +114,7 @@ abstract class TextFormat{
* @param string $placeholder default "&"
*/
public static function colorize(string $string, string $placeholder = "&") : string{
return preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-fk-or])/u', TextFormat::ESCAPE . '$1', $string);
return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-fk-or])/u', TextFormat::ESCAPE . '$1', $string, "Colorizing string");
}
/**

View File

@ -1760,16 +1760,6 @@ parameters:
count: 1
path: ../../../src/pocketmine/utils/MainLogger.php
-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, string\\|null given\\.$#"
count: 2
path: ../../../src/pocketmine/utils/TextFormat.php
-
message: "#^Method pocketmine\\\\utils\\\\TextFormat\\:\\:colorize\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: ../../../src/pocketmine/utils/TextFormat.php
-
message: "#^Method pocketmine\\\\utils\\\\Utils\\:\\:printable\\(\\) should return string but returns string\\|null\\.$#"
count: 1