-
Notifications
You must be signed in to change notification settings - Fork 385
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
Declare strict_types=1 in every file #763
Conversation
@@ -373,7 +375,7 @@ protected function remove_dot_segments($input) | |||
// C: if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise, | |||
elseif (strpos($input, '/../') === 0) { | |||
$input = substr($input, 3); | |||
$output = substr_replace($output, '', strrpos($output, '/')); | |||
$output = substr_replace($output, '', intval(strrpos($output, '/'))); |
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.
It might be clearer to use something more explicit like ($pos = strrpos($output, '/')) === false ? 0 : $pos
or at least strrpos($output, '/') ?: 0
.
But since this is going away in the future it might be fine as is.
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.
My motivation to improve this soon to be deprecated class is not very high. 😅
@@ -817,7 +819,7 @@ public function set_authority($authority, $clear_cache = false) | |||
} else { | |||
$iuserinfo = null; | |||
} | |||
if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false) { | |||
if (($port_start = strpos($remaining, ':', intval(strpos($remaining, ']')))) !== false) { |
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.
Same here – with casts, developer needs to consider both possible initial return values and the mapping performed by the cast. Explicit conversion would make it more transparent.
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.
May I ask for an example?
Returning float instead of int was wrongly introduced in commt simplepie@33a8f76 According to the RSS 2.0 spec height and width must be the number of pixels. see https://www.rssboard.org/rss-specification#ltimagegtSubelementOfLtchannelgt
* bump version to 1.8.0 * Update CHANGELOG.md * Fix version tags in deprecated messages * fix version in old deprecation messages * Fix typo see comment from @jtojnar in #752 * Add comment for DataCache interface see comment from @jtojnar in #752 * Update CHANGELOG.md for #760, #764 and #765 * Update CHANGELOG.md for #762, #767 and #763 * Update CHANGELOG.md for #768 and #770 * Update release date * Update CHANGELOG.md for #769 and #771 * Update CHANGELOG.md for #766
Why should we revert the complete PR? A better option would be to remove |
Because it is impossible to tell which files are actually broken without a complete test coverage or static analysis. And since most of our functions currently only have argument types specified using a PHPDoc, incorrectly typed value can cause an error arbitrarily deep, when they are passed to native PHP functions. So far we have seen four or five different instances of this, IIRC, and I doubt those are all of the issues. |
I would like to see bug fixes releases more often where the issues are fixed instead of removing the declare statement.. But I'm afraid that the chances are not very high that this will happen. Maybe we could speed up the release for 1.9.0 instead? |
Same here.
Well, I see the following options:
|
By default, PHP will coerce values of the wrong type into the expected scalar type declaration if possible. It is possible to enable strict mode, so only a value corresponding exactly to the type declaration will be accepted, otherwise a
TypeError
will be thrown.This PR enables strict mode in all files. In most of the classes, everything works as expected but in a few classes I had to cast some values explicitly. I could even find and fix some bugs:
Misc
classSimplePie::get_image_height()
andget_image_width()
returningint|null
instead ofint|float|null
, introduced in 33a8f76I've also checked and fixed most of the errors for PHPStan Level 1.
I will keep this PR as draft until the other open PRs are merged so I can later add the strict types in the new classes and interfaces created by theses PRs.