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

Make timeout configurable #44

Merged
merged 10 commits into from
Mar 21, 2023
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
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text
version: 9.6
env:
XDEBUG_MODE: coverage

Expand All @@ -45,3 +46,4 @@ jobs:
files: "**.php" # you may customize glob as needed
phpcs_path: php phpcs.phar
standard: phpcs.xml
scope: "file"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bug that causes the default to not respect the standards xml: tinovyatkin/action-php-codesniffer#40

43 changes: 34 additions & 9 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ class Client
* @param array $options array of consumer options [optional]
* @param HttpClient|null $httpClient
*/
public function __construct(string $apiKey, array $options = [], ?HttpClient $httpClient = null, string $personalAPIKey = null)
{
public function __construct(
string $apiKey,
array $options = [],
?HttpClient $httpClient = null,
string $personalAPIKey = null
) {
$this->apiKey = $apiKey;
$this->personalAPIKey = $personalAPIKey;
$Consumer = self::CONSUMERS[$options["consumer"] ?? "lib_curl"];
Expand All @@ -76,7 +80,9 @@ public function __construct(string $apiKey, array $options = [], ?HttpClient $ht
$options['ssl'] ?? true,
(int) ($options['maximum_backoff_duration'] ?? 10000),
false,
$options["debug"] ?? false
$options["debug"] ?? false,
null,
liyiy marked this conversation as resolved.
Show resolved Hide resolved
(int) ($options['timeout'] ?? 10000)
);
$this->featureFlags = [];
$this->groupTypeMapping = [];
Expand Down Expand Up @@ -162,7 +168,15 @@ public function isFeatureEnabled(
bool $onlyEvaluateLocally = false,
bool $sendFeatureFlagEvents = true
): null | bool {
$result = $this->getFeatureFlag($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
$result = $this->getFeatureFlag(
$key,
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally,
$sendFeatureFlagEvents
);

if (is_null($result)) {
return $result;
Expand Down Expand Up @@ -339,9 +353,16 @@ private function computeFlagLocally(
* @return array of feature flags
* @throws Exception
*/
public function fetchFeatureVariants(string $distinctId, array $groups = array(), array $personProperties = [], array $groupProperties = []): array
{
$flags = json_decode($this->decide($distinctId, $groups, $personProperties, $groupProperties), true)['featureFlags'] ?? [];
public function fetchFeatureVariants(
string $distinctId,
array $groups = array(),
array $personProperties = [],
array $groupProperties = []
): array {
$flags = json_decode(
$this->decide($distinctId, $groups, $personProperties, $groupProperties),
true
)['featureFlags'] ?? [];
return $flags;
}

Expand Down Expand Up @@ -376,8 +397,12 @@ public function localFlags()
)->getResponse();
}

public function decide(string $distinctId, array $groups = array(), array $personProperties = [], array $groupProperties = [])
{
public function decide(
string $distinctId,
array $groups = array(),
array $personProperties = [],
array $groupProperties = []
) {
$payload = array(
'api_key' => $this->apiKey,
'distinct_id' => $distinctId,
Expand Down
16 changes: 8 additions & 8 deletions lib/FeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ private static function compareFlagConditions($conditionA, $conditionB)

if ($AhasVariantOverride && $BhasVariantOverride) {
return 0;
} else if ($AhasVariantOverride) {
} elseif ($AhasVariantOverride) {
return -1;
} else if ($BhasVariantOverride) {
} elseif ($BhasVariantOverride) {
return 1;
} else {
return 0;
Expand All @@ -155,20 +155,20 @@ public static function matchFeatureFlagProperties($flag, $distinctId, $propertie
$flagConditionsWithIndexes[] = array($value, $i);
$i++;
}
// # Stable sort conditions with variant overrides to the top. This ensures that if overrides are present, they are
// # Stable sort conditions with variant overrides to the top.
// # This ensures that if overrides are present, they are
// # evaluated first, and the variant override is applied to the first matching condition.
usort(
$flagConditionsWithIndexes,
function ($conditionA, $conditionB)
{
function ($conditionA, $conditionB) {
$AhasVariantOverride = isset($conditionA[0]["variant"]);
$BhasVariantOverride = isset($conditionB[0]["variant"]);

if ($AhasVariantOverride && $BhasVariantOverride) {
return $conditionA[1] - $conditionB[1];
} else if ($AhasVariantOverride) {
} elseif ($AhasVariantOverride) {
return -1;
} else if ($BhasVariantOverride) {
} elseif ($BhasVariantOverride) {
return 1;
} else {
return $conditionA[1] - $conditionB[1];
Expand Down
2 changes: 1 addition & 1 deletion lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
bool $compressRequests = false,
bool $debug = false,
?Closure $errorHandler = null,
int $curlTimeoutMilliseconds = 750
int $curlTimeoutMilliseconds = 10000
) {
$this->host = $host;
$this->useSsl = $useSsl;
Expand Down
40 changes: 33 additions & 7 deletions lib/PostHog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class PostHog
{
public const VERSION = '3.0.2';
public const VERSION = '3.0.3';
public const ENV_API_KEY = "POSTHOG_API_KEY";
public const ENV_HOST = "POSTHOG_HOST";

Expand All @@ -19,8 +19,12 @@ class PostHog
* @param Client|null $client
* @throws Exception
*/
public static function init(?string $apiKey = null, ?array $options = [], ?Client $client = null, ?string $personalAPIKey = null): void
{
public static function init(
?string $apiKey = null,
?array $options = [],
?Client $client = null,
?string $personalAPIKey = null
): void {
if (null === $client) {
$apiKey = $apiKey ?: getenv(self::ENV_API_KEY);

Expand Down Expand Up @@ -124,7 +128,15 @@ public static function isFeatureEnabled(
bool $sendFeatureFlagEvents = true
): null | bool {
self::checkClient();
return self::$client->isFeatureEnabled($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
return self::$client->isFeatureEnabled(
$key,
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally,
$sendFeatureFlagEvents
);
}

/**
Expand All @@ -148,7 +160,15 @@ public static function getFeatureFlag(
bool $sendFeatureFlagEvents = true
): null | bool | string {
self::checkClient();
return self::$client->GetFeatureFlag($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
return self::$client->GetFeatureFlag(
$key,
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally,
$sendFeatureFlagEvents
);
}

/**
Expand All @@ -169,7 +189,13 @@ public static function getAllFlags(
bool $onlyEvaluateLocally = false
): array {
self::checkClient();
return self::$client->getAllFlags($distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally);
return self::$client->getAllFlags(
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally
);
}


Expand Down Expand Up @@ -224,7 +250,7 @@ public static function raw(array $message)
public static function validate($msg, $type)
{
$distinctId = !empty($msg["distinctId"]);
self::assert($distinctId, "PostHog::${type}() requires distinctId");
self::assert($distinctId, "PostHog::{$type}() requires distinctId");
}

/**
Expand Down
10 changes: 9 additions & 1 deletion test/MockedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ public function __construct(
int $curlTimeoutMilliseconds = 750,
array $flagEndpointResponse = []
) {
parent::__construct($host, $useSsl, $maximumBackoffDuration, $compressRequests, $debug, $errorHandler, $curlTimeoutMilliseconds);
parent::__construct(
$host,
$useSsl,
$maximumBackoffDuration,
$compressRequests,
$debug,
$errorHandler,
$curlTimeoutMilliseconds
);
$this->flagEndpointResponse = $flagEndpointResponse;
}

Expand Down
15 changes: 12 additions & 3 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public function testIsFeatureEnabledGroups()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}', FAKE_API_KEY),
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}',
FAKE_API_KEY
),
),
)
);
Expand Down Expand Up @@ -164,7 +167,10 @@ public function testGetFeatureFlagDefault()

public function testGetFeatureFlagGroups()
{
$this->assertEquals("variant-value", PostHog::getFeatureFlag('multivariate-test', 'user-id', array("company" => "id:5")));
$this->assertEquals(
"variant-value",
PostHog::getFeatureFlag('multivariate-test', 'user-id', array("company" => "id:5"))
);

$this->assertEquals(
$this->http_client->calls,
Expand All @@ -175,7 +181,10 @@ public function testGetFeatureFlagGroups()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}', FAKE_API_KEY),
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}',
FAKE_API_KEY
),
),
)
);
Expand Down