1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #8450 from fluffycondor/ctype-functions-assertions

Make ctype_digit and ctype_lower work as assertions
This commit is contained in:
orklah 2022-09-05 18:42:55 +02:00 committed by GitHub
commit cfe7fd1a53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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>'
]
],
];
}