Skip to content

Wrong inferred type after variable assignment in ternary if statements #13334

Closed
phpstan/phpstan-src
#5857

Description

@vHeemstra

Bug report

Situation

I have a validation method that checks if the value provided is a string with a zero-padded integer larger than 0 (like '00123').

I use ternary ifs to narrow down the type. If 'strict mode' is used, the value must be a string, otherwise I use a helper function to ensure it's a string. Then finally, it checks the format using a preg_match call.

Issue 1 - Wrong inferred type

PHPStan gives an error on the $subject parameter of preg_match, saying its type is not string.

Inline example:
Parameter #2 $subject of function preg_match expects string, bool|int|string|null given.

Using functions example:
Parameter #2 $subject of function preg_match expects string, mixed given.

However, this is wrong.

Because of the && operators, the call to preg_match only happens if the value is a string at that point. So it should not be an issue, right?

Issue 2 - Inconsistent inferred type

Funny enough the inferred type of the value is different when the code is not using functions.

See linked example for more details.

Code snippet that reproduces the problem

https://phpstan.org/r/a9893366-4c38-46c8-a87c-1e0a71c441f0

Expected output

No errors from PHPStan; the type of $value at the point of using it as parameter in preg_match should be string (or a compatible subset).

Did PHPStan help you today? Did it make you happy in any way?

It made me triple check all possible outcomes of basic PHP methods I've been using for years now. A bit maddening. 😄

But lots of love to all PHPStan contributors!

Activity

  1. added this to the Easy fixes milestone on Jul 31, 2025
  2. phpstan-bot commented on Jul 31, 2025

    @phpstan-bot
    Collaborator

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

    @@ @@
    -PHP 8.0 – 8.5 (2 errors)
    +PHP 8.0 – 8.5 (3 errors)
     ==========
     
    +13: No error to ignore is reported on line 13.
     31: Parameter #2 $subject of function preg_match expects string, bool|int|string|null given.
     52: Parameter #2 $subject of function preg_match expects string, mixed given.
     
    -PHP 7.2 – 7.4 (8 errors)
    +PHP 7.2 – 7.4 (9 errors)
     ==========
     
    +13: No error to ignore is reported on line 13.
     31: Parameter #2 $subject of function preg_match expects string, bool|int|string|null given.
     37: Parameter $value of function ensureString() has invalid type mixed.
     40: Call to function is_numeric() with mixed will always evaluate to false.
    Full report

    PHP 8.0 – 8.5 (3 errors)

    Line Error
    13 No error to ignore is reported on line 13.
    31 `Parameter #2 $subject of function preg_match expects string, bool
    52 Parameter #2 $subject of function preg_match expects string, mixed given.

    PHP 7.2 – 7.4 (9 errors)

    Line Error
    13 No error to ignore is reported on line 13.
    31 `Parameter #2 $subject of function preg_match expects string, bool
    37 Parameter $value of function ensureString() has invalid type mixed.
    40 Call to function is_numeric() with mixed will always evaluate to false.
    40 Casting to *NEVER* something that's already *NEVER*.
    40 Variable $value in empty() always exists and is not falsy.
    43 Parameter $value of function isNonZeroPaddedString() has invalid type mixed.
    46 Call to function is_string() with mixed will always evaluate to false.
    52 `Parameter #2 $subject of function preg_match expects string, mixed
  3. github-actions commented on Oct 4, 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.

  4. locked as resolved and limited conversation to collaborators on Oct 4, 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