Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array_pop output should not be null given a full array #1722

Closed
muglug opened this issue Dec 18, 2018 · 6 comments
Closed

array_pop output should not be null given a full array #1722

muglug opened this issue Dec 18, 2018 · 6 comments

Comments

@muglug
Copy link
Contributor

muglug commented Dec 18, 2018

Found this while deciding whether to add similar functionality in another package.

/**
 * @param array<int, int> $arr
 */
function foo(array $arr) : int {
    $a = array_pop($arr);
	
    if ($arr) {
        return $a;
    }
	
    return 0;
}

Expected: no issue
Actual: Function foo() should return int but returns int|null
https://phpstan.org/r/693d212d-fec7-45fd-bce5-7bcf7c01afff

@ondrejmirtes
Copy link
Member

This falls under dependent types, here are other examples: https://github.com/phpstan/phpstan/milestone/12

I'm not sure if it's worth tackling since the code is really analyser-unfriendly. I have no idea right now how I would model it and approach this problem.

@muglug
Copy link
Contributor Author

muglug commented Dec 18, 2018

I have no idea right now how I would model it and approach this problem.

I don't either.

Here's where I use this pattern.

Easy obvious workaround is to assert if ($a && $arr), but that doesn't feel great.

@hrach
Copy link
Contributor

hrach commented Dec 18, 2018

I don't see any reason to support this. Statically & strictly typed language don't to this either.
https://pl.kotl.in/B1BOqoIeV

@muglug
Copy link
Contributor Author

muglug commented Dec 18, 2018

@hrach

Statically & strictly typed language don't to this either.

That in itself shouldn't be an argument, I don't think. For example, those tools generally do not allow this code:

function takesObject(object $_o): void {}
function foo(?object $a, ?object $b): void {
  if ($a || $b) {
    if ($a) {
      $c = $a;
    } else {
      $c = $b;
    }

    takesObject($c);
  }
}

But with the right analysis a tool can understand what's happening: https://getpsalm.org/r/680d27b861

@ondrejmirtes
Copy link
Member

Fixed the issue in the OP: https://phpstan.org/r/14f773c7-b8fd-4666-b0d2-5f6ba10037cf

I'll keep track of #1722 (comment) to do it later :)

Fixed by: phpstan/phpstan-src@a9d5c8b

@github-actions
Copy link

github-actions bot commented May 3, 2021

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants