Skip to content

Commit f390b54

Browse files
staabmondrejmirtes
authored andcommitted
RegexArrayShapeMatcher: fix regex alternatives in capture group are concatenated
1 parent e9e419c commit f390b54

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Type/Regex/RegexGroupParser.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,30 @@ private function walkGroupAst(
446446
}
447447

448448
if ($ast->getId() === '#alternation') {
449-
$inAlternation = true;
449+
$newLiterals = [];
450+
foreach ($children as $child) {
451+
$walkResult = $this->walkGroupAst(
452+
$child,
453+
true,
454+
$inClass,
455+
$patternModifiers,
456+
$walkResult->onlyLiterals([]),
457+
);
458+
459+
if ($newLiterals === null) {
460+
continue;
461+
}
462+
463+
if (count($walkResult->getOnlyLiterals() ?? []) > 0) {
464+
foreach ($walkResult->getOnlyLiterals() as $alternationLiterals) {
465+
$newLiterals[] = $alternationLiterals;
466+
}
467+
} else {
468+
$newLiterals = null;
469+
}
470+
}
471+
472+
return $walkResult->onlyLiterals($newLiterals);
450473
}
451474

452475
// [^0-9] should not parse as numeric-string, and [^list-everything-but-numbers] is technically
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php // lint >= 7.4
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug12210;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
function bug12210a(string $text): void {
10+
assert(preg_match('(((sum|min|max)))', $text, $match) === 1);
11+
assertType("array{string, 'max'|'min'|'sum', 'max'|'min'|'sum'}", $match);
12+
}
13+
14+
function bug12210b(string $text): void {
15+
assert(preg_match('(((sum|min|ma.)))', $text, $match) === 1);
16+
assertType("array{string, non-empty-string, non-falsy-string}", $match);
17+
}
18+
19+
function bug12210c(string $text): void {
20+
assert(preg_match('(((su.|min|max)))', $text, $match) === 1);
21+
assertType("array{string, non-empty-string, non-falsy-string}", $match);
22+
}
23+
24+
function bug12210d(string $text): void {
25+
assert(preg_match('(((sum|mi.|max)))', $text, $match) === 1);
26+
assertType("array{string, non-empty-string, non-falsy-string}", $match);
27+
}

0 commit comments

Comments
 (0)