Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ private function walkGroupAst(
->onlyLiterals($newLiterals)
->nonEmpty($walkResult->isNonEmpty()->or($nonEmpty))
->nonFalsy($walkResult->isNonFalsy()->or($nonFalsy))
->decimalInteger($walkResult->isDecimalInteger()->and($decimalInteger));
->decimalInteger(TrinaryLogic::maxMin($walkResult->isDecimalInteger(), $decimalInteger));
}

// [^0-9] should not parse as decimal-int-string, and [^list-everything-but-numbers] is technically
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14784.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace Bug14784;

use function PHPStan\Testing\assertType;

function doFoo(string $str): void
{
if (preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
assertType('decimal-int-string', $str);
}

// capturing group variant
if (preg_match('/^(0|-?[1-9][0-9]*)$/', $str, $matches)) {
assertType('decimal-int-string', $str);
assertType("array{decimal-int-string, '0'|(decimal-int-string&non-falsy-string)}", $matches);
}

// alternation of two decimal literals
if (preg_match('/^(?:0|123)$/', $str)) {
assertType('decimal-int-string', $str);
}

// alternation without a leading sign
if (preg_match('/^(?:0|[1-9][0-9]*)$/', $str)) {
assertType('decimal-int-string', $str);
}

// alternation digits prefixed by required digits stays decimal
if (preg_match('/^[0-9]+(?:0|5)$/', $str)) {
assertType('decimal-int-string', $str);
}

// an alternation branch that is not decimal breaks the narrowing
if (preg_match('/^(?:0|abc)$/', $str)) {
assertType('non-empty-string', $str);
}
if (preg_match('/^(?:2|abc)$/', $str)) {
assertType('non-falsy-string', $str);
}

// a capturing decimal alternation keeps the group decimal too
if (preg_match('/^(0|[1-9][0-9]*)$/', $str, $matches)) {
assertType("'0'|(decimal-int-string&non-falsy-string)", $matches[1]);
}
}
Loading