Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/relative-date-month-clamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-php": patch
---

Relative date filters in local flag evaluation no longer roll past the end of a shorter month. `1m` on March 31 now resolves to February 28 instead of March 3.
75 changes: 65 additions & 10 deletions lib/FeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,45 @@ private static function matchPropertyValue($property, $propertyValues, $property
}

if ($operator == "icontains") {
return strpos(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))) !== false;
return strpos(
strtolower(FeatureFlag::valueToString($overrideValue)),
strtolower(FeatureFlag::valueToString($value))
) !== false;
}

if ($operator == "not_icontains") {
return strpos(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))) === false;
return strpos(
strtolower(FeatureFlag::valueToString($overrideValue)),
strtolower(FeatureFlag::valueToString($value))
) === false;
}

if ($operator == "starts_with") {
return str_starts_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value)));
return str_starts_with(
strtolower(FeatureFlag::valueToString($overrideValue)),
strtolower(FeatureFlag::valueToString($value))
);
}

if ($operator == "not_starts_with") {
return !str_starts_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value)));
return !str_starts_with(
strtolower(FeatureFlag::valueToString($overrideValue)),
strtolower(FeatureFlag::valueToString($value))
);
}

if ($operator == "ends_with") {
return str_ends_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value)));
return str_ends_with(
strtolower(FeatureFlag::valueToString($overrideValue)),
strtolower(FeatureFlag::valueToString($value))
);
}

if ($operator == "not_ends_with") {
return !str_ends_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value)));
return !str_ends_with(
strtolower(FeatureFlag::valueToString($overrideValue)),
strtolower(FeatureFlag::valueToString($value))
);
}

if (in_array($operator, ["regex", "not_regex"])) {
Expand Down Expand Up @@ -529,9 +547,9 @@ public static function relativeDateParseForFeatureFlagMatching($value)
} elseif ($interval == "w") {
$parsedDt->sub(new \DateInterval("P{$number}W"));
} elseif ($interval == "m") {
$parsedDt->sub(new \DateInterval("P{$number}M"));
FeatureFlag::subtractMonths($parsedDt, $number);
} elseif ($interval == "y") {
$parsedDt->sub(new \DateInterval("P{$number}Y"));
FeatureFlag::subtractYears($parsedDt, $number);
} else {
return null;
}
Expand All @@ -542,6 +560,39 @@ public static function relativeDateParseForFeatureFlagMatching($value)
}
}

/**
* Step back one month at a time and clamp the day after every step, as the SDK
* spec requires: March 31 minus two months is January 28. DateInterval would
* normalize February 31 forward into March.
*/
private static function subtractMonths(\DateTime $date, int $months): void
{
$year = (int) $date->format('Y');
$month = (int) $date->format('n');
$day = (int) $date->format('j');
// Every month has at least 28 days, so nothing can clamp further after that.
for ($step = 1; $step <= $months && $day > 28; $step++) {
// Day 0 of a month is the last day of the month before it.
$lastDay = (int) (new \DateTime('@0'))->setDate($year, $month - $step + 1, 0)->format('j');
$day = min($day, $lastDay);
}
$date->setDate($year, $month - $months, $day);
}

/**
* Step back whole years the same way. Only February 29 can clamp, and once it
* becomes February 28 in the year before, no earlier step changes it again.
*/
private static function subtractYears(\DateTime $date, int $years): void
{
$month = (int) $date->format('n');
$day = (int) $date->format('j');
if ($years > 0 && $month === 2 && $day === 29) {
$day = 28;
}
$date->setDate((int) $date->format('Y') - $years, $month, $day);
}

/**
* Parse a semver numeric identifier (e.g., the "1" in "1.2.3").
*
Expand Down Expand Up @@ -738,7 +789,10 @@ private static function wildcardBounds($value): array
$parts = explode(".", $text);

// Remove trailing wildcard parts and empty parts
while (count($parts) > 0 && (end($parts) === "*" || end($parts) === "x" || end($parts) === "X" || end($parts) === "")) {
while (
count($parts) > 0
&& (end($parts) === "*" || end($parts) === "x" || end($parts) === "X" || end($parts) === "")
) {
array_pop($parts);
}

Expand Down Expand Up @@ -1378,7 +1432,8 @@ private static function isConditionMatch(
$propertyMatchingVersion,
$identityAggregation
): ConditionMatch {
$rolloutPercentage = array_key_exists("rollout_percentage", $condition) ? $condition["rollout_percentage"] : null;
$rolloutPercentage = array_key_exists("rollout_percentage", $condition)
? $condition["rollout_percentage"] : null;

if (count($condition['properties'] ?? []) > 0) {
// Filters in a condition have no defined order, so an inconclusive filter does not stop
Expand Down
26 changes: 26 additions & 0 deletions test/FeatureFlagLocalEvaluationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,32 @@ public function testRelativeDateParsingMonths()
});
}

public static function relativeDateClampProvider(): array
{
return [
'one month from March 31' => ['2025-03-31T12:00:00Z', '1m', '2025-02-28T12:00:00Z'],
'one month from May 31' => ['2025-05-31T12:00:00Z', '1m', '2025-04-30T12:00:00Z'],
'one month into a leap February' => ['2024-03-31T12:00:00Z', '1m', '2024-02-29T12:00:00Z'],
'two months keep the February clamp' => ['2025-03-31T12:00:00Z', '2m', '2025-01-28T12:00:00Z'],
'thirteen months' => ['2025-03-31T12:00:00Z', '13m', '2024-02-28T12:00:00Z'],
'ten months from December 31' => ['2025-12-31T12:00:00Z', '10m', '2025-02-28T12:00:00Z'],
'twelve months pass through February' => ['2019-01-29T12:00:00Z', '12m', '2018-01-28T12:00:00Z'],
'one year does not' => ['2019-01-29T12:00:00Z', '1y', '2018-01-29T12:00:00Z'],
'zero months' => ['2025-03-31T12:00:00Z', '0m', '2025-03-31T12:00:00Z'],
'one year from February 29' => ['2024-02-29T12:00:00Z', '1y', '2023-02-28T12:00:00Z'],
'four years from February 29' => ['2024-02-29T12:00:00Z', '4y', '2020-02-28T12:00:00Z'],
'zero years from February 29' => ['2024-02-29T12:00:00Z', '0y', '2024-02-29T12:00:00Z'],
];
}

#[DataProvider('relativeDateClampProvider')]
public function testRelativeDateParsingClampsAfterEveryStep(string $now, string $value, string $expected)
{
$this->executeAtFrozenDateTime(new \DateTime($now), function () use ($value, $expected) {
self::assertEquals(new \DateTime($expected), FeatureFlag::relativeDateParseForFeatureFlagMatching($value));
});
}

public function testRelativeDateParsingYears()
{
$this->executeAtFrozenDateTime(new \DateTime('2020-01-01T12:01:20Z'), function () {
Expand Down
Loading