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
Bitwise assignment doesn't change type #1340
Comments
I've fixed this for ints - not sure about bools |
Thanks. Are you willing to add support for bools later or at least a warning? |
Yeah, going to keep this open because it's a valid use case |
Just checking in; is the following example a separate issue to this one? https://psalm.dev/r/222baddf7b |
@SignpostMarv It's a separate issue but similar to #2139. |
This has been reworked since and now boolean operations with bitwise operators emits an error. Not sure we can still consider this a valid use case |
You're right. I've had to avoid this for other tools too, so this issue isn't a priority. It's surprising php hasn't started supporting bitwise operators for booleans since it would be very useful |
This issue still exists, yet the error messages have changed over time; |
I found these snippets: https://psalm.dev/r/aa2d948c03<?php
$enabled = false;
$classes = ['SomeClass', 'SomeOtherClass'];
foreach ($classes as $class) {
$enabled |= class_exists($class);
}
return $enabled;
|
I went and implemented this with the I figured for consistency we should support all the boolean operators: $bool = random_int(0, 1) ? true : false;
/** @psalm-check-type-exact $var = 1|2 */
$var = $bool + 1;
/** @psalm-check-type-exact $var = -1|-2 */
$var = $bool - 2;
/** @psalm-check-type-exact $var = 0|1 */
$var = $bool % 3;
/** @psalm-check-type-exact $var = 0|2 */
$var = $bool * 2;
/** @psalm-check-type-exact $var = 0|1 */
$var = $bool ** 3;
/** @psalm-check-type-exact $var = 4|5 */
$var = $bool | 4;
/** @psalm-check-type-exact $var = 2|3 */
$var = $bool ^ 3;
/** @psalm-check-type-exact $var = 0|1 */
$var = $bool & 3;
/** @psalm-check-type-exact $var = 0|4 */
$var = $bool << 2;
/** @psalm-check-type-exact $var = 0|1 */
$var = $bool >> 0; But if we do that, then this doesn't show any issues: /** @var string */
$str = "some string";
$pos = strpos($str, "z") + 1; And I think catching that is more important than allowing Should we go ahead and close this @orklah? |
I found these snippets: https://psalm.dev/r/daa21fb9b5<?php
/** @var string */
$str = "some string";
$pos = strpos($str, "z") + 1;
https://psalm.dev/r/bdb02a1b35<?php
$x = (int) false;
$x |= (int) true;
$x |= 1;
$x &= (int) (rand(0, 1) !== 1);
if ($x) {
echo $x;
}
|
I'm not sure why solving boolean operations make the strpos operation go without Issue, can you explain that? |
I pushed my attempt here so you can look at it. The problem is that the Especially after the casting improvements in #7696 I think the best solution for now is just for users to cast when doing this, it will result in correct types and no issues from Psalm. |
If
$x
isfalse
, using|= true
should make it1
.Example (https://getpsalm.org/r/11805c0b24):
Example output:
The text was updated successfully, but these errors were encountered: