From 342769902d8b1ab9162324e85cfa5913ea596e6c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 16 May 2025 15:34:48 +0200 Subject: [PATCH 1/4] Revert "non-falsy-string cannot be converted to 0" This reverts commit f615b1a39a70d9ec9e49963474c7f9f9577cf88b. --- src/Type/Accessory/AccessoryNonFalsyStringType.php | 3 +-- tests/PHPStan/Analyser/nsrt/bug-10893.php | 12 ++++++------ tests/PHPStan/Analyser/nsrt/non-falsy-string.php | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Type/Accessory/AccessoryNonFalsyStringType.php b/src/Type/Accessory/AccessoryNonFalsyStringType.php index 6600512da1..afa77a3476 100644 --- a/src/Type/Accessory/AccessoryNonFalsyStringType.php +++ b/src/Type/Accessory/AccessoryNonFalsyStringType.php @@ -31,7 +31,6 @@ use PHPStan\Type\Traits\TruthyBooleanTypeTrait; use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; use PHPStan\Type\Type; -use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; @@ -184,7 +183,7 @@ public function toAbsoluteNumber(): Type public function toInteger(): Type { - return TypeCombinator::remove(new IntegerType(), new ConstantIntegerType(0)); + return new IntegerType(); } public function toFloat(): Type diff --git a/tests/PHPStan/Analyser/nsrt/bug-10893.php b/tests/PHPStan/Analyser/nsrt/bug-10893.php index a2b8396dd5..0878d2f302 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-10893.php +++ b/tests/PHPStan/Analyser/nsrt/bug-10893.php @@ -10,16 +10,16 @@ function hasMicroseconds(\DateTimeInterface $value, string $str): bool { assertType('non-falsy-string&numeric-string', $str); - assertType('int|int<1, max>', (int)$str); - assertType('true', (int)$str !== 0); + assertType('int', (int)$str); + assertType('bool', (int)$str !== 0); assertType('non-falsy-string&numeric-string', $value->format('u')); - assertType('int|int<1, max>', (int)$value->format('u')); - assertType('true', (int)$value->format('u') !== 0); + assertType('int', (int)$value->format('u')); + assertType('bool', (int)$value->format('u') !== 0); assertType('non-falsy-string&numeric-string', $value->format('v')); - assertType('int|int<1, max>', (int)$value->format('v')); - assertType('true', (int)$value->format('v') !== 0); + assertType('int', (int)$value->format('v')); + assertType('bool', (int)$value->format('v') !== 0); assertType('float', $value->format('u') * 1e-6); assertType('float', $value->format('v') * 1e-3); diff --git a/tests/PHPStan/Analyser/nsrt/non-falsy-string.php b/tests/PHPStan/Analyser/nsrt/non-falsy-string.php index 5e7e05f229..cef87c8ac4 100644 --- a/tests/PHPStan/Analyser/nsrt/non-falsy-string.php +++ b/tests/PHPStan/Analyser/nsrt/non-falsy-string.php @@ -11,7 +11,7 @@ class Foo { * @param truthy-string $truthyString */ public function bar($nonFalseyString, $truthyString) { - assertType('int|int<1, max>', (int) $nonFalseyString); + assertType('int', (int) $nonFalseyString); // truthy-string is an alias for non-falsy-string assertType('non-falsy-string', $truthyString); } From d9dc7b1c51bc4582f22023d13b1d699106fd1520 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 16 May 2025 15:38:32 +0200 Subject: [PATCH 2/4] Fix --- src/Type/Accessory/AccessoryNonFalsyStringType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Type/Accessory/AccessoryNonFalsyStringType.php b/src/Type/Accessory/AccessoryNonFalsyStringType.php index afa77a3476..9929d5595b 100644 --- a/src/Type/Accessory/AccessoryNonFalsyStringType.php +++ b/src/Type/Accessory/AccessoryNonFalsyStringType.php @@ -31,6 +31,7 @@ use PHPStan\Type\Traits\TruthyBooleanTypeTrait; use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; use PHPStan\Type\Type; +use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; From 080836b017399e575316dfa4ac71bbb454c5c4a2 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 16 May 2025 15:39:26 +0200 Subject: [PATCH 3/4] Add comment --- src/Type/Accessory/AccessoryNonFalsyStringType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Type/Accessory/AccessoryNonFalsyStringType.php b/src/Type/Accessory/AccessoryNonFalsyStringType.php index 9929d5595b..53e854283d 100644 --- a/src/Type/Accessory/AccessoryNonFalsyStringType.php +++ b/src/Type/Accessory/AccessoryNonFalsyStringType.php @@ -184,6 +184,7 @@ public function toAbsoluteNumber(): Type public function toInteger(): Type { + // Do not remove `0` since `(int) '00'` is still `0`. return new IntegerType(); } From 8246e6d5ccbb561a4c3736732ebae0705bd59a6c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 16 May 2025 16:10:27 +0200 Subject: [PATCH 4/4] Fix --- tests/PHPStan/Analyser/nsrt/bug-12393b.php | 3 +-- tests/PHPStan/Analyser/nsrt/non-falsy-string.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Analyser/nsrt/bug-12393b.php b/tests/PHPStan/Analyser/nsrt/bug-12393b.php index 7ec8f3012b..a21dcf561a 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-12393b.php +++ b/tests/PHPStan/Analyser/nsrt/bug-12393b.php @@ -177,7 +177,7 @@ function doStrings($nonEmpty, $nonFalsy, $numeric, $literal, $lower, $upper) { $this->foo = $nonEmpty; assertType('int', $this->foo); $this->foo = $nonFalsy; - assertType('int|int<1, max>', $this->foo); + assertType('int', $this->foo); $this->foo = $numeric; assertType('int', $this->foo); $this->foo = $literal; @@ -706,4 +706,3 @@ public function __toString(): string { return 'Foo'; } } - diff --git a/tests/PHPStan/Analyser/nsrt/non-falsy-string.php b/tests/PHPStan/Analyser/nsrt/non-falsy-string.php index cef87c8ac4..12ca4c3fac 100644 --- a/tests/PHPStan/Analyser/nsrt/non-falsy-string.php +++ b/tests/PHPStan/Analyser/nsrt/non-falsy-string.php @@ -11,7 +11,7 @@ class Foo { * @param truthy-string $truthyString */ public function bar($nonFalseyString, $truthyString) { - assertType('int', (int) $nonFalseyString); + assertType('int', (int) $nonFalseyString); // Do not remove `0` since `(int) '00'` is still `0` // truthy-string is an alias for non-falsy-string assertType('non-falsy-string', $truthyString); }