Skip to content

Commit

Permalink
fix(flags): Safe access flags in decide v2
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Sep 25, 2023
1 parent 3eed763 commit 1885bf5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion test/FeatureFlagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PostHog\InconclusiveMatchException;
use PostHog\SizeLimitedHash;

class FeatureFlagMatch extends TestCase
class FeatureFlagTest extends TestCase
{
const FAKE_API_KEY = "random_key";

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 1885bf5

Please sign in to comment.