-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[VarDumper] Dumping DateTime throws error if getTimezone is false #50644
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
Conversation
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test that covers your change.
Thank you for your PR. I disagree with the motivation of your change for two reasons:
However as per PHP's documentation, I'm fine with the change, once my review has been addressed. |
Thanks for the quick reply, you are right about the motivation, what I was thinking is that since PHP enables you to return false, then of course that allows for some special cases like dumping a Mock, and it is possible without a Mock also. |
$location = $d->getTimezone()->getLocation(); | ||
$fromNow = (new \DateTime())->diff($d); | ||
$location = $d->getTimezone() ? $d->getTimezone()->getLocation() : null; | ||
$fromNow = $d->getTimezone() ? (new \DateTime())->diff($d) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted this change
e20b306
to
d6bc7a4
Compare
Thank you @bogdanmoza. |
TL;DR:
In case you have a DateTime object that returns false when calling
getTimezone()
, an error is thrown in DateCaster.Description
So I have encountered this case when I have tried to dump a mocked DateTime object. The mock will not have a timezone making the
getTimezone()
method return false. I know that this is not really a bug, as a DateTime object not having a DateTimeZone should not be happening, but it is possible in case someone extends the DateTime object and overrides thegetTimezone()
method.The error thrown is 'The DatetimeInterface object has not been correctly initialized by its constructor'
How to reproduce
Dump the A class will cause an error.
Final notes
Even though this a special case, I still think that you should be able to dump a mocked DateTime.
I hope the description and everything is ok as this is my first pr here.
Cheers!