Declare strict_types=1 in every file#763
Conversation
| 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.
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.
My motivation to improve this soon to be deprecated class is not very high. 😅
| $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.
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.
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
TypeErrorwill 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:
MiscclassSimplePie::get_image_height()andget_image_width()returningint|nullinstead 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.