Skip to content

Commit

Permalink
Support # comments in regex
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 16, 2024
1 parent 2d64686 commit fac659e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function count;
use function in_array;
use function is_int;
use function preg_replace;
use function rtrim;
use function sscanf;
use function str_contains;
Expand Down Expand Up @@ -64,20 +65,24 @@ public function parseGroups(string $regex): ?array
return null;
}

$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);
try {
$ast = self::$parser->parse($rawRegex);
} catch (Exception) {
return null;
}

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

if (str_contains($modifiers, 'x')) {
$regex = preg_replace('/#.*/', '', $regex);
}

$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 79 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.
try {

Check failure on line 80 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.

Check failure on line 80 in src/Type/Regex/RegexGroupParser.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #1 $pattern of method PHPStan\Type\Regex\RegexExpressionHelper::removeDelimitersAndModifiers() expects string, string|null given.
$ast = self::$parser->parse($rawRegex);
} catch (Exception) {
return null;
}

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

namespace Bug12242;

use function PHPStan\Testing\assertType;

function foo(string $str): void
{
$regexp = '/
# (
([\d,]*)
# )
/x';
if (preg_match($regexp, $str, $match)) {
assertType('array{string, string}', $match);
}
}

0 comments on commit fac659e

Please sign in to comment.