Skip to content

Fix wrong signature for DateInterval::createFromDateString in 8.3+#5483

Open
sunkan wants to merge 4 commits intophpstan:2.1.xfrom
sunkan:bug-14479
Open

Fix wrong signature for DateInterval::createFromDateString in 8.3+#5483
sunkan wants to merge 4 commits intophpstan:2.1.xfrom
sunkan:bug-14479

Conversation

@sunkan
Copy link
Copy Markdown

@sunkan sunkan commented Apr 16, 2026

Fixes phpstan/phpstan#14479

Summary

In php8.3 the signature changed from

DateInterval::createFromDateString(string $datetime): DateInterval|false

to

DateInterval::createFromDateString(string $datetime): DateInterval

now it instead throws an DateMalformedStringException

Reference: https://www.php.net/manual/en/dateinterval.createfromdatestring.php


This pr collides with the fix for phpstan/phpstan#8442 but I don't know how to solve that.

@ondrejmirtes
Copy link
Copy Markdown
Member

The linked issue is closed. Please first create an issue from one of the templates at https://github.com/phpstan/phpstan/issues/new/choose and provide a failing playground example.

@sunkan
Copy link
Copy Markdown
Author

sunkan commented Apr 16, 2026

I did use the "Function signature mismatch"

And it says

<!-- Please send a pull request in phpstan/phpstan-src that updates file resources/functionMap.php instead. Thank you. -->

@VincentLanglet
Copy link
Copy Markdown
Contributor

VincentLanglet commented Apr 16, 2026

Exemple of the issue https://phpstan.org/r/213cc168-576f-49eb-b1e3-f32de3fdf0bd

The DateIntervalDynamicReturnTypeExtension need to be updated too

use function count;

#[AutowiredService]
final class DateIntervalCreateFromDateStringMethodThrowTypeExtension implements DynamicStaticMethodThrowTypeExtension
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this extension needs testing.

see e.g. 5645c04#diff-0ec4f831901651eabb90c05586b3404e02c820361f0df02b7789a381c65d1f7d on how other *ThrowTypeExtensions have been tested in the past

$possibleReturnTypes[] = false;
continue;
}
// @phpstan-ignore instanceof.alwaysTrue (should only run for < 8.3 and then statement isn't true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can get arround this by moving the above newly added return cases after the try/catch into the if/else block, which also does the return-type determination for the PHP < 8.3 case

@VincentLanglet
Copy link
Copy Markdown
Contributor

You're getting +809: Dead catch - DateMalformedIntervalStringException is never thrown in the try block. because the default throwType is not correct.

I'm working on it JetBrains/phpstorm-stubs#1874

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.

4 participants