Infer numeric-string for DateInterval::format('%a') when interval comes from diff()#5674
Open
phpstan-bot wants to merge 3 commits into
Open
Infer numeric-string for DateInterval::format('%a') when interval comes from diff()#5674phpstan-bot wants to merge 3 commits into
numeric-string for DateInterval::format('%a') when interval comes from diff()#5674phpstan-bot wants to merge 3 commits into
Conversation
… comes from `diff()` - Extract shared format type inference logic into DateIntervalFormatReturnTypeHelper - Check if the receiver's `days` property is narrowed to `int` (indicating the interval came from DateTimeInterface::diff() or date_diff()) - When `days` is `int`, use a diff-created DateInterval for evaluating `%a` format specifier, which produces a numeric string instead of "(unknown)" - Add DateIntervalFormatFunctionReturnTypeExtension for the procedural date_interval_format() function, which previously had no type inference
Use a single $dateInterval selected based on $daysIsInt instead of maintaining two separate intervals and checking str_contains for %a. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
VincentLanglet
approved these changes
May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DateInterval::format('%a')was incorrectly inferred asnon-empty-string(not numeric) even when the DateInterval came fromDateTimeInterface::diff(). This caused false positives when usingformat('%a')in arithmetic operations like$interval->format('%a') * 60.The root cause: the extension evaluated format specifiers against
new DateInterval('P0D'), where%areturns(unknown)becausedaysisfalse. But afterdiff(),daysis narrowed tointand%areturns a numeric string.Changes
DateIntervalFormatDynamicReturnTypeExtensioninto a newDateIntervalFormatReturnTypeHelper(src/Type/Php/DateIntervalFormatReturnTypeHelper.php)daysproperty isint(indicating it came fromdiff()ordate_diff())daysisint, a diff-created DateInterval is used for evaluating%a, producing correct numeric-string inferenceDateIntervalFormatFunctionReturnTypeExtension(src/Type/Php/DateIntervalFormatFunctionReturnTypeExtension.php) for the proceduraldate_interval_format()function, which previously had no dynamic return type inference at allDateIntervalFormatDynamicReturnTypeExtensionto delegate to the helperRoot cause
The
DateIntervalFormatDynamicReturnTypeExtensiontested all format specifiers against a freshnew DateInterval('P0D'). For%a(total days), this produces(unknown)since thedaysproperty isfalsewhen the interval was not created viadiff(). However, PHPStan's stubs already narrowdiff()to returnDateInterval&object{days:int}, which means the%aspecifier will always produce a numeric string in that context.Test
tests/PHPStan/Analyser/nsrt/bug-1452.phptesting:DateTimeImmutable::diff()+format('%a')→numeric-stringDateTime::diff()+format('%a')→numeric-stringDateTimeInterface::diff()+format('%a')→numeric-stringdate_diff()+format('%a')→numeric-string%R%acombination →numeric-string(since+0is numeric)DateInterval(not from diff) +format('%a')→non-empty-string(not numeric)date_interval_format()function with both diff and non-diff intervalsformat('%a')result producesfloat|int(no error)Fixes phpstan/phpstan#1452