Skip to content

Commit

Permalink
Merge pull request #8450 from fluffycondor/ctype-functions-assertions
Browse files Browse the repository at this point in the history
Make ctype_digit and ctype_lower work as assertions
  • Loading branch information
orklah committed Sep 5, 2022
2 parents 82be359 + 969c7a0 commit cfe7fd1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Expand Up @@ -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']],
];

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/TypeReconciliation/ConditionalTest.php
Expand Up @@ -2848,6 +2848,58 @@ function matches(string $value): bool {
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>'
]
],
];
}

Expand Down

0 comments on commit cfe7fd1

Please sign in to comment.