New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
\DatetimeImmutable::modify signature change (vs \Datetime::modify) #8454
Comments
I found these snippets: https://psalm.dev/r/d94dac07ab<?php
function foo(\DateTimeImmutable $d): int {
/** @psalm-trace $a */
$a = $d->modify('+23 hours 59 minutes 59 seconds');
return $a->getTimestamp();
}
function bar(\DateTime $d): int {
/** @psalm-trace $a */
$a = $d->modify('+23 hours 59 minutes 59 seconds');
return $a->getTimestamp();
}
|
This is correct: From PHP 8.2.0-RC1: https://github.com/php/php-src/blob/6a0e11445d0eb7edf3fae196032ffa399137082c/ext/date/php_date.c#L3077-L3091 |
I agree, it's correct, but \DateTime::modify can return false too and doesn't work the same way for psalm.
We should look for consistency. I was wondering if some Benevolent were used for \DateTime::modify which would have explained that no error were found Also
is not false. So it might be worth to implement something similar to |
TBH, I didn't touch The |
Interesting, do you have an example ? Phpstan only checks the modify input https://github.com/phpstan/phpstan-src/blob/f1fd385433480f87bdda1d7d8ff6887e25f003ba/conf/config.neon#L1264-L1269 Currently I have the issue with
|
Btw, all commits from #8350 are detailed on why these choices were made: please read up on them. |
Not sure if anything is actionnable here then? It seems that the false case can happen (admittedly in very rare cases) that can't be statically inferred. So either PHPStan should start adding this in their definitions or there will be a difference between tools on that method. The only correct thing we could do would be to align DateTime if anyone need to |
Phpstan consider that Even with the tweet or the commit messages, I still don't understand when
can return false. If it's never false, psalm should add some dynamic return type resolver. (For datetime and datetimeimmutable) Do you have a php example @orklah ? |
You'd probably need to look in the PHP test suite for one: what has been done here is just reflecting the code paths that I already linked above. |
I'm really sorry but I did look and still don't understand. You linked https://github.com/php/php-src/blob/6a0e11445d0eb7edf3fae196032ffa399137082c/ext/date/php_date.c#L3077-L3091, and I looked at tests, and the only one I found about it was https://github.com/php/php-src/blob/b46632e9e478f1f8205f8c62b2e0738f36005427/ext/date/tests/DateTimeImmutable_modify_invalid_format.phpt and also looked your commit message 7cd3d49 I never said this method was not returning false. But I thought we could statically guess the return type, like phpstan does.
to know if the return value will be false or a DateTime/DateTimeImmutable. Psalm could do the same. Even re-reading multiple times the discussion I still don't understand how the modify return type is related to the value passed when instantiating the DateTime/DateTimeImmutable. You said
is false but
is a \DateTimeImmutable You also linked https://github.com/php/php-src/blob/534127d3b22b193ffb9511c4447584f0d2bd4e24/ext/date/php_date.c#L3157-L3160 but I don't understand how the code in the |
It doesn't: I asked the author of The code path returning
The problem is when you don't have BOTH the That said, if you are this passionate about this issue, my suggestion is that you raise an RFC for PHP 8.3, to remove the edge case completely, and remove the last few If you can demonstrate that the date/time functions NEVER return Once that's done, it should be possible to do it here too, if that is verifiable. |
So we need to wait an example from him then.
But that's what you got told without an example. So we can consider implementing the same here, unless someone has a counter example.
Again, that's another topic. The question is not weither the false return type exists or not (or should exist). But I feel like I'm loosing your time since we dont succeed to understand each other. |
@Ocramius I see a |
Ooooh, interesting, so in theory it should not be possible to ever land in a Perhaps I took the advice too eagerly? Happy to adjust accordingly here, if anyone else can check @AndrolGenhald and confirm (I'm too sleep deprived, but I skimmed the linked code, and it may really be that |
I'm a bit confused as to how we got to talking about
But as far as I can tell, the false return from |
If this is confirmed I opened #8462 |
@Ocramius The It indeed gives a warning and returns a clone, so there's no Also, go get some sleep 😛 |
Currently https://psalm.dev/r/d94dac07ab is not reflecting the 4.27 release.
I'm running psalm on php 7.4 version
is reported as DateTimeImmutable&static|false
and I have an error reported when calling
getTimestamp
on it.This is a new error related to the changes in #8350 (@Ocramius, @orklah, @AndrolGenhald)
What is weird is the fact that
is reported as DateTime&static|false
but not error is reported when calling
getTimestamp
on it.If psalm is using a benevolent union, it seems like the recent DatetimeImmutable stub lost it.
The text was updated successfully, but these errors were encountered: