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 2d3fbfb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Type/Php/CtypeDigitFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeCombinator;
Expand All @@ -38,6 +39,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 2d3fbfb

Please sign in to comment.