diff --git a/.changeset/empty-scopes-stop.md b/.changeset/empty-scopes-stop.md new file mode 100644 index 0000000..092b412 --- /dev/null +++ b/.changeset/empty-scopes-stop.md @@ -0,0 +1,5 @@ +--- +'posthog-php': patch +--- + +Return an empty feature flag snapshot without evaluation requests when the requested flag key list is empty. diff --git a/lib/Client.php b/lib/Client.php index 6fd0e85..8f496b4 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -1044,9 +1044,10 @@ public function getAllFlags( * @param bool $disableGeoip Whether to disable GeoIP enrichment during remote evaluation. * @param list|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( @@ -1069,6 +1070,15 @@ public function evaluateFlags( ); } + if ($flagKeys === []) { + return new FeatureFlagEvaluations( + $distinctId, + [], + $groups, + $this, + ); + } + [$personProperties, $groupProperties] = $this->addLocalPersonAndGroupProperties( $groups, $personProperties, diff --git a/test/FeatureFlagEvaluationsTest.php b/test/FeatureFlagEvaluationsTest.php index 3c59364..0c8fa12 100644 --- a/test/FeatureFlagEvaluationsTest.php +++ b/test/FeatureFlagEvaluationsTest.php @@ -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();