Skip to content

Add some ValueErrors to ext/date #5613

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

Closed
wants to merge 11 commits into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented May 22, 2020

A new spin on #4976 which is more conservative.

This allows to drop a couple of false returns.

@derickr is this change OK with you?

@Girgias Girgias force-pushed the date-argument-errors branch from 5b5fc69 to 71d0b18 Compare May 24, 2020 12:09
var_dump(idate(0));
try {
var_dump(idate(1,1));
} catch (\ValueError $e) {
Copy link
Member

Choose a reason for hiding this comment

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

I am not keen on this change, as this is a BC break. Previously wrong values just returned false, which is something that strtotime and the older date functions have done for decades. Upgrading them to throw an exception is not a great thing to do here. It's going to cause BC breaks, even if people already carefully checked the return values. As most input to date functions will come from either user input or a database, I don't believe this is a worthy place to introduce breaks.

Instead, we should point people to the DateTime classes.

var_dump(strftime(""));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
Copy link
Member

Choose a reason for hiding this comment

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

I am not keen on this change, as this is a BC break. Previously wrong values just returned false, which is something that strtotime and the older date functions have done for decades. Upgrading them to throw an exception is not a great thing to do here. It's going to cause BC breaks, even if people already carefully checked the return values. As most input to date functions will come from either user input or a database, I don't believe this is a worthy place to introduce breaks.

Instead, we should point people to the DateTime classes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you trying to promote the use of the DateTim(Immutable) class for this specific case, or more generally?

From what I've understood the DateTime classes also return false and don't throw, or would you be more inclined for the objects to throw and be able to drop the false return?

@@ -4408,6 +4425,7 @@ PHP_FUNCTION(timezone_identifiers_list)

/* Extra validation */
if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
// Promoto to ValueError?
Copy link
Member

Choose a reason for hiding this comment

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

In any case, either do it, or don't add a comment.

if (error) {
/* TODO Add warning? */
Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking a ValueException or something is OK here, as the function/method specifically asks the code to return an integer since the epoch.

@Girgias Girgias force-pushed the date-argument-errors branch 2 times, most recently from ee82bda to c1b5a49 Compare September 13, 2020 18:57
Copy link
Member

@derickr derickr left a comment

Choose a reason for hiding this comment

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

I made some minor new comments, but there are a few questions open from previous reviews wrt to idate.

@Girgias Girgias force-pushed the date-argument-errors branch 7 times, most recently from a9a6628 to 9a54cd7 Compare September 15, 2020 17:52
@Girgias Girgias force-pushed the date-argument-errors branch from 9a54cd7 to 52593f5 Compare September 15, 2020 19:10
Copy link
Member

@derickr derickr left a comment

Choose a reason for hiding this comment

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

Except for my nit, and ignore the return type hint comments :-)

@@ -1224,7 +1239,6 @@ PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
ta.tm_zone = offset->abbr;
#endif
}

Copy link
Member

Choose a reason for hiding this comment

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

nit: don't remove this empty line

@@ -230,7 +230,7 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1) {}
public function setTimestamp(int $timestamp) {}

/**
* @return int|false
* @return int
Copy link
Member

Choose a reason for hiding this comment

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

Now it returns just an "int", can it not be set as a return type on the function (line 236) itself?

@@ -282,7 +282,7 @@ public function getTimezone() {}
public function getOffset() {}

/**
* @return int|false
* @return int
Copy link
Member

Choose a reason for hiding this comment

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

ditto

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

Successfully merging this pull request may close these issues.

4 participants