From cbbdfaed84e8b7c05d42370ec9be2383242c76c4 Mon Sep 17 00:00:00 2001 From: gertdepagter Date: Wed, 25 Sep 2024 16:58:24 +0200 Subject: [PATCH 1/2] Give the correct deprecations for PHP 8.1+ This isn't just for integers that are outside the integer range of -128, 255. https://3v4l.org/PgZ6c --- src/Ctype/Ctype.php | 51 +++++++++++++++++++++++++++-------------- src/Ctype/composer.json | 3 ++- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Ctype/Ctype.php b/src/Ctype/Ctype.php index ba75a2c95..a46105d00 100644 --- a/src/Ctype/Ctype.php +++ b/src/Ctype/Ctype.php @@ -31,7 +31,8 @@ final class Ctype */ public static function ctype_alnum($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z0-9]/', $text); } @@ -47,7 +48,8 @@ public static function ctype_alnum($text) */ public static function ctype_alpha($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z]/', $text); } @@ -63,7 +65,8 @@ public static function ctype_alpha($text) */ public static function ctype_cntrl($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^\x00-\x1f\x7f]/', $text); } @@ -79,7 +82,8 @@ public static function ctype_cntrl($text) */ public static function ctype_digit($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^0-9]/', $text); } @@ -95,7 +99,8 @@ public static function ctype_digit($text) */ public static function ctype_graph($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^!-~]/', $text); } @@ -111,7 +116,8 @@ public static function ctype_graph($text) */ public static function ctype_lower($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^a-z]/', $text); } @@ -127,7 +133,8 @@ public static function ctype_lower($text) */ public static function ctype_print($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^ -~]/', $text); } @@ -143,7 +150,8 @@ public static function ctype_print($text) */ public static function ctype_punct($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^!-\/\:-@\[-`\{-~]/', $text); } @@ -159,7 +167,8 @@ public static function ctype_punct($text) */ public static function ctype_space($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^\s]/', $text); } @@ -175,7 +184,8 @@ public static function ctype_space($text) */ public static function ctype_upper($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^A-Z]/', $text); } @@ -191,7 +201,8 @@ public static function ctype_upper($text) */ public static function ctype_xdigit($text) { - $text = self::convert_int_to_char_for_ctype($text, __FUNCTION__); + self::checkType($text, __FUNCTION__); + $text = self::convert_int_to_char_for_ctype($text); return \is_string($text) && '' !== $text && !preg_match('/[^A-Fa-f0-9]/', $text); } @@ -205,11 +216,10 @@ public static function ctype_xdigit($text) * Any other integer is interpreted as a string containing the decimal digits of the integer. * * @param mixed $int - * @param string $function * * @return mixed */ - private static function convert_int_to_char_for_ctype($int, $function) + private static function convert_int_to_char_for_ctype($int) { if (!\is_int($int)) { return $int; @@ -219,14 +229,21 @@ private static function convert_int_to_char_for_ctype($int, $function) return (string) $int; } - if (\PHP_VERSION_ID >= 80100) { - @trigger_error($function.'(): Argument of type int will be interpreted as string in the future', \E_USER_DEPRECATED); - } - if ($int < 0) { $int += 256; } return \chr($int); } + + /** + * @param mixed $input + * @param string $function + */ + public static function checkType($input, $function) + { + if (\PHP_VERSION_ID >= 80100 && !\is_string($input)) { + @trigger_error($function.'(): Argument of type '.get_debug_type($input).' will be interpreted as string in the future', \E_USER_DEPRECATED); + } + } } diff --git a/src/Ctype/composer.json b/src/Ctype/composer.json index 131ca7adb..99d74b24e 100644 --- a/src/Ctype/composer.json +++ b/src/Ctype/composer.json @@ -16,7 +16,8 @@ } ], "require": { - "php": ">=7.2" + "php": ">=7.2", + "symfony/polyfill-php80": "^1.31" }, "provide": { "ext-ctype": "*" From 116cfd7354499e0a6ad572112cd034be11d3e5ea Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Thu, 26 Sep 2024 10:40:10 +0200 Subject: [PATCH 2/2] Make checkType method private Co-authored-by: Alexander M. Turek --- src/Ctype/Ctype.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Ctype/Ctype.php b/src/Ctype/Ctype.php index a46105d00..b19c2f151 100644 --- a/src/Ctype/Ctype.php +++ b/src/Ctype/Ctype.php @@ -238,9 +238,8 @@ private static function convert_int_to_char_for_ctype($int) /** * @param mixed $input - * @param string $function */ - public static function checkType($input, $function) + private static function checkType($input, string $function): void { if (\PHP_VERSION_ID >= 80100 && !\is_string($input)) { @trigger_error($function.'(): Argument of type '.get_debug_type($input).' will be interpreted as string in the future', \E_USER_DEPRECATED);