Skip to content

Commit 966812e

Browse files
authored
Merge branch refs/heads/1.12.x into 2.0.x
2 parents 961150e + 9a6e12c commit 966812e

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php // lint >= 7.4
2+
3+
namespace Bug12173;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
public function parse(string $string): void
10+
{
11+
$regex = '#.*(?<fruit>(apple|orange)).*#';
12+
13+
if (preg_match($regex, $string, $matches) !== 1) {
14+
throw new \Exception('Invalid input');
15+
}
16+
17+
assertType("'apple'|'orange'", $matches['fruit']);;
18+
}
19+
}
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)