- Notifications
You must be signed in to change notification settings - Fork 545
Catch implicit conversions in mod operator #2466
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
Open
ryium wants to merge 7 commits into phpstan:1.11.x Choose a base branch from ryium:ryium/issue/8288-op-mod
base: 1.11.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits Select commit Hold shift + click to select a range
3a891ff Add test cases for table
ryium 510d4d2 Run make cs-fix
ryium b194dfd Remove PHP_VERSION_ID
ryium 03a9fd0 Messages in PHP 8.1 and above with PhpVersion
ryium dd46d78 Change expected errors with PHP_VERSION_ID
ryium 27f6f33 Sort errors by line number
ryium 07916c7 Run make cs-fix
ryium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
| | ||
| namespace Bug8288; | ||
| | ||
| use Stringable; | ||
| | ||
| class Bug8288 { | ||
| | ||
| /** | ||
| * @param numeric-string $numericString | ||
| */ | ||
| public function opMod(int $int, float $float, string $string, string $numericString, Stringable $stringable, array $array) | ||
| { | ||
| # Safe | ||
| $int % $int; | ||
| # Deprecated in PHP 8.1 | ||
| $int % $float; | ||
| $int % $numericString; | ||
| $float % $int; | ||
| $float % $float; | ||
| $float % $numericString; | ||
| $numericString % $int; | ||
| $numericString % $float; | ||
| $numericString % $numericString; | ||
| # Not safe | ||
| $int % $string; | ||
| $int % $stringable; | ||
| $int % $array; | ||
| $float % $string; | ||
| $float % $stringable; | ||
| $float % $array; | ||
| $string % $int; | ||
| $string % $float; | ||
| $string % $string; | ||
| $string % $numericString; | ||
| $string % $stringable; | ||
| $string % $array; | ||
| $numericString % $string; | ||
| $numericString % $stringable; | ||
| $numericString % $array; | ||
| $stringable % $int; | ||
| $stringable % $float; | ||
| $stringable % $string; | ||
| $stringable % $numericString; | ||
| $stringable % $stringable; | ||
| $stringable % $array; | ||
| $array % $int; | ||
| $array % $float; | ||
| $array % $string; | ||
| $array % $numericString; | ||
| $array % $stringable; | ||
| $array % $array; | ||
| | ||
| // The following three examples should all be flagged as resulting in implicit float to int conversion with potential loss of precision. This behaviour is deprecated in PHP 8.1, and results in a deprecation warning (if deprecation warnings are enabled, which they are by default PHP 8+), if encountered at runtime. It would be helpful if phpstan could catch and flag such cases during static analysis. | ||
| $singleByteCode = ord('a'); | ||
| $float1 = ( $singleByteCode / 16 ); | ||
| // This is not safe | ||
| $float1 % 15; | ||
| ( 97 / 16 ) % 15; | ||
| 6.0625 % 15; | ||
| | ||
| // The following three examples should not result in any error | ||
| $singleByteCode = ord('a'); | ||
| $int1 = intdiv( $singleByteCode, 16 ); | ||
| // This is safe | ||
| $int1 % 15; | ||
| intdiv( 97, 16 ) % 15; | ||
| 6 % 15; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
How the type system responds shouldn't change because it's not actually true. The
%operator still returns a number but only on some PHP versions it throws a deprecation notice.