You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeError dead catch when assigning mixed to int in property #9146
Originally posted by GMQS April 4, 2023
When assigning mixed data to a narrower type, a TypeError is said to be uncaught.
I am using PHPStan at level 8.
Is this behavior correct?
PHPStan should have consistency about exceptions about mixed.
(string) $mixed is considered as not throwing an exception, the opposite would be annoying since it would force to add
@throws Error
above the function when using PHPStan with exceptions checks.
It's recommended to add __toString or Stringable check rather than try catch the cast error
Here, it's the same with $this->int = $mixed, it seems recommended to check is_int rather than try/catch.
@@ @@-10: Dead catch - TypeError is never thrown in the try block.+PHP 8.0 – 8.2 (1 error)+==========++10: Dead catch - TypeError is never thrown in the try block.++PHP 7.2 – 7.4 (2 errors)+==========++ 6: Parameter $number of method HelloWorld::__construct() has invalid type mixed.+ 9: Property HelloWorld::$number (int) does not accept mixed.
Full report
PHP 8.0 – 8.2 (1 error)
Line
Error
10
Dead catch - TypeError is never thrown in the try block.
PHP 7.2 – 7.4 (2 errors)
Line
Error
6
Parameter $number of method HelloWorld::__construct() has invalid type mixed.
9
Property HelloWorld::$number (int) does not accept mixed.
@@ @@-10: Dead catch - TypeError is never thrown in the try block.+PHP 8.4+==========++No errors++PHP 8.0 – 8.3 (1 error)+==========++10: Dead catch - TypeError is never thrown in the try block.++PHP 7.2 – 7.4 (2 errors)+==========++ 6: Parameter $number of method HelloWorld::__construct() has invalid type mixed.+ 9: Property HelloWorld::$number (int) does not accept mixed.
Full report
PHP 8.4
No errors
PHP 8.0 – 8.3 (1 error)
Line
Error
10
Dead catch - TypeError is never thrown in the try block.
PHP 7.2 – 7.4 (2 errors)
Line
Error
6
Parameter $number of method HelloWorld::__construct() has invalid type mixed.
9
Property HelloWorld::$number (int) does not accept mixed.
@@ @@-10: Dead catch - TypeError is never thrown in the try block.+PHP 8.4 – 8.5+==========++No errors++PHP 8.0 – 8.3 (1 error)+==========++10: Dead catch - TypeError is never thrown in the try block.++PHP 7.2 – 7.4 (2 errors)+==========++ 6: Parameter $number of method HelloWorld::__construct() has invalid type mixed.+ 9: Property HelloWorld::$number (int) does not accept mixed.
Full report
PHP 8.4 – 8.5
No errors
PHP 8.0 – 8.3 (1 error)
Line
Error
10
Dead catch - TypeError is never thrown in the try block.
PHP 7.2 – 7.4 (2 errors)
Line
Error
6
Parameter $number of method HelloWorld::__construct() has invalid type mixed.
9
Property HelloWorld::$number (int) does not accept mixed.
Activity
ondrejmirtes commentedon Apr 4, 2023
Related: phpstan/phpstan-src#1047
VincentLanglet commentedon Apr 10, 2023
It makes me think of #6232 @ondrejmirtes
PHPStan should have consistency about exceptions about mixed.
(string) $mixedis considered as not throwing an exception, the opposite would be annoying since it would force to addabove the function when using PHPStan with exceptions checks.
It's recommended to add
__toStringorStringablecheck rather than try catch the cast errorHere, it's the same with
$this->int = $mixed, it seems recommended to checkis_intrather than try/catch.phpstan-bot commentedon Sep 4, 2024
@ondrejmirtes After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.0 – 8.2 (1 error)
Dead catch - TypeError is never thrown in the try block.PHP 7.2 – 7.4 (2 errors)
Parameter $number of method HelloWorld::__construct() has invalid type mixed.Property HelloWorld::$number (int) does not accept mixed.phpstan-bot commentedon Dec 28, 2024
@ondrejmirtes After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.4
No errors
PHP 8.0 – 8.3 (1 error)
Dead catch - TypeError is never thrown in the try block.PHP 7.2 – 7.4 (2 errors)
Parameter $number of method HelloWorld::__construct() has invalid type mixed.Property HelloWorld::$number (int) does not accept mixed.phpstan-bot commentedon Nov 1, 2025
@ondrejmirtes After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.4 – 8.5
No errors
PHP 8.0 – 8.3 (1 error)
Dead catch - TypeError is never thrown in the try block.PHP 7.2 – 7.4 (2 errors)
Parameter $number of method HelloWorld::__construct() has invalid type mixed.Property HelloWorld::$number (int) does not accept mixed.ondrejmirtes commentedon Nov 1, 2025
There are no errors on PHP 8.4+ because of property hooks. The error would still show up for final classes/ final properties.
Fix TypeError dead catch false positive when assigning mixed to typed…
staabm commentedon Feb 17, 2026
the errors should depend on strict-types.
e.g.
intaccepts anumeric-stringwith strict-types=0, but does not accept it with strict-types=1https://3v4l.org/2TIt2#veol
https://3v4l.org/kVik8#veol
intacceptsfloatindependent fromstrict-types(and vice versa)phpstan-bot commentedon Feb 18, 2026
Closed via merging phpstan/phpstan-src#4981
github-actions commentedon Mar 22, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.