diff --git a/lib/Client.php b/lib/Client.php index abf1e7c..b490864 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -231,7 +231,11 @@ public function getFeatureFlag( if (!$flagWasEvaluatedLocally && !$onlyEvaluateLocally) { try { $featureFlags = $this->fetchFeatureVariants($distinctId, $groups, $personProperties, $groupProperties); - $result = $featureFlags[$key] ?? null; + if(array_key_exists($key, $featureFlags)) { + $result = $featureFlags[$key]; + } else { + $result = null; + } } catch (Exception $e) { error_log("[PostHog][Client] Unable to get feature variants:" . $e->getMessage()); $result = null; diff --git a/test/FeatureFlagTest.php b/test/FeatureFlagTest.php index bdaa60b..3fbb84c 100644 --- a/test/FeatureFlagTest.php +++ b/test/FeatureFlagTest.php @@ -11,7 +11,7 @@ use PostHog\InconclusiveMatchException; use PostHog\SizeLimitedHash; -class FeatureFlagMatch extends TestCase +class FeatureFlagTest extends TestCase { const FAKE_API_KEY = "random_key"; @@ -516,6 +516,23 @@ public function testFlagFallbackToDecide() $this->assertEquals(PostHog::getFeatureFlag('feature-2', 'some-distinct'), 'decide-fallback-value'); } + public function testFlagFallbackToDecideWithFalseFlag() + { + $this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse: MockedResponses::FALLBACK_TO_DECIDE_REQUEST); + $this->client = new Client( + self::FAKE_API_KEY, + [ + "debug" => true, + ], + $this->http_client, + "test" + ); + PostHog::init(null, null, $this->client); + + $this->assertEquals(PostHog::getFeatureFlag('unknown-flag???', 'some-distinct'), null); + $this->assertEquals(PostHog::getFeatureFlag('false-flag', 'some-distinct'), null); + } + public function testFeatureFlagDefaultsComeIntoPlayOnlyWhenDecideErrorsOut() { $this->client = new Client(