diff --git a/src/Metadata/Api/Groups.php b/src/Metadata/Api/Groups.php index 73341851a4b..1a2b23c4732 100644 --- a/src/Metadata/Api/Groups.php +++ b/src/Metadata/Api/Groups.php @@ -10,6 +10,7 @@ namespace PHPUnit\Metadata\Api; use function array_flip; +use function array_key_exists; use function array_unique; use function assert; use function strtolower; @@ -29,6 +30,11 @@ */ final class Groups { + /** + * @var array> + */ + private static array $groupCache = []; + /** * @psalm-param class-string $className * @psalm-param non-empty-string $methodName @@ -37,6 +43,12 @@ final class Groups */ public function groups(string $className, string $methodName, bool $includeVirtual = true): array { + $key = $className . '::' . $methodName . '::' . $includeVirtual; + + if (array_key_exists($key, self::$groupCache)) { + return self::$groupCache[$key]; + } + $groups = []; foreach (Registry::parser()->forClassAndMethod($className, $methodName)->isGroup() as $group) { @@ -50,7 +62,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu } if (!$includeVirtual) { - return array_unique($groups); + return self::$groupCache[$key] = array_unique($groups); } foreach (Registry::parser()->forClassAndMethod($className, $methodName) as $metadata) { @@ -85,7 +97,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu } } - return array_unique($groups); + return self::$groupCache[$key] = array_unique($groups); } /**