Skip to content

Conversation

VincentLanglet
Copy link
Contributor

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Also: what about add method?

return null;
}

return new ObjectType('DateMalformedStringException');
Copy link
Member

Choose a reason for hiding this comment

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

We need to check the constant strings in the argument type and only throw this exception if some of them are invalid. Or if there are zero constant strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DateTime::sub does not accepts constant strings, the signature is

public DateTime::sub(DateInterval $interval): DateTime

It only throw an exception if the DateInterval is relative like

$dt->sub(new DateInterval('next weekday'))

but I didnt see a way to have something like getDateIntervalConstantStrings(). I wasn't sure

So as a first step, it improve the situation for all the PHP version lower than 8.3.

Copy link
Contributor Author

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

Also: what about add method?

public DateTime::add(DateInterval $interval): DateTime

does not throw exceptions.

return null;
}

return new ObjectType('DateMalformedStringException');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

DateTime::sub does not accepts constant strings, the signature is

public DateTime::sub(DateInterval $interval): DateTime

It only throw an exception if the DateInterval is relative like

$dt->sub(new DateInterval('next weekday'))

but I didnt see a way to have something like getDateIntervalConstantStrings(). I wasn't sure

So as a first step, it improve the situation for all the PHP version lower than 8.3.

@ondrejmirtes
Copy link
Member

I apologize, I didn't check the method signature before.

But I don't see the same behaviour you do. Can you show me when the sub method throws an exception and not the DateInterval constructor? https://3v4l.org/pfAXh

@VincentLanglet
Copy link
Contributor Author

I made a copy-paste error, it should have been a DateInvalidOperationException in the DateTimeSubMethodThrowTypeExtension

But I don't see the same behaviour you do. Can you show me when the sub method throws an exception and not the DateInterval constructor? 3v4l.org/pfAXh

See https://3v4l.org/T91Mv

It seems like the DateInterval constructor does not accept relative interval.

So

$datetime->sub(new \DateInterval(...))

won't throw an exception while

$datetime->sub(DateInterval::createFromDateFormat(...))

could.

@ondrejmirtes ondrejmirtes merged commit 9c4bee9 into phpstan:1.12.x Sep 3, 2024
488 of 499 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

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.

Call to method DateTimeImmutable::sub() on a separate line doesn't produce an error

2 participants