From e737ab90084008cddd0ef11111618915b97ccd70 Mon Sep 17 00:00:00 2001 From: zigzagdev <47250974+zigzagdev@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:10:47 +0900 Subject: [PATCH 1/2] fix: Prefer a targeting key of "0" over the key attribute getTargetingKey() used `$targetingKey ?: $key` to fall back to the key attribute. "0" is falsy in PHP, so a targeting key of "0" was replaced by the key attribute, even though the targeting key should take precedence. Fall back to the key attribute only when the targeting key is null or an empty string, matching the check earlier in the same method. --- src/EvaluationContextConverter.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/EvaluationContextConverter.php b/src/EvaluationContextConverter.php index 0300e1f..2a0a746 100644 --- a/src/EvaluationContextConverter.php +++ b/src/EvaluationContextConverter.php @@ -80,9 +80,10 @@ private function getTargetingKey(?string $targetingKey, $key): string $this->logger->warning("A non-string 'key' attribute was provided."); } - if ($key !== null && is_string($key)) { - // Targeting key takes precedence over key, because targeting key is in the spec. - $targetingKey = $targetingKey ?: $key; + // Targeting key takes precedence over key, because targeting key is in the spec. + // Compare against "" explicitly, because "0" is falsy in PHP but is a valid key. + if (($targetingKey === null || $targetingKey === "") && is_string($key)) { + $targetingKey = $key; } if ($targetingKey === null || $targetingKey === "") { From d7bb0285a15558934d2c24d309b4c3df49639b94 Mon Sep 17 00:00:00 2001 From: zigzagdev <47250974+zigzagdev@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:10:47 +0900 Subject: [PATCH 2/2] test: Cover a targeting key of "0" taking precedence over the key attribute Add single-context and multi-kind context cases, since both go through getTargetingKey(). --- tests/EvaluationContextConverterTest.php | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/EvaluationContextConverterTest.php b/tests/EvaluationContextConverterTest.php index b70c666..cdf3738 100644 --- a/tests/EvaluationContextConverterTest.php +++ b/tests/EvaluationContextConverterTest.php @@ -58,6 +58,34 @@ public function testTargetingKeyTakesPrecedenceOverAttributeKey(): void $this->assertEquals("should-use", $ldContext->getKey()); } + public function testZeroTargetingKeyTakesPrecedenceOverAttributeKey(): void + { + $attributes = new Attributes(["key" => "do-not-use"]); + $context = new EvaluationContext("0", $attributes); + + $ldContext = $this->contextConverter->toLdContext($context); + + $this->assertTrue($ldContext->isValid()); + $this->assertEquals("0", $ldContext->getKey()); + } + + public function testZeroTargetingKeyTakesPrecedenceOverAttributeKeyInMultiContext(): void + { + $attributes = [ + 'kind' => 'multi', + 'user' => ['targetingKey' => '0', 'key' => 'do-not-use'], + ]; + $context = new EvaluationContext(null, new Attributes($attributes)); + + $ldContext = $this->contextConverter->toLdContext($context); + + $this->assertTrue($ldContext->isValid()); + + /** @var \LaunchDarkly\LDContext */ + $userContext = $ldContext->getIndividualContext("user"); + $this->assertEquals("0", $userContext->getKey()); + } + public function testCreateContextWithKeyAndKind(): void { $attributes = new Attributes(["kind" => "org"]);