File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed
Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 2020use PHPStan \Type \StringType ;
2121use PHPStan \Type \Type ;
2222use PHPStan \Type \TypeCombinator ;
23+ use function array_key_exists ;
2324use function array_values ;
2425use function count ;
2526use 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 */
You can’t perform that action at this time.
0 commit comments