Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-scopes-stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-php': patch
---

Return an empty feature flag snapshot without evaluation requests when the requested flag key list is empty.
16 changes: 13 additions & 3 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,10 @@ public function getAllFlags(
* @param bool $disableGeoip Whether to disable GeoIP enrichment during remote evaluation.
* @param list<string>|null $flagKeys Optional list of flag keys. When provided, only these
* flags are evaluated — the underlying /flags request asks the server for just this
* subset, which makes the response smaller and the request cheaper. Use this when you
* only need a handful of flags out of many. Distinct from FeatureFlagEvaluations::only(),
* which scopes which already-evaluated flags get attached to a captured event.
* subset, which makes the response smaller and the request cheaper. An empty list returns
* an empty snapshot without local or remote evaluation. Use this when you only need a
* handful of flags out of many. Distinct from FeatureFlagEvaluations::only(), which scopes
* which already-evaluated flags get attached to a captured event.
* @return FeatureFlagEvaluations
*/
public function evaluateFlags(
Expand All @@ -1069,6 +1070,15 @@ public function evaluateFlags(
);
}

if ($flagKeys === []) {
Comment thread
marandaneto marked this conversation as resolved.
return new FeatureFlagEvaluations(
$distinctId,
[],
$groups,
$this,
);
}

[$personProperties, $groupProperties] = $this->addLocalPersonAndGroupProperties(
$groups,
$personProperties,
Expand Down
13 changes: 13 additions & 0 deletions test/FeatureFlagEvaluationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ public function testCaptureFlagsAttachesFeaturePropertiesWithoutHttpRequest(): v
}
}

public function testEmptyFlagKeysReturnsEmptySnapshotWithoutEvaluationRequests(): void
{
$this->makeClient();
$callsBefore = count($this->http_client->calls ?? []);

$snapshot = PostHog::evaluateFlags('user-1', flagKeys: []);

$this->assertInstanceOf(FeatureFlagEvaluations::class, $snapshot);
$this->assertSame([], $snapshot->getKeys());
$this->assertCount($callsBefore, $this->http_client->calls ?? []);
$this->assertSame(0, $this->flagsRequestCount());
}

public function testFlagKeysIsForwardedInRequestBody(): void
{
$this->makeClient();
Expand Down
Loading