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_shift, null coalescing operator and throw #8164

Closed
verfriemelt-dot-org opened this issue Oct 14, 2022 · 8 comments
Closed

array_shift, null coalescing operator and throw #8164

verfriemelt-dot-org opened this issue Oct 14, 2022 · 8 comments

Comments

@verfriemelt-dot-org
Copy link
Contributor

Bug report

playground: https://phpstan.org/r/3a7ec8b3-fd08-4eee-b3a7-2f2098b6ec15

array_shift($args) ?? throw new \RuntimeException('missing center lat');

yields: Expression on left side of ?? is not nullable.

Expected output

since array_shift can and will return null, i'd expect this to be valid. and raise no errors

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

as happy as ever, this is such a help on a day to day basis! :)

@herndlm
Copy link
Contributor

herndlm commented Oct 14, 2022

interesting, ArrayShiftFunctionReturnTypeExtension should handle this case I think, but apparently there is no null return type added https://phpstan.org/r/a5f5f209-3b16-498b-bd6f-889215861532

@herndlm
Copy link
Contributor

herndlm commented Oct 14, 2022

ah I'm an idiot, of course it is added, but mixed|null is mixed xD so the problem is most likely then that mixed ?? throw in that context does not work correctly :)

@rvanvelzen
Copy link
Contributor

array_shift should probably be marked as having side effects. Perhaps all functions with by-ref parameters should be marked as such?

@mad-briller
Copy link
Contributor

i tried manually adding 'array_shift' => ['hasSideEffects' => true] to functionMetadata and separately adding a stub to arrayFunctions that had @phpstan-impure on it, but neither of those things rectified the issue,
am i missing the correct way to mark something as impure?

@staabm
Copy link
Contributor

staabm commented Oct 18, 2022

Did you follow the steps in the file comment of https://github.com/phpstan/phpstan-src/blob/1.9.x/resources/functionMetadata.php ?

@mad-briller
Copy link
Contributor

not gonna lie, i missed that comment entirely, thanks @staabm

however manually updating the file should be a quick estimate of the outcome of those steps right? so there has to be another issue at play here

@ondrejmirtes
Copy link
Member

Duplicate of #8084

@ondrejmirtes ondrejmirtes marked this as a duplicate of #8084 Oct 26, 2022
@github-actions
Copy link

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 Nov 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants