Skip to content

Commit

Permalink
Micro: cache Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Mar 13, 2024
1 parent 59ad5c9 commit cfbe088
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Metadata/Api/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,11 @@
*/
final class Groups
{
/**
* @var array<string, list<string>>
*/
private static array $groupCache = [];

/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
Expand All @@ -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) {
Expand All @@ -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);

Check failure on line 65 in src/Metadata/Api/Groups.php

View workflow job for this annotation

GitHub Actions / Type Checker

PropertyTypeCoercion

src/Metadata/Api/Groups.php:65:20: PropertyTypeCoercion: PHPUnit\Metadata\Api\Groups::$groupCache expects 'array<string, list<string>>', parent type 'non-empty-array<string, non-empty-array<int<0, max>, string>>' provided (see https://psalm.dev/198)

Check failure on line 65 in src/Metadata/Api/Groups.php

View workflow job for this annotation

GitHub Actions / Type Checker

LessSpecificReturnStatement

src/Metadata/Api/Groups.php:65:20: LessSpecificReturnStatement: The type 'non-empty-array<int<0, max>, non-empty-string>' is more general than the declared return type 'list<string>' for PHPUnit\Metadata\Api\Groups::groups (see https://psalm.dev/129)
}

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

return array_unique($groups);
return self::$groupCache[$key] = array_unique($groups);

Check failure on line 100 in src/Metadata/Api/Groups.php

View workflow job for this annotation

GitHub Actions / Type Checker

PropertyTypeCoercion

src/Metadata/Api/Groups.php:100:16: PropertyTypeCoercion: PHPUnit\Metadata\Api\Groups::$groupCache expects 'array<string, list<string>>', parent type 'non-empty-array<string, non-empty-array<int<0, max>, string>>' provided (see https://psalm.dev/198)

Check failure on line 100 in src/Metadata/Api/Groups.php

View workflow job for this annotation

GitHub Actions / Type Checker

LessSpecificReturnStatement

src/Metadata/Api/Groups.php:100:16: LessSpecificReturnStatement: The type 'non-empty-array<int<0, max>, non-empty-string>' is more general than the declared return type 'list<string>' for PHPUnit\Metadata\Api\Groups::groups (see https://psalm.dev/129)
}

/**
Expand Down

0 comments on commit cfbe088

Please sign in to comment.