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:
commit
cfe7fd1a53
@ -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']],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -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>'
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user