Skip to content

Commit 9eb2c0b

Browse files
committed
Bugfix: mbstring polyfills must not raise value errors in PHP 7
1 parent 731f6e1 commit 9eb2c0b

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

src/Mbstring/Mbstring.php

+42-18
Original file line numberDiff line numberDiff line change
@@ -834,19 +834,32 @@ public static function mb_ord($s, $encoding = null)
834834
return $code;
835835
}
836836

837-
public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null): string
837+
/** @return string|false */
838+
public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null)
838839
{
839840
if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) {
841+
if (\PHP_VERSION_ID < 80000) {
842+
trigger_error('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', \E_USER_WARNING);
843+
844+
return false;
845+
}
846+
840847
throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH');
841848
}
842849

843850
if (null === $encoding) {
844851
$encoding = self::mb_internal_encoding();
845-
} else {
846-
self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given');
852+
} elseif (!self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given')) {
853+
return false;
847854
}
848855

849856
if (self::mb_strlen($pad_string, $encoding) <= 0) {
857+
if (\PHP_VERSION_ID < 80000) {
858+
trigger_error('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', \E_USER_WARNING);
859+
860+
return false;
861+
}
862+
850863
throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string');
851864
}
852865

@@ -869,12 +882,13 @@ public static function mb_str_pad(string $string, int $length, string $pad_strin
869882
}
870883
}
871884

872-
public static function mb_ucfirst(string $string, ?string $encoding = null): string
885+
/** @return string|false */
886+
public static function mb_ucfirst(string $string, ?string $encoding = null)
873887
{
874888
if (null === $encoding) {
875889
$encoding = self::mb_internal_encoding();
876-
} else {
877-
self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given');
890+
} elseif (!self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) {
891+
return false;
878892
}
879893

880894
$firstChar = mb_substr($string, 0, 1, $encoding);
@@ -883,12 +897,13 @@ public static function mb_ucfirst(string $string, ?string $encoding = null): str
883897
return $firstChar.mb_substr($string, 1, null, $encoding);
884898
}
885899

886-
public static function mb_lcfirst(string $string, ?string $encoding = null): string
900+
/** @return string|false */
901+
public static function mb_lcfirst(string $string, ?string $encoding = null)
887902
{
888903
if (null === $encoding) {
889904
$encoding = self::mb_internal_encoding();
890-
} else {
891-
self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given');
905+
} elseif (!self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) {
906+
return false;
892907
}
893908

894909
$firstChar = mb_substr($string, 0, 1, $encoding);
@@ -971,27 +986,31 @@ private static function getEncoding($encoding)
971986
return $encoding;
972987
}
973988

974-
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
989+
/** @return string|false */
990+
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null)
975991
{
976992
return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__);
977993
}
978994

979-
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string
995+
/** @return string|false */
996+
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null)
980997
{
981998
return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__);
982999
}
9831000

984-
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
1001+
/** @return string|false */
1002+
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null)
9851003
{
9861004
return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__);
9871005
}
9881006

989-
private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string
1007+
/** @return string|false */
1008+
private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function)
9901009
{
9911010
if (null === $encoding) {
9921011
$encoding = self::mb_internal_encoding();
993-
} else {
994-
self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given');
1012+
} elseif (!self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given')) {
1013+
return false;
9951014
}
9961015

9971016
if ('' === $characters) {
@@ -1029,17 +1048,22 @@ private static function mb_internal_trim(string $regex, string $string, ?string
10291048
return iconv('UTF-8', $encoding.'//IGNORE', $string);
10301049
}
10311050

1032-
private static function assertEncoding(string $encoding, string $errorFormat): void
1051+
private static function assertEncoding(string $encoding, string $errorFormat): bool
10331052
{
10341053
try {
10351054
$validEncoding = @self::mb_check_encoding('', $encoding);
10361055
} catch (\ValueError $e) {
10371056
throw new \ValueError(sprintf($errorFormat, $encoding));
10381057
}
10391058

1040-
// BC for PHP 7.3 and lower
10411059
if (!$validEncoding) {
1042-
throw new \ValueError(sprintf($errorFormat, $encoding));
1060+
if (\PHP_VERSION_ID >= 80000) {
1061+
throw new \ValueError(sprintf($errorFormat, $encoding));
1062+
}
1063+
1064+
trigger_error(sprintf($errorFormat, $encoding), E_USER_WARNING);
10431065
}
1066+
1067+
return $validEncoding;
10441068
}
10451069
}

src/Mbstring/bootstrap.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,27 @@ function mb_str_split($string, $length = 1, $encoding = null) { return p\Mbstrin
133133
}
134134

135135
if (!function_exists('mb_str_pad')) {
136-
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
136+
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null) { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
137137
}
138138

139139
if (!function_exists('mb_ucfirst')) {
140-
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
140+
function mb_ucfirst(string $string, ?string $encoding = null) { return p\Mbstring::mb_ucfirst($string, $encoding); }
141141
}
142142

143143
if (!function_exists('mb_lcfirst')) {
144-
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
144+
function mb_lcfirst(string $string, ?string $encoding = null) { return p\Mbstring::mb_lcfirst($string, $encoding); }
145145
}
146146

147147
if (!function_exists('mb_trim')) {
148-
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); }
148+
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) { return p\Mbstring::mb_trim($string, $characters, $encoding); }
149149
}
150150

151151
if (!function_exists('mb_ltrim')) {
152-
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
152+
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) { return p\Mbstring::mb_ltrim($string, $characters, $encoding); }
153153
}
154154

155155
if (!function_exists('mb_rtrim')) {
156-
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
156+
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) { return p\Mbstring::mb_rtrim($string, $characters, $encoding); }
157157
}
158158

159159

0 commit comments

Comments
 (0)