diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 708f6fd..1f5b512 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -23,6 +23,7 @@ jobs: bootstrap: vendor/autoload.php configuration: phpunit.xml args: --coverage-text + version: 9.6 env: XDEBUG_MODE: coverage @@ -45,3 +46,4 @@ jobs: files: "**.php" # you may customize glob as needed phpcs_path: php phpcs.phar standard: phpcs.xml + scope: "file" diff --git a/lib/Client.php b/lib/Client.php index 43e413c..51569db 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -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"]; @@ -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, + (int) ($options['timeout'] ?? 10000) ); $this->featureFlags = []; $this->groupTypeMapping = []; @@ -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; @@ -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; } @@ -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, diff --git a/lib/FeatureFlag.php b/lib/FeatureFlag.php index 25527d0..e35ba45 100644 --- a/lib/FeatureFlag.php +++ b/lib/FeatureFlag.php @@ -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; @@ -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]; diff --git a/lib/HttpClient.php b/lib/HttpClient.php index 43099b8..4fc28e5 100644 --- a/lib/HttpClient.php +++ b/lib/HttpClient.php @@ -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; diff --git a/lib/PostHog.php b/lib/PostHog.php index 07ea331..b872dcc 100644 --- a/lib/PostHog.php +++ b/lib/PostHog.php @@ -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"; @@ -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); @@ -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 + ); } /** @@ -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 + ); } /** @@ -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 + ); } @@ -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"); } /** diff --git a/test/MockedHttpClient.php b/test/MockedHttpClient.php index ec6a27f..328e109 100644 --- a/test/MockedHttpClient.php +++ b/test/MockedHttpClient.php @@ -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; } diff --git a/test/PostHogTest.php b/test/PostHogTest.php index b9e11b3..2c5687c 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -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 + ), ), ) ); @@ -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, @@ -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 + ), ), ) );