Skip to content

Loosen the requirements for the $function parameter of the register_shutdown_function function#240

Merged
ondrejmirtes merged 1 commit intophpstan:masterfrom
ste93cry:fix/issue-2998-register-shutdown-function-false-positive
Jun 20, 2020
Merged

Loosen the requirements for the $function parameter of the register_shutdown_function function#240
ondrejmirtes merged 1 commit intophpstan:masterfrom
ste93cry:fix/issue-2998-register-shutdown-function-false-positive

Conversation

@ste93cry
Copy link
Copy Markdown
Contributor

Fixes phpstan/phpstan#2998, by loosening the requirements of the $function argument of the register_shutdown_function function

@ondrejmirtes
Copy link
Copy Markdown
Member

ondrejmirtes commented Jun 12, 2020

Hi, I don't think that the void return typehint is the problem. The problem is that register_shutdown_function expects an argument to be passed in - a callable. As the function cannot be called without an argument (https://3v4l.org/n6n0p), there has to be some kind of hack in the PHP source to actually allow passing register_shutdown_function in there. Can you look into it?

@ste93cry
Copy link
Copy Markdown
Contributor Author

Hi, I don't think that the void return typehint is the problem

I agree, but I was not sure about the syntax to require a callable with void as return type and any number of arguments

As the function cannot be called without an argument (https://3v4l.org/n6n0p), there has to be some kind of hack in the PHP source to actually allow passing register_shutdown_function in there

Maybe I didn't understand the issue you're seeing, but all the arguments after the first one are passed to the callback, so in the case of the example snippet what's happening is that I'm calling the register_shutdown_function by passing as callback itself and as argument that will be passed to the callback the static lambda. It's not straightforward, but it's perfectly legit

@ondrejmirtes
Copy link
Copy Markdown
Member

Shouldn't we think about this more generally? It should probably be okay to pass a closure that returns a value somewhere a callable with void return type is expected.

@ste93cry
Copy link
Copy Markdown
Contributor Author

It should probably be okay to pass a closure that returns a value somewhere a callable with void return type is expected

In a strongly typed language it would be forbidden because function signature would not match, so I don't think we should encourage users that doing this in PHP is fine. The issue I'm trying to solve is that the first argument of register_shutdown_function should accept any callable with any return type (it's ignored anway from PHP) and with a number of parameters equal to the number of the rest of the arguments passed to the register_shutdown_function function itself

@ondrejmirtes
Copy link
Copy Markdown
Member

Alright :)

@ondrejmirtes ondrejmirtes merged commit 749f50b into phpstan:master Jun 20, 2020
@ste93cry ste93cry deleted the fix/issue-2998-register-shutdown-function-false-positive branch June 20, 2020 15:21
@ste93cry
Copy link
Copy Markdown
Contributor Author

Do you think it would be possible to improve what I did to check that the arguments passed to the callable are correct? If so, how would you do it?

@ondrejmirtes
Copy link
Copy Markdown
Member

@ste93cry That would only be possible with a custom rule. But you can do this instead:

register_shutdown_function(function (): void use ($foo, $bar) {
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

register_shutdown_function with itself as callback false-positive

2 participants