Skip to content

Commit

Permalink
Fix preg_match() group containing start/end meta characters
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 19, 2024
1 parent 5dfc583 commit 1540b71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private function isMaybeEmptyNode(TreeNode $node, string $patternModifiers, bool
if ($literal !== '' && $literal !== '0') {
$isNonFalsy = true;
}
return false;
return $literal === '';
}

foreach ($node->getChildren() as $child) {
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12297.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php // lint >= 7.4

namespace Bug12297;

use function PHPStan\Testing\assertType;

function doFoo($s) {
if (preg_match('/(^.+)/', $s, $matches) === 1) {
assertType('non-empty-string', $matches[1]);
}

if (preg_match('/(.+$)/', $s, $matches) === 1) {
assertType('non-empty-string', $matches[1]);
}

if (preg_match('/(^.+$)/', $s, $matches) === 1) {
assertType('non-empty-string', $matches[1]);
}
}

0 comments on commit 1540b71

Please sign in to comment.