Skip to content

RegexGroupParser - cache regex parsing results #3894

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

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 17 additions & 1 deletion src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_key_exists;
use function array_values;
use function count;
use function in_array;
Expand All @@ -42,6 +43,9 @@

private static ?Parser $parser = null;

/** @var array<string, array{array<int, RegexCapturingGroup>, list<string>}|null> */
private static array $resultCache = [];

public function __construct(
private PhpVersion $phpVersion,
private RegexExpressionHelper $regexExpressionHelper,
Expand All @@ -53,6 +57,18 @@
* @return array{array<int, RegexCapturingGroup>, list<string>}|null
*/
public function parseGroups(string $regex): ?array
{
if (array_key_exists($regex, self::$resultCache)) {
return self::$resultCache[$regex];
}

return self::$resultCache[$regex] = $this->parse($regex);
}

/**
* @return array{array<int, RegexCapturingGroup>, list<string>}|null
*/
private function parse(string $regex): ?array
{
if (self::$parser === null) {
/** @throws void */
Expand Down Expand Up @@ -236,7 +252,7 @@
}

if ($ast->getId() === '#mark') {
return $astWalkResult->markVerb($ast->getChild(0)->getValueValue());

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

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.
}

if (
Expand All @@ -246,7 +262,7 @@
$astWalkResult = $astWalkResult->addCapturingGroup($group);

if ($alternation !== null) {
$alternation->pushGroup($combinationIndex, $group);
$alternation = $alternation->pushGroup($combinationIndex, $group);

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.

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

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Result of method PHPStan\Type\Regex\RegexAlternation::pushGroup() (void) is used.
}
}

Expand Down
Loading