1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Make ctype_digit and ctype_lower work as assertions

This commit is contained in:
Semyon 2022-09-02 17:37:10 +03:00
parent faf106e717
commit 969c7a098b
2 changed files with 54 additions and 0 deletions

View File

@ -103,6 +103,8 @@ class AssertionFinder
'is_scalar' => ['scalar', [Type::class, 'getScalar']],
'is_iterable' => ['iterable'],
'is_countable' => ['countable'],
'ctype_digit' => ['numeric-string', [Type::class, 'getNumericString']],
'ctype_lower' => ['non-empty-lowercase-string', [Type::class, 'getNonEmptyLowercaseString']],
];
/**

View File

@ -2848,6 +2848,58 @@ class ConditionalTest extends TestCase
return true;
}'
],
'ctypeDigitMakesStringNumeric' => [
'<?php
/** @param numeric-string $num */
function foo(string $num): void {}
/** @param mixed $m */
function bar(mixed $m): void
{
if (is_string($m) && ctype_digit($m)) {
foo($m);
}
}
',
],
'SKIPPED-ctypeDigitNarrowsIntToARange' => [
'<?php
$int = rand(-1000, 1000);
if (!ctype_digit($int)) {
die;
}
',
'assertions' => [
'$int' => 'int<48, 57>|int<256, 1000>'
]
],
'ctypeLowerMakesStringLowercase' => [
'<?php
/** @param non-empty-lowercase-string $num */
function foo(string $num): void {}
/** @param mixed $m */
function bar($m): void
{
if (is_string($m) && ctype_lower($m)) {
foo($m);
}
}
',
],
'SKIPPED-ctypeLowerNarrowsIntToARange' => [
'<?php
$int = rand(-1000, 1000);
if (!ctype_lower($int)) {
die;
}
',
'assertions' => [
'$int' => 'int<97, 122>'
]
],
];
}