Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctype: Give the correct deprecations for PHP 8.1+ #504

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/Ctype/Ctype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -219,14 +229,20 @@ 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
*/
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);
}
}
}
3 changes: 2 additions & 1 deletion src/Ctype/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=7.2"
"php": ">=7.2",
"symfony/polyfill-php80": "^1.31"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you adding this dependency ?

},
"provide": {
"ext-ctype": "*"
Expand Down
Loading