-
Notifications
You must be signed in to change notification settings - Fork 148
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
adds isStatic and isNotStatic #310
base: master
Are you sure you want to change the base?
Conversation
what would be a real life use case for this exactly? What property of a static function can break 3rd party code versus non-static function that simply does not use |
So if you're writing an API that accepts a Closure but contextually binds it later on it will stop the code from working. $obj = new stdClass();
$function = static function () {
return 'foo';
};
$function->bindTo($obj); This would then throw an exception like 'Cannot bind an instance to a static closure'. A good example of where I've seen this happen is with Pest where you provide closures where the Closure is then bound to a different object. There's probably less of a need for |
src/Assert.php
Outdated
Assert::isCallable($closure); | ||
$reflection = new ReflectionFunction($closure); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not type safe, because:
- The argument
$closure
is not typed, except with a docblock - The docblock says
Closure
, but is enforced withisCallable
- The assertion
isCallable
usesis_callable()
, which has typecallable|mixed
- But ReflectionFunction accepts
Closure|string
End result, the input type is enforced as callable
and then passed to a constructor that accepts Closure|string
. I believe the correct solution here is to type cast:
```php
if (($closure instanceof Closure) === false) {
$closure = Closure::fromCallable($closure);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by just type hinting as that made the most sense to me. It also then matches the throws assertion.
Changes
isStatic
andisNotStatic
.Why
This can be useful in some libraries where you want to make sure that the end user hasn't provided a static function as an argument.