Skip to content

typehint for range() does not suggest string[] #1201

Closed

Description

@sanmai

For this code:

$allLines = range($input['startLine'], $input['endLine']);
foreach ($allLines as $lineIn) {
    if (array_key_exists($lineIn, $this->example)) {
    // ...

I'm getting: https://phpstan.org/r/e33af1b753c49bfa7c60020f3672f0eb

Parameter $key of function array_key_exists expects int|string, float|int given.

But there's a slight problem: range() may return an array of strings just as well.

var_dump(range('c', 'a'));
// outputs array('c', 'b', 'a')

So the proper error should read:

Parameter $key of function array_key_exists expects int|string, float|int|string given.

Here's another example: https://phpstan.org/r/4c3ac5e0da8e88136270c7c3ab2672ab

Activity

  1. changed the title [-]typehint for range() does not suggest string[/-] [+]typehint for range() does not suggest string[][/+] on Jul 11, 2018
  2. ondrejmirtes commented on Jul 11, 2018

    @ondrejmirtes
    Member
  3. sanmai commented on Jul 11, 2018

    @sanmai
    ContributorAuthor

    I've added a link to the reproduction.

    range() returns array of either floats, ints, or strings, but PHPStan thinks it can only be of numbers.

  4. adaamz commented on Jul 11, 2018

    @adaamz
    Contributor

    @ondrejmirtes Dynamic return type extension really returns type array<int, int|flost>, see

    return new ArrayType(new IntegerType(), new UnionType([new IntegerType(), new FloatType()]));

  5. added this to the 0.10 milestone on Jul 11, 2018
  6. added 2 commits that reference this issue on Jul 12, 2018
    b147885
    8ff1322
  7. ondrejmirtes commented on Feb 18, 2021

    @ondrejmirtes
    Member

    This is already fixed.

  8. github-actions commented on Apr 29, 2021

    @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.

  9. locked as resolved and limited conversation to collaborators on Apr 29, 2021
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