From 05b6a265ad9ca799efaec8fd59a7dc2f841a50cf Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 13 Oct 2025 21:04:05 +0200 Subject: [PATCH 1/2] Resolve bitwise not on constant integer --- src/Reflection/InitializerExprTypeResolver.php | 5 ++++- tests/PHPStan/Analyser/nsrt/bitwise-not.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index 8b38c5a383..1464d38fe5 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(~$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); } From 4477c49d4b32d5f3590b285567b6214848b2bc7d Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 13 Oct 2025 21:34:45 +0200 Subject: [PATCH 2/2] Fix deprecation --- src/Reflection/InitializerExprTypeResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index 1464d38fe5..65c4c09bc7 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -2160,7 +2160,7 @@ public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type return TypeCombinator::intersect(...$accessories); } if ($type instanceof ConstantIntegerType || $type instanceof ConstantFloatType) { - return new ConstantIntegerType(~$type->getValue()); + return new ConstantIntegerType(~ (int) $type->getValue()); } if ($type->isInteger()->yes() || $type->isFloat()->yes()) { return new IntegerType();