nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Callback - bug with closure and private/protected methods #270

Closed ncou closed 2 years ago

ncou commented 2 years ago

Hi,

Nice piece of code. There is a bug in the Callback::unwrap function (https://github.com/nette/utils/blob/master/src/Utils/Callback.php#L127), the return value could be an array and not a callable in case you have a closure using a private method. for exemple :

class MyClass
{
  private function myPrivateMethod() {}

  public getCallable(): callable
  { 
    return \Closure::fromCallable[$this, 'myPrivateMethod']
  }
}

Callback::unwrap((new MyClass())->getCallable()); // This will raise a TypeError

I think the return typehint for the unwrap function to be 'array|callable' instead of simply 'callable'.

And by the way i have see in the toString funtion a case for "lambda" when does this "if" is used ?

Have a good day.

dg commented 2 years ago

{lamda} was used for functions created by create_function()

ncou commented 2 years ago

Thanks for the answer :+1:

dg commented 2 years ago

Thanks for report :-)