Skip to content

TypeError dead catch when assigning mixed to int in property #9146

Closed
phpstan/phpstan-src
#4981

Description

@ondrejmirtes

Discussed in #9145

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?

https://phpstan.org/r/f49b839d-dd70-43c7-9f69-2dab324a12e6

Activity

  1. added this to the Easy fixes milestone on Apr 4, 2023
  2. ondrejmirtes commented on Apr 4, 2023

    @ondrejmirtes
    MemberAuthor
  3. VincentLanglet commented on Apr 10, 2023

    @VincentLanglet
    Contributor

    It makes me think of #6232 @ondrejmirtes

    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.

  4. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

    @ondrejmirtes After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -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.
  5. phpstan-bot commented on Dec 28, 2024

    @phpstan-bot
    Contributor

    @ondrejmirtes After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -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.
  6. phpstan-bot commented on Nov 1, 2025

    @phpstan-bot
    Contributor

    @ondrejmirtes After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -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.
  7. ondrejmirtes commented on Nov 1, 2025

    @ondrejmirtes
    MemberAuthor

    There are no errors on PHP 8.4+ because of property hooks. The error would still show up for final classes/ final properties.

  8. staabm commented on Feb 17, 2026

    @staabm
    Contributor

    the errors should depend on strict-types.

    e.g. int accepts a numeric-string with strict-types=0, but does not accept it with strict-types=1
    https://3v4l.org/2TIt2#veol
    https://3v4l.org/kVik8#veol

    int accepts float independent from strict-types (and vice versa)

  9. phpstan-bot commented on Feb 18, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#4981

  10. github-actions commented on Mar 22, 2026

    @github-actions

    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.

  11. locked as resolved and limited conversation to collaborators on Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions