Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache result of Groups::groups() #5747

Merged
merged 3 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 0 additions & 7 deletions .psalm/baseline.xml
Expand Up @@ -640,13 +640,6 @@
</RedundantCondition>
</file>
<file src="src/Metadata/Api/Groups.php">
<LessSpecificReturnStatement>
<code><![CDATA[array_unique($groups)]]></code>
<code><![CDATA[array_unique($groups)]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<string>]]></code>
</MoreSpecificReturnType>
<RedundantCondition>
<code><![CDATA[$metadata instanceof CoversFunction]]></code>
<code><![CDATA[$metadata instanceof UsesFunction]]></code>
Expand Down
18 changes: 15 additions & 3 deletions src/Metadata/Api/Groups.php
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,14 +30,25 @@
*/
final class Groups
{
/**
* @var array<string, array<int, string>>
*/
private static array $groupCache = [];

/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @psalm-return list<string>
* @psalm-return array<int, string>
*/
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);
}

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);
}

/**
Expand Down