Skip to content

Support getting reflection of an anonymous function#100

Closed
Lctrs wants to merge 1 commit intophpstan:masterfrom
Lctrs:closure-reflection
Closed

Support getting reflection of an anonymous function#100
Lctrs wants to merge 1 commit intophpstan:masterfrom
Lctrs:closure-reflection

Conversation

@Lctrs
Copy link
Copy Markdown
Contributor

@Lctrs Lctrs commented Jan 14, 2020

From https://www.php.net/manual/en/class.closure.php :

Besides the methods listed here, this class also has an __invoke method.

@ondrejmirtes
Copy link
Copy Markdown
Member

Hi, I don't understand what you're trying to achieve. Can you first post something on phpstan.org that fails?

@Lctrs
Copy link
Copy Markdown
Contributor Author

Lctrs commented Jan 14, 2020

Hi @ondrejmirtes,

Well, nothing fails.

In rectorphp/rector#2658, I worked on a class that could resolve any calls (FuncCall|MethodCall|StaticCall) to the called function or method reflection (FunctionReflection|MethodReflection).

I had one problem when FuncCall::$name is an Expr (can be obtained with eg $f = function() {}; $f();). I could not get the real signature of the function (the one without the $newthis param). So I implemented this to resolve my case. I thought maybe you would consider this a valid case and accept a PR for this.

I don't know if I did the right thing though. Here is where I implemented it, maybe you can understand me better with this.

From https://www.php.net/manual/en/class.closure.php :
> Besides the methods listed here, this class also has an __invoke method.
@ondrejmirtes
Copy link
Copy Markdown
Member

For $expr(), Type::getCallableParametersAcceptors() should be used. Maybe this will simplify the logic you implemented in Rector too?

@ondrejmirtes
Copy link
Copy Markdown
Member

It works with this too for example:

$func = 'date';
$func();

So ConstantStringType::getCallableParametersAcceptors() also answers this correctly.

@Lctrs
Copy link
Copy Markdown
Contributor Author

Lctrs commented Jan 14, 2020

Something like this ?

$type = $scope->getType($funcCall->name);
$parametersAcceptors = $type->getCallableParametersAcceptors($scope);

return new NativeFunctionReflection(
    '{closure}', //TODO: get real name
    $parametersAcceptors,
    null,
    TrinaryLogic::createMaybe()
);

Is this the PHPStan way of doing the following ?

$f = function() {};
$r = new \ReflectionFunction($f);

$f = 'date';
$r = new \ReflectionFunction($f);

@ondrejmirtes
Copy link
Copy Markdown
Member

Not sure what you need FunctionReflection, but yeah, something like that.

@Lctrs Lctrs deleted the closure-reflection branch January 14, 2020 15:42
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.

2 participants