Skip to content

Fix DateInterval::createFromDateString return type for PHP 8.3+#11849

Open
xelaris wants to merge 1 commit into
vimeo:6.xfrom
xelaris:fix/date-interval-create-from-date-string-php83
Open

Fix DateInterval::createFromDateString return type for PHP 8.3+#11849
xelaris wants to merge 1 commit into
vimeo:6.xfrom
xelaris:fix/date-interval-create-from-date-string-php83

Conversation

@xelaris

@xelaris xelaris commented May 17, 2026

Copy link
Copy Markdown

On PHP 8.3 and newer DateInterval::createFromDateString() throws DateMalformedIntervalStringException on failure instead of returning false (see php/php-src#10366). Psalm still reports PossiblyFalseReference when calling a method on the return value of createFromDateString, even when targeting PHP 8.3 and newer.

See also https://www.php.net/manual/en/dateinterval.createfromdatestring.php#refsect1-dateinterval.createfromdatestring-changelog

The return type change was considered in CallMap_84_delta.php, but I think it should be in CallMap_83_delta.php.

The CallMap lookup seems to be skipped for Psalm's own stubs. I added a Php83.phpstub that overrides the createFromDateString definition in stubs/CoreGenericClasses.phpstub with return type DateInterval.

/**
* Sets up a DateInterval from the relative parts of the string
* @return DateInterval|false Returns a new {@link https://www.php.net/manual/en/class.dateinterval.php DateInterval}
* instance on success, or <b>FALSE</b> on failure.
* @link https://php.net/manual/en/dateinterval.createfromdatestring.php
* @psalm-ignore-falsable-return
*/
public static function createFromDateString(string $datetime = ''): DateInterval|false {}

There are two new test cases in CoreStubsTest. One is confirming the return type is still DateInterval|false for PHP 8.2 and the other one is confirming it's DateInterval on PHP 8.3.

Since PHP 8.3, DateInterval::createFromDateString() throws
DateMalformedIntervalStringException on invalid input instead of
returning false. Move the CallMap delta from 8.4 to 8.3 and add a
Php83.phpstub override so that both the CallMap and the stub return
the correct type.
@zoltan-schwarz

Copy link
Copy Markdown

I'm on php8.4, but it is still inferring false as possible return type. Tried direct cli argument --php-version= also defining in the config file, it's there in the composer.json. I even defined a local stub. Still I resolved false. Am I missing something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants