Skip to content
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

fix create from format with invalid format #42

Conversation

niko-38500
Copy link
Contributor

e.g : date_create_immutable_from_format('d/m/Y', '2022-05-28')

return false instead of throwing an exception

…e_from_format('d/m/Y', '2022-05-28')

return false instead of throwing an exception
@@ -223,7 +223,13 @@ public static function mock_date_create_immutable_from_format(): callable
// Create an immutable instance starting from the mutable mock, so we don't have to replicate mocking logic.
$mutableDateTime = date_create_from_format($format, $datetime, $timezone);

return new \DateTimeImmutable($mutableDateTime->format('Y-m-d\TH:i:s.u'), $mutableDateTime->getTimezone());
return $mutableDateTime
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of treating a valid date object like a truthy value in a ternary statement, I believe it would be more explicit to make an early return if the return value from date_create_from_format is false, like that:

if ($mutableDateTime === false) {
    return false;
}

Also, a line with a comment saying that this variable can be false in case of invalid format would help.

@asprega asprega merged commit 075d605 into slope-it:master Aug 21, 2023
1 of 3 checks passed
@asprega
Copy link
Contributor

asprega commented Aug 21, 2023

Thanks! Released in 0.4.0

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.

None yet

2 participants