File tree Expand file tree Collapse file tree 3 files changed +36
-3
lines changed
tests/PHPStan/Rules/Exceptions Expand file tree Collapse file tree 3 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 174174use PHPStan \Type \Constant \ConstantIntegerType ;
175175use PHPStan \Type \Constant \ConstantStringType ;
176176use PHPStan \Type \FileTypeMapper ;
177- use PHPStan \Type \FloatType ;
178177use PHPStan \Type \GeneralizePrecision ;
179178use PHPStan \Type \Generic \TemplateTypeHelper ;
180179use PHPStan \Type \Generic \TemplateTypeMap ;
@@ -6369,7 +6368,7 @@ private function processAssignVar(
63696368 $ nativeProperty = $ declaringClass ->getNativeProperty ($ propertyName );
63706369 $ propertyNativeType = $ nativeProperty ->getNativeType ();
63716370 // Widen property type to accept int for float properties (PHP allows int-to-float coercion without TypeError)
6372- $ propertyNativeTypeForAccepts = $ propertyNativeType ->isSuperTypeOf ( new FloatType ())-> yes ()
6371+ $ propertyNativeTypeForAccepts = ! $ propertyNativeType ->isFloat ()-> no ()
63736372 ? TypeCombinator::union ($ propertyNativeType , new IntegerType ())
63746373 : $ propertyNativeType ;
63756374 if (
Original file line number Diff line number Diff line change @@ -645,7 +645,12 @@ public function testPropertyHooks(): void
645645
646646 public function testBug9146 (): void
647647 {
648- $ this ->analyse ([__DIR__ . '/data/bug-9146.php ' ], []);
648+ $ this ->analyse ([__DIR__ . '/data/bug-9146.php ' ], [
649+ [
650+ 'Dead catch - TypeError is never thrown in the try block. ' ,
651+ 52 ,
652+ ],
653+ ]);
649654 }
650655
651656}
Original file line number Diff line number Diff line change @@ -40,3 +40,32 @@ public function setAmount(mixed $value): void
4040 }
4141 }
4242}
43+
44+ // Dead catch: int assigned to ?float, PHP coerces int to float without TypeError
45+ final class FloatNullCoercion
46+ {
47+ public ?float $ amount ;
48+ public function setAmount (int $ value ): void
49+ {
50+ try {
51+ $ this ->amount = $ value ;
52+ } catch (\TypeError $ e ) { // error: Dead catch - TypeError is never thrown in the try block.
53+ echo "caught " ;
54+ }
55+ }
56+ }
57+
58+ // Not dead: int|string assigned to int, string part could throw TypeError
59+ final class PartialTypeMatch
60+ {
61+ public int $ number ;
62+ /** @param int|string $value */
63+ public function setNumber ($ value ): void
64+ {
65+ try {
66+ $ this ->number = $ value ;
67+ } catch (\TypeError $ e ) {
68+ echo "caught " ;
69+ }
70+ }
71+ }
You can’t perform that action at this time.
0 commit comments