Skip to content

known offsets after preg_split #14464

Open
phpstan/phpstan-src
#6383

Description

@staabm

Feature request

I think we should be able to infer more precise array-shapes after if-casing the count() of a list-typed variable

I expect no "Offset X might not exist" errors in https://phpstan.org/r/579e635a-3dc9-4a12-82cd-a4439a418213

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

No response

Activity

  1. VincentLanglet commented on Apr 14, 2026

    @VincentLanglet
    Contributor

    Works fine when count is used inside the if.
    https://phpstan.org/r/6cfd8a6f-6775-4ec8-bf56-9355fe6fc980

    The issue is just the fact it's inside a variable

  2. staabm commented on Apr 14, 2026

    @staabm
    ContributorAuthor

    yes, thats why its a dependent-types issue

  3. added a commit that references this issue on Apr 14, 2026
    a9a6ebc
  4. phpstan-bot commented on Apr 15, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -PHP 8.0 – 8.5 (5 errors)
    -==========
    -
    -14: Dumped type: list<non-empty-string>
    -18: Dumped type: non-empty-list<non-empty-string>
    -26: Dumped type: non-empty-list<non-empty-string>
    -31: Dumped type: non-empty-list<non-empty-string>
    -39: Dumped type: non-empty-list<non-empty-string>
    -
    -PHP 7.2 – 7.4 (7 errors)
    -==========
    -
    -12: Function preg_last_error_msg not found.
    -12: Parameter #1 $message of class RuntimeException constructor expects string, mixed given.
     14: Dumped type: list<non-empty-string>
     18: Dumped type: non-empty-list<non-empty-string>
     26: Dumped type: non-empty-list<non-empty-string>
     31: Dumped type: non-empty-list<non-empty-string>
     39: Dumped type: non-empty-list<non-empty-string>
    Full report
    Line Error
    14 Dumped type: list<non-empty-string>
    18 Dumped type: non-empty-list<non-empty-string>
    26 Dumped type: non-empty-list<non-empty-string>
    31 Dumped type: non-empty-list<non-empty-string>
    39 Dumped type: non-empty-list<non-empty-string>
  5. phpstan-bot commented on Apr 15, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -PHP 8.0 – 8.5 (5 errors)
    -==========
    -
    -14: Dumped type: list<non-empty-string>
    -18: Dumped type: array{non-empty-string, non-empty-string, non-empty-string}
    -26: Dumped type: array{non-empty-string, non-empty-string}
    -31: Dumped type: array{non-empty-string}
    -39: Dumped type: list{0: non-empty-string, 1?: non-empty-string, 2?: non-empty-string}
    -
    -PHP 7.2 – 7.4 (7 errors)
    -==========
    -
    -12: Function preg_last_error_msg not found.
    -12: Parameter #1 $message of class RuntimeException constructor expects string, mixed given.
     14: Dumped type: list<non-empty-string>
     18: Dumped type: array{non-empty-string, non-empty-string, non-empty-string}
     26: Dumped type: array{non-empty-string, non-empty-string}
     31: Dumped type: array{non-empty-string}
     39: Dumped type: list{0: non-empty-string, 1?: non-empty-string, 2?: non-empty-string}
    Full report
    Line Error
    14 Dumped type: list<non-empty-string>
    18 Dumped type: array{non-empty-string, non-empty-string, non-empty-string}
    26 Dumped type: array{non-empty-string, non-empty-string}
    31 Dumped type: array{non-empty-string}
    39 Dumped type: list{0: non-empty-string, 1?: non-empty-string, 2?: non-empty-string}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions