Skip to content

Commit eab5568

Browse files
emyllerclaude
andauthored
Exclude identities when PERCENTAGE_SPLIT trait is undefined (#125)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9e459f5 commit eab5568

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "tests/Engine/EngineTests/EngineTestData"]
22
path = tests/Engine/EngineTests/EngineTestData
33
url = git@github.com:Flagsmith/engine-test-data.git
4-
tag = v3.4.1
4+
tag = v3.5.0

src/Engine/Engine.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ private static function _contextMatchesCondition(
253253
$condition,
254254
$segmentKey,
255255
): bool {
256-
$contextValue = self::_getContextValue($context, $condition->property);
256+
// NOTE: Currently, the only supported condition with a blank property is percentage split. In this
257+
// case, we use the identity key as context value. This is mainly to support legacy segments created
258+
// before we introduced JSONPath support.
259+
if ($condition->operator === SegmentConditionOperator::PERCENTAGE_SPLIT && empty($condition->property)) {
260+
$contextValue = $context->identity?->key;
261+
} else {
262+
$contextValue = self::_getContextValue($context, $condition->property);
263+
}
264+
257265
$cast = self::_getCaster($contextValue);
258266

259267
switch ($condition->operator) {
@@ -282,22 +290,16 @@ private static function _contextMatchesCondition(
282290
return in_array($contextValue, $inValues, strict: true);
283291

284292
case SegmentConditionOperator::PERCENTAGE_SPLIT:
285-
if (!is_numeric($condition->value)) {
293+
if ($contextValue === null) {
286294
return false;
287295
}
288-
289-
/** @var array<string> $objectIds */
290-
if ($contextValue !== null) {
291-
$objectIds = [$segmentKey, $contextValue];
292-
} elseif ($context->identity !== null) {
293-
$objectIds = [$segmentKey, $context->identity->key];
294-
} else {
296+
if (!is_numeric($condition->value)) {
295297
return false;
296298
}
297299

298300
$hashing = new Hashing();
299301
$threshold = $hashing->getHashedPercentageForObjectIds(
300-
$objectIds,
302+
[$segmentKey, $contextValue],
301303
);
302304
return $threshold <= ((float) $condition->value);
303305

0 commit comments

Comments
 (0)