-
Notifications
You must be signed in to change notification settings - Fork 544
Add exceptional case for DateInterval::format return type inference #4442
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
base: 2.1.x
Are you sure you want to change the base?
Add exceptional case for DateInterval::format return type inference #4442
Conversation
| // Could be lowercase-string&non-falsy-string&numeric-string&uppercase-string | ||
| assertType('lowercase-string&non-falsy-string', $dateInterval->format('%a')); | ||
| assertType( | ||
| "'(unknown)'&lowercase-string&non-empty-string&numeric-string&uppercase-string", |
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.
This is not ok.
I think you're expecting
'(unknown)'|lowercase-string&non-empty-string&numeric-string&uppercase-string
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.
Thanks for the review! I've pushed a change to fix this.
8740766 to
75008af
Compare
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.
Thinking about it again, I think the approach is not correct cause you handle %a to add new ConstantStringType('(unknown)') but it could occur with eveyr other format containing %a.
| ]); | ||
| $possibleReturnTypes[] = $diffInDaysType === null | ||
| ? $intersectionType | ||
| : new UnionType([$diffInDaysType, $intersectionType]); |
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.
You don't have to add $diffInDayType in every string you could do
$possibleReturnTypes = [];
if ($formatString === '%a') {
$possibleReturnTypes[] = new ConstantStringType('(unknown)');
}
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.
If the pattern is not %a without any additional character, it won't only return (unknown) and so it would only be a lowercase-string&non-falsy-string.
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.
Yes, but it could be considered weird/unnecessary to have the (unknown)|foo precision for %a and not for foo %a
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.
The "weirdness" of it I'm gonna let you decide :) I'd say the more precise the better, but I'm okay if we don't go with this.
|
@adamturcsan Wouldn't be enough to just change into Then the dumped type will be lowercase-string&non-empty-string which seems correct. |
[UPDATED with second if] I'd argue a if (is_numeric($value))and if (strtoupper($value) === $value)into if (is_numeric($value) && $format !== '%a')and if (strtoupper($value) === $value && $formatString !== '%a')it becomes correct and results in |
Since I feel like you should keep the then it should just be
you'll need |
Difference in days might behave differently when the DateInterval is created from scratch or from a diff. Filter returned type information for DateInterval::format. It can only be non-falsy for sure if it's not '%a'.
75008af to
4b35722
Compare
|
Alright, this is much-much simpler so I really can get behind of not providing "too precise" results and it also still solves my own problem, so I pushed the relevant changes. Thanks so much for your collaboration! |
Resolves phpstan/phpstan#13693
Difference in days might behave differently when the DateInterval is created from scratch or from a diff.
Extend returned type information for DateInterval::format method