Skip to content

Fix matches[0] type for regexes containing "\K" #3920

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

Merged
merged 4 commits into from
Apr 15, 2025
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
17 changes: 16 additions & 1 deletion src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function parseGroups(string $regex): ?RegexAstWalkResult
RegexGroupWalkResult::createEmpty(),
);

if (!$subjectAsGroupResult->mightContainEmptyStringLiteral()) {
if (!$subjectAsGroupResult->mightContainEmptyStringLiteral() && !$this->containsEscapeK($ast)) {
// we could handle numeric-string, in case we know the regex is delimited by ^ and $
if ($subjectAsGroupResult->isNonFalsy()->yes()) {
$astWalkResult = $astWalkResult->withSubjectBaseType(
Expand Down Expand Up @@ -171,6 +171,21 @@ private function updateCapturingAstAddEmptyToken(TreeNode $ast): void
$ast->setChildren([$emptyAlternationAst]);
}

private function containsEscapeK(TreeNode $ast): bool
{
if ($ast->getId() === 'token' && $ast->getValueToken() === 'match_point_reset') {
return true;
}

foreach ($ast->getChildren() as $child) {
if ($this->containsEscapeK($child)) {
return true;
}
}

return false;
}

private function walkRegexAst(
TreeNode $ast,
?RegexAlternation $alternation,
Expand Down
60 changes: 59 additions & 1 deletion tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,65 @@ function bug12749f(string $str): void
}
}

function bug12397(string $string) : array {
function bug12397(string $string): void {
$m = preg_match('#\b([A-Z]{2,})-(\d+)#', $string, $match);
assertType('list{0?: string, 1?: non-falsy-string, 2?: numeric-string}', $match);
}

function bug12792(string $string): void {
if (preg_match('~a\Kb~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{'b'}
}

if (preg_match('~a\K~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{''}
}

if (preg_match('~a\K.+~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{non-empty-string}
}

if (preg_match('~a\K.*~', $string, $match) === 1) {
assertType('array{string}', $match);
}

if (preg_match('~a\K(.+)~', $string, $match) === 1) {
assertType('array{string, non-empty-string}', $match); // could be array{non-empty-string, non-empty-string}
}

if (preg_match('~a\K(.*)~', $string, $match) === 1) {
assertType('array{string, string}', $match);
}

if (preg_match('~a\K(.+?)~', $string, $match) === 1) {
assertType('array{string, non-empty-string}', $match); // could be array{non-empty-string, non-empty-string}
}

if (preg_match('~a\K(.*?)~', $string, $match) === 1) {
assertType('array{string, string}', $match);
}

if (preg_match('~a\K(?=.+)~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{''}
}

if (preg_match('~a\K(?=.*)~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{''}
}

if (preg_match('~a(?:x\Kb|c)~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{'ac'|'b'}
}

if (preg_match('~a(?:c|x\Kb)~', $string, $match) === 1) {
assertType('array{string}', $match); // could be array{'ac'|'b'}
}

if (preg_match('~a(y|(?:x\Kb|c))d~', $string, $match) === 1) {
assertType('array{string, non-empty-string}', $match); // could be array{'acd'|'ayd'|'bd', 'c'|'xb'|'y'}
}

if (preg_match('~a((?:c|x\Kb)|y)d~', $string, $match) === 1) {
assertType('array{string, non-empty-string}', $match); // could be array{'acd'|'ayd'|'bd', 'c'|'xb'|'y'}
}
}
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_replace_callback_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ function ($matches) {
PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL
);
};

function bug12792(string $string) : void {
preg_replace_callback(
'~\'(?:[^\']+|\'\')*+\'\K|\[(\w*)\]~',
function ($matches) {
assertType("array{0: string, 1?: string}", $matches);
return '';
},
$string
);
}
Loading