From 57efe8ca678e5a8d92c1ddbac74213785a90cc5d Mon Sep 17 00:00:00 2001 From: Evandro Myller Date: Thu, 27 Nov 2025 18:16:38 -0300 Subject: [PATCH] Exclude identities when PERCENTAGE_SPLIT trait is undefined Co-authored-by: Claude --- .gitmodules | 2 +- src/Engine/Engine.php | 22 ++++++++++++---------- tests/Engine/EngineTests/EngineTestData | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.gitmodules b/.gitmodules index 15a9644..5e04c6d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "tests/Engine/EngineTests/EngineTestData"] path = tests/Engine/EngineTests/EngineTestData url = git@github.com:Flagsmith/engine-test-data.git - tag = v3.4.1 + tag = v3.5.0 diff --git a/src/Engine/Engine.php b/src/Engine/Engine.php index d5f70ce..bde35e2 100644 --- a/src/Engine/Engine.php +++ b/src/Engine/Engine.php @@ -253,7 +253,15 @@ private static function _contextMatchesCondition( $condition, $segmentKey, ): bool { - $contextValue = self::_getContextValue($context, $condition->property); + // NOTE: Currently, the only supported condition with a blank property is percentage split. In this + // case, we use the identity key as context value. This is mainly to support legacy segments created + // before we introduced JSONPath support. + if ($condition->operator === SegmentConditionOperator::PERCENTAGE_SPLIT && empty($condition->property)) { + $contextValue = $context->identity?->key; + } else { + $contextValue = self::_getContextValue($context, $condition->property); + } + $cast = self::_getCaster($contextValue); switch ($condition->operator) { @@ -282,22 +290,16 @@ private static function _contextMatchesCondition( return in_array($contextValue, $inValues, strict: true); case SegmentConditionOperator::PERCENTAGE_SPLIT: - if (!is_numeric($condition->value)) { + if ($contextValue === null) { return false; } - - /** @var array $objectIds */ - if ($contextValue !== null) { - $objectIds = [$segmentKey, $contextValue]; - } elseif ($context->identity !== null) { - $objectIds = [$segmentKey, $context->identity->key]; - } else { + if (!is_numeric($condition->value)) { return false; } $hashing = new Hashing(); $threshold = $hashing->getHashedPercentageForObjectIds( - $objectIds, + [$segmentKey, $contextValue], ); return $threshold <= ((float) $condition->value); diff --git a/tests/Engine/EngineTests/EngineTestData b/tests/Engine/EngineTests/EngineTestData index 839e8d5..7840a13 160000 --- a/tests/Engine/EngineTests/EngineTestData +++ b/tests/Engine/EngineTests/EngineTestData @@ -1 +1 @@ -Subproject commit 839e8d5e5f2e9af6392062cf5e575d43c03770d4 +Subproject commit 7840a1349b601df3b6b4a089f40864f659801afb