Skip to content

Commit a56b42d

Browse files
committed
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
1 parent 80b0d3f commit a56b42d

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/Ctype/Ctype.php

+34-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ final class Ctype
3131
*/
3232
public static function ctype_alnum($text)
3333
{
34-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
34+
self::checkType($text, __FUNCTION__);
35+
$text = self::convert_int_to_char_for_ctype($text);
3536

3637
return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z0-9]/', $text);
3738
}
@@ -47,7 +48,8 @@ public static function ctype_alnum($text)
4748
*/
4849
public static function ctype_alpha($text)
4950
{
50-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
51+
self::checkType($text, __FUNCTION__);
52+
$text = self::convert_int_to_char_for_ctype($text);
5153

5254
return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z]/', $text);
5355
}
@@ -63,7 +65,8 @@ public static function ctype_alpha($text)
6365
*/
6466
public static function ctype_cntrl($text)
6567
{
66-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
68+
self::checkType($text, __FUNCTION__);
69+
$text = self::convert_int_to_char_for_ctype($text);
6770

6871
return \is_string($text) && '' !== $text && !preg_match('/[^\x00-\x1f\x7f]/', $text);
6972
}
@@ -79,7 +82,8 @@ public static function ctype_cntrl($text)
7982
*/
8083
public static function ctype_digit($text)
8184
{
82-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
85+
self::checkType($text, __FUNCTION__);
86+
$text = self::convert_int_to_char_for_ctype($text);
8387

8488
return \is_string($text) && '' !== $text && !preg_match('/[^0-9]/', $text);
8589
}
@@ -95,7 +99,8 @@ public static function ctype_digit($text)
9599
*/
96100
public static function ctype_graph($text)
97101
{
98-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
102+
self::checkType($text, __FUNCTION__);
103+
$text = self::convert_int_to_char_for_ctype($text);
99104

100105
return \is_string($text) && '' !== $text && !preg_match('/[^!-~]/', $text);
101106
}
@@ -111,7 +116,8 @@ public static function ctype_graph($text)
111116
*/
112117
public static function ctype_lower($text)
113118
{
114-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
119+
self::checkType($text, __FUNCTION__);
120+
$text = self::convert_int_to_char_for_ctype($text);
115121

116122
return \is_string($text) && '' !== $text && !preg_match('/[^a-z]/', $text);
117123
}
@@ -127,7 +133,8 @@ public static function ctype_lower($text)
127133
*/
128134
public static function ctype_print($text)
129135
{
130-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
136+
self::checkType($text, __FUNCTION__);
137+
$text = self::convert_int_to_char_for_ctype($text);
131138

132139
return \is_string($text) && '' !== $text && !preg_match('/[^ -~]/', $text);
133140
}
@@ -143,7 +150,8 @@ public static function ctype_print($text)
143150
*/
144151
public static function ctype_punct($text)
145152
{
146-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
153+
self::checkType($text, __FUNCTION__);
154+
$text = self::convert_int_to_char_for_ctype($text);
147155

148156
return \is_string($text) && '' !== $text && !preg_match('/[^!-\/\:-@\[-`\{-~]/', $text);
149157
}
@@ -159,7 +167,8 @@ public static function ctype_punct($text)
159167
*/
160168
public static function ctype_space($text)
161169
{
162-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
170+
self::checkType($text, __FUNCTION__);
171+
$text = self::convert_int_to_char_for_ctype($text);
163172

164173
return \is_string($text) && '' !== $text && !preg_match('/[^\s]/', $text);
165174
}
@@ -175,7 +184,8 @@ public static function ctype_space($text)
175184
*/
176185
public static function ctype_upper($text)
177186
{
178-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
187+
self::checkType($text, __FUNCTION__);
188+
$text = self::convert_int_to_char_for_ctype($text);
179189

180190
return \is_string($text) && '' !== $text && !preg_match('/[^A-Z]/', $text);
181191
}
@@ -191,7 +201,8 @@ public static function ctype_upper($text)
191201
*/
192202
public static function ctype_xdigit($text)
193203
{
194-
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
204+
self::checkType($text, __FUNCTION__);
205+
$text = self::convert_int_to_char_for_ctype($text);
195206

196207
return \is_string($text) && '' !== $text && !preg_match('/[^A-Fa-f0-9]/', $text);
197208
}
@@ -205,11 +216,10 @@ public static function ctype_xdigit($text)
205216
* Any other integer is interpreted as a string containing the decimal digits of the integer.
206217
*
207218
* @param mixed $int
208-
* @param string $function
209219
*
210220
* @return mixed
211221
*/
212-
private static function convert_int_to_char_for_ctype($int, $function)
222+
private static function convert_int_to_char_for_ctype($int)
213223
{
214224
if (!\is_int($int)) {
215225
return $int;
@@ -219,14 +229,21 @@ private static function convert_int_to_char_for_ctype($int, $function)
219229
return (string) $int;
220230
}
221231

222-
if (\PHP_VERSION_ID >= 80100) {
223-
@trigger_error($function.'(): Argument of type int will be interpreted as string in the future', \E_USER_DEPRECATED);
224-
}
225-
226232
if ($int < 0) {
227233
$int += 256;
228234
}
229235

230236
return \chr($int);
231237
}
238+
239+
/**
240+
* @param mixed $input
241+
* @param string $function
242+
*/
243+
public static function checkType($input, $function)
244+
{
245+
if (\PHP_VERSION_ID >= 80100 && !\is_string($input)) {
246+
@trigger_error($function.'(): Argument of type '.get_debug_type($input).' will be interpreted as string in the future', \E_USER_DEPRECATED);
247+
}
248+
}
232249
}

src/Ctype/composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.2"
19+
"php": ">=7.2",
20+
"symfony/polyfill-php80": "^1.31"
2021
},
2122
"provide": {
2223
"ext-ctype": "*"

0 commit comments

Comments
 (0)