Skip to content

Commit 7e9475d

Browse files
staabmsebastianbergmann
authored andcommitted
Micro: cache Groups
1 parent 59ad5c9 commit 7e9475d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Metadata/Api/Groups.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPUnit\Metadata\Api;
1111

1212
use function array_flip;
13+
use function array_key_exists;
1314
use function array_unique;
1415
use function assert;
1516
use function strtolower;
@@ -29,6 +30,11 @@
2930
*/
3031
final class Groups
3132
{
33+
/**
34+
* @var array<string, list<string>>
35+
*/
36+
private static array $groupCache = [];
37+
3238
/**
3339
* @psalm-param class-string $className
3440
* @psalm-param non-empty-string $methodName
@@ -37,6 +43,12 @@ final class Groups
3743
*/
3844
public function groups(string $className, string $methodName, bool $includeVirtual = true): array
3945
{
46+
$key = $className . '::' . $methodName . '::' . $includeVirtual;
47+
48+
if (array_key_exists($key, self::$groupCache)) {
49+
return self::$groupCache[$key];
50+
}
51+
4052
$groups = [];
4153

4254
foreach (Registry::parser()->forClassAndMethod($className, $methodName)->isGroup() as $group) {
@@ -50,7 +62,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu
5062
}
5163

5264
if (!$includeVirtual) {
53-
return array_unique($groups);
65+
return self::$groupCache[$key] = array_unique($groups);
5466
}
5567

5668
foreach (Registry::parser()->forClassAndMethod($className, $methodName) as $metadata) {
@@ -85,7 +97,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu
8597
}
8698
}
8799

88-
return array_unique($groups);
100+
return self::$groupCache[$key] = array_unique($groups);
89101
}
90102

91103
/**

0 commit comments

Comments
 (0)