Skip to content

Commit fb0c189

Browse files
committed
RegexGroupParser - cache regex parsing results
1 parent 12a0b4e commit fb0c189

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Type/Regex/RegexGroupParser.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\StringType;
2121
use PHPStan\Type\Type;
2222
use PHPStan\Type\TypeCombinator;
23+
use function array_key_exists;
2324
use function array_values;
2425
use function count;
2526
use function in_array;
@@ -42,6 +43,8 @@ final class RegexGroupParser
4243

4344
private static ?Parser $parser = null;
4445

46+
private static array $resultCache = [];
47+
4548
public function __construct(
4649
private PhpVersion $phpVersion,
4750
private RegexExpressionHelper $regexExpressionHelper,
@@ -53,6 +56,18 @@ public function __construct(
5356
* @return array{array<int, RegexCapturingGroup>, list<string>}|null
5457
*/
5558
public function parseGroups(string $regex): ?array
59+
{
60+
if (array_key_exists($regex, self::$resultCache)) {
61+
return self::$resultCache[$regex];
62+
}
63+
64+
return self::$resultCache[$regex] = $this->parse($regex);
65+
}
66+
67+
/**
68+
* @return array{array<int, RegexCapturingGroup>, list<string>}|null
69+
*/
70+
private function parse(string $regex): ?array
5671
{
5772
if (self::$parser === null) {
5873
/** @throws void */

0 commit comments

Comments
 (0)