Skip to content
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
12 changes: 11 additions & 1 deletion src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
final class RegexGroupParser
{

private const NOT_SUPPORTED_MODIFIERS = [
'J', // rare modifier too complicated to support
];

private static ?Parser $parser = null;

public function __construct(
Expand Down Expand Up @@ -67,8 +71,14 @@ public function parseGroups(string $regex): ?array
return null;
}

$captureOnlyNamed = false;
$modifiers = $this->regexExpressionHelper->getPatternModifiers($regex) ?? '';
foreach (self::NOT_SUPPORTED_MODIFIERS as $notSupportedModifier) {
if (str_contains($modifiers, $notSupportedModifier)) {
return null;
}
}

$captureOnlyNamed = false;
if ($this->phpVersion->supportsPregCaptureOnlyNamedGroups()) {
$captureOnlyNamed = str_contains($modifiers, 'n');
}
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12126.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php // lint >= 7.4

namespace Bug12126;

use function PHPStan\Testing\assertType;


class HelloWorld
{
public function sayHello(): void
{
$options = ['footest', 'testfoo'];
$key = array_rand($options, 1);

$regex = '/foo(?P<test>test)|test(?P<test>foo)/J';
if (!preg_match_all($regex, $options[$key], $matches, PREG_SET_ORDER)) {
return;
}

assertType('list<array<string>>', $matches);
// could be assertType("list<array{0: string, test: 'foo'|'test', 1?: 'test', 2?: 'foo'}>", $matches);
if (!preg_match_all($regex, $options[$key], $matches, PREG_PATTERN_ORDER)) {
return;
}

assertType('array<list<string>>', $matches);
// could be assertType("array{0: list<string>, test: list<'foo'|'test'>, 1: list<'test'|''>, 2: list<''|'foo'>}", $matches);

if (!preg_match($regex, $options[$key], $matches)) {
return;
}

assertType('array<string>', $matches);
// could be assertType("array{0: list<string>, test: 'foo', 1: '', 2: 'foo'}|array{0: list<string>, test: 'test', 1: 'test', 2: ''}", $matches);
}
}
Loading