Skip to content

Commit

Permalink
Fix bug parsing negative classes containing only digits ([^0-9])
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek authored and ondrejmirtes committed Jul 19, 2024
1 parent dbb9665 commit fc75deb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,13 @@ private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryL
}
}

// [^0-9] should not parse as numeric-string, and [^list-everything-but-numbers] is technically
// doable but really silly compared to just \d so we can safely assume the string is not numeric
// for negative classes
if ($ast->getId() === '#negativeclass') {
$isNumeric = TrinaryLogic::createNo();
}

foreach ($children as $child) {
$this->walkGroupAst(
$child,
Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,7 @@ function bug11323(string $s): void {
if (preg_match('{(x?([1-4])\d)}', $s, $matches)) {
assertType('array{string, non-empty-string, numeric-string}', $matches);
}
if (preg_match('{([^1-4])}', $s, $matches)) {
assertType('array{string, non-empty-string}', $matches);
}
}

0 comments on commit fc75deb

Please sign in to comment.