Skip to content

Commit 8430e21

Browse files
Resolve bitwise not on constant integer
1 parent 8896129 commit 8430e21

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2634,8 +2634,11 @@ public function getBitwiseNotTypeFromType(Type $exprType): Type
26342634

26352635
return new IntersectionType([new StringType(), ...$accessories]);
26362636
}
2637+
if ($type instanceof ConstantIntegerType || $type instanceof ConstantFloatType) {
2638+
return new ConstantIntegerType(~ (int) $type->getValue());
2639+
}
26372640
if ($type->isInteger()->yes() || $type->isFloat()->yes()) {
2638-
return new IntegerType(); //no const types here, result depends on PHP_INT_SIZE
2641+
return new IntegerType();
26392642
}
26402643
return new ErrorType();
26412644
});

tests/PHPStan/Analyser/Fiber/data/fnsr.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public function doBitwiseNot($a, int $b): void
9696
{
9797
assertType('int', ~$a);
9898
assertNativeType('int', ~$b);
99-
assertType('int', ~1);
100-
assertNativeType('int', ~1);
99+
assertType('-2', ~1);
100+
assertNativeType('-2', ~1);
101101
assertType('int', ~$b);
102102
assertNativeType('int', ~$b);
103103
}

tests/PHPStan/Analyser/nsrt/bitwise-not.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ function foo(int $int, string $string, float $float, $stringOrInt, string $nonEm
1717
assertType('int', ~$float);
1818
assertType('int|string', ~$stringOrInt);
1919
assertType("'" . (~"abc") . "'", ~"abc");
20-
assertType('int', ~1); //result is dependent on PHP_INT_SIZE
20+
assertType('-2', ~1);
2121
}

0 commit comments

Comments
 (0)