Skip to content
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

Fix call GateCollector using callback instead of array #867

Merged

Conversation

Douglasdc3
Copy link
Contributor

This fixes #864 & #863

Pre Laravel 5.7 the the $gate->after would not explicity check for optional / null. Laravel 5.7 the Gate class in Laravel will explicity check the callback signature this does not work with array arguments.

Gate Class (Laravel 5.7)

protected function callbackAllowsGuests($callback)
{
        $parameters = (new ReflectionFunction($callback))->getParameters();

        return isset($parameters[0]) && $this->parameterAllowsGuests($parameters[0]);
}

The $callable will be array as argument, The ReflectionFunction can only take callable argument.

The reason we get this far into the code and is not cuaght by the Gate contract is the fact that the contract expects a callable type since an array is considered callable if you use a object reference as the first argument and string argument as second argument as method name.

This is a bit of breaking change between Laravel 5.6 - Laravel 5.6. My fix is backwards compatible with Laravel 5.6.

This fixes barryvdh#862 & barryvdh#863

Pre Laravel 5.7 the the `$gate->after` would not explicity check for optional / null. Laravel 5.7 the Gate class in Laravel will explicity check the callback signature this does not work with array arguments.

Gate Class (Laravel 5.7)

```php
protected function callbackAllowsGuests($callback)
{
        $parameters = (new ReflectionFunction($callback))->getParameters();

        return isset($parameters[0]) && $this->parameterAllowsGuests($parameters[0]);
}
```

The `$callable` will be array  as argument, The `ReflectionFunction` can only take callable argument.

The reason we get this far into the code and is not cuaght by the Gate contract is the fact that the contract expects a `callable` type since an array is considered callable if you use a object reference as the first argument and string argument as second argument as method name.

This is a bit of breaking change between Laravel 5.6 - Laravel 5.6. My fix is backwards compatible with Laravel 5.6.
@drbyte
Copy link

drbyte commented Sep 14, 2018

Thanks @Douglasdc3 -- That fixed it for me!

@annya-a
Copy link

annya-a commented Sep 26, 2018

Works like a charm. Thanks @Douglasdc3

@barryvdh barryvdh merged commit 706192d into barryvdh:master Sep 28, 2018
@Douglasdc3 Douglasdc3 deleted the fix/864-reflection-function-exception branch September 28, 2018 13:56
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.

ReflectionFunction::__construct() expects parameter 1 to be string, array given
4 participants