diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index 8b38c5a383..65c4c09bc7 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -2159,8 +2159,11 @@ public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type return TypeCombinator::intersect(...$accessories); } + if ($type instanceof ConstantIntegerType || $type instanceof ConstantFloatType) { + return new ConstantIntegerType(~ (int) $type->getValue()); + } if ($type->isInteger()->yes() || $type->isFloat()->yes()) { - return new IntegerType(); //no const types here, result depends on PHP_INT_SIZE + return new IntegerType(); } return new ErrorType(); }); diff --git a/tests/PHPStan/Analyser/nsrt/bitwise-not.php b/tests/PHPStan/Analyser/nsrt/bitwise-not.php index 37c29f8f97..01623d2d5b 100644 --- a/tests/PHPStan/Analyser/nsrt/bitwise-not.php +++ b/tests/PHPStan/Analyser/nsrt/bitwise-not.php @@ -17,5 +17,5 @@ function foo(int $int, string $string, float $float, $stringOrInt, string $nonEm assertType('int', ~$float); assertType('int|string', ~$stringOrInt); assertType("'" . (~"abc") . "'", ~"abc"); - assertType('int', ~1); //result is dependent on PHP_INT_SIZE + assertType('-2', ~1); }