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 059bd83
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Metadata/Api/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
final class Groups
{
static private $groupCache = [];

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

View workflow job for this annotation

GitHub Actions / Type Checker

MissingPropertyType

src/Metadata/Api/Groups.php:32:20: MissingPropertyType: Property PHPUnit\Metadata\Api\Groups::$groupCache does not have a declared type - consider array<array-key, mixed|non-empty-array<int<0, max>, string>> (see https://psalm.dev/045)

/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
Expand All @@ -37,6 +39,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 +58,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 61 in src/Metadata/Api/Groups.php

View workflow job for this annotation

GitHub Actions / Type Checker

LessSpecificReturnStatement

src/Metadata/Api/Groups.php:61: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 +93,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 96 in src/Metadata/Api/Groups.php

View workflow job for this annotation

GitHub Actions / Type Checker

LessSpecificReturnStatement

src/Metadata/Api/Groups.php:96: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 059bd83

Please sign in to comment.