Skip to content

Commit

Permalink
Fix numeric string ctype_digit always true type specifier result
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard van Velzen committed Sep 6, 2022
1 parent 12e40f1 commit 8dfe991
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Type/Php/CtypeDigitFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
throw new ShouldNotHappenException();
}

if ($context->true() && $scope->getType($node->getArgs()[0]->value)->isNumericString()->yes()) {
return new SpecifiedTypes();
}

$types = [
IntegerRangeType::fromInterval(48, 57), // ASCII-codes for 0-9
IntegerRangeType::createAllGreaterThanOrEqualTo(256), // Starting from 256 ints are interpreted as strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,11 @@ public function testBug6599(): void
$this->analyse([__DIR__ . '/data/bug-6599.php'], []);
}

public function testBug7914(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-7914.php'], []);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-7914.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace Bug7914;

function foo(string $s): void
{
if (is_numeric($s)) {
if (ctype_digit($s)) {
echo "I'm all-digit string";
} else {
echo "I'm a not all-digit string";
}
}
}

0 comments on commit 8dfe991

Please sign in to comment.