Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support int separators in docblocks #9491

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -91,6 +91,7 @@
use function strlen;
use function strpos;
use function strtolower;
use function strtr;
use function substr;

/**
Expand Down Expand Up @@ -420,8 +421,8 @@ public static function getTypeFromTree(
return new TLiteralFloat((float) $parse_tree->value, $from_docblock);
}

if (preg_match('/^\-?(0|[1-9][0-9]*)$/', $parse_tree->value)) {
return new TLiteralInt((int) $parse_tree->value, $from_docblock);
if (preg_match('/^\-?(0|[1-9]([0-9_]*[0-9])?)$/', $parse_tree->value)) {
orklah marked this conversation as resolved.
Show resolved Hide resolved
return new TLiteralInt((int) strtr($parse_tree->value, ['_' => '']), $from_docblock);
}

if (!preg_match('@^(\$this|\\\\?[a-zA-Z_\x7f-\xff][\\\\\-0-9a-zA-Z_\x7f-\xff]*)$@', $parse_tree->value)) {
Expand Down
20 changes: 20 additions & 0 deletions tests/TypeParseTest.php
Expand Up @@ -1032,6 +1032,26 @@ public function testSingleLiteralInt(): void
);
}

public function testSingleLiteralIntWithSeparators(): void
{
$this->assertSame('10', Type::parseString('1_0')->getId());
}

public function testIntRangeWithSeparators(): void
{
$this->assertSame('int<10, 20>', Type::parseString('int<1_0, 2_0>')->getId());
}

public function testLiteralIntUnionWithSeparators(): void
{
$this->assertSame('10|20', Type::parseString('1_0|2_0')->getId());
}

public function testIntMaskWithIntsWithSeparators(): void
{
$this->assertSame('0|10|20|30', Type::parseString('int-mask<1_0, 2_0>')->getId());
}

public function testSingleLiteralFloat(): void
{
$this->assertSame(
Expand Down