phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

Narrow string type when `ctype_*` functions return true

Open takaram opened this issue 1 month ago • 0 comments

Closes phpstan/phpstan#13089

Some ctype functions asserts non-falsy-string, while others asserts non-empty-string.

Test script to see the functions' return value when `''` or `'0'` given
<?php

$ctypeFuncs = array_filter(
	get_defined_functions()['internal'],
	fn (string $name) => str_starts_with($name, 'ctype_'),
);

echo "func name   \t''\t'0'\n";
foreach ($ctypeFuncs as $func) {
	printf(
		"%-12s\t%s\t%s\n",
		$func,
		var_export($func(''), true),
		var_export($func('0'), true),
	);
}

Result:

func name       ''      '0'
ctype_alnum     false   true
ctype_alpha     false   false
ctype_cntrl     false   false
ctype_digit     false   true
ctype_lower     false   false
ctype_graph     false   true
ctype_print     false   true
ctype_punct     false   false
ctype_space     false   false
ctype_upper     false   false
ctype_xdigit    false   true

takaram avatar Nov 24 '25 01:11 takaram