phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

Disallow extra parameters for callables

Open enumag opened this issue 2 years ago • 5 comments

Attempting to fix https://github.com/phpstan/phpstan/issues/9248

Should this be bleeding-edge only error for now?

enumag avatar May 09 '23 07:05 enumag

You've opened the pull request against the latest branch 1.11.x. If your code is relevant on 1.10.x and you want it to be released sooner, please rebase your pull request and change its target to 1.10.x.

phpstan-bot avatar May 09 '23 07:05 phpstan-bot

As you can see from the CI results this isn't the right fix. This is totally OK to do for situations where it doesn't cause fatal errors. A callable might be called with a parameter but the passed callable might not be interested in that parameter.

Instead, we need to somehow remember when a callable is created from an internal function affected by this behaviour. This includes strlen(...) (first-class callable - ClosureType) and also 'strlen' - ConstantStringType. And then use this information in CallableTypeHelper.

We might need to require ParametersAcceptorWithPhpDocs instead of ParametersAcceptor in CallableTypeHelper. Think of ParametersAcceptorWithPhpDocs as ExtendedParametersAcceptor, similar to ExtendedMethodReflection. And add a relevant method on ParametersAcceptorWithPhpDocs so that we can ask about this.

ondrejmirtes avatar May 09 '23 07:05 ondrejmirtes

Yeah, I expected something like that. That's why I marked it as a draft.

CallableTypeHelper is used in CallableType and ClosureType.

I believe I only need ParametersAcceptorWithPhpDocs for the $theirs argument which is the passed callback function. But how can I obtain ParametersAcceptorWithPhpDocs implementation instead of ParametersAcceptor in those two places?

EDIT: It seems like both ClosureType and CallableType would need to implement ParametersAcceptorWithPhpDocs which won't be easy considering all the places where they can be created.

enumag avatar May 09 '23 08:05 enumag

Your EDIT is right.

which won't be easy considering all the places where they can be created

Exactly, these are the places that need to pass in the information about the function being internal or not.

ondrejmirtes avatar May 09 '23 12:05 ondrejmirtes

Exactly, these are the places that need to pass in the information about the function being internal or not.

Right now I'm more concerned about how to correctly handle the extra data which ParametersAcceptorWithPhpDocs already has since correctly handling that is a pre-requisite:

  • more restrictive getParameters()
  • getPhpDocReturnType()
  • getNativeReturnType()

enumag avatar May 09 '23 13:05 enumag