Skip to content

PHPStan falsely detects explode() as returning empty arrays in cases where it doesn't #3961

@colinodell

Description

@colinodell

Bug report

The latest version of PHPStan incorrectly believes that the explode() function returns an empty array in cases where it doesn't.

Per the PHP manual:

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

Code snippet that reproduces the problem

See https://phpstan.org/r/a664b522-b2d8-4541-a071-f1e768a0f9ad

This snippet will always return a non-empty array because (per the PHP documentation):

  • If delimiter is an empty string (""), explode() will return FALSE. The string is never going to be empty
  • If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned we're not providing a negative limit - we're using the default of PHP_INT_MAX
  • otherwise an array containing string will be returned - this is what the sample snippet always does

Expected output

PHPStan should not report any false errors

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions