Expected type 'bool'. Found 'Illuminate\Auth\Access\Response'.intelephense(1006)

292 Views Asked by At

I am working on a Laravel 10 on Visual Studio Code with intelephense extension installed. I am getting error on my policy classes saying,

Expected type 'bool'. Found 'Illuminate\Auth\Access\Response'.intelephense(1006)

Here is a portion of my code:

public function create(User $user): bool
{
    $roles = $user->roles->pluck('id')->toArray();
    return in_array(Role::TRAINING_MANAGER, $roles)
        ? Response::allow()
        : Response::deny();
}

How to get ride of this error notification?

Update

I should have changed the return type to Response.

2

There are 2 best solutions below

0
Top-Master On

If OP's create(...) is a route-handler (aka controller):

Simple, remove : bool of the controller/handler.

The line:

public function create(User $user): bool

Should be:

public function create(User $user)

Else, try:

public function create(User $user): bool
{
    $roles = $user->roles->pluck('id')->toArray();
    return in_array(Role::TRAINING_MANAGER, $roles);
}
0
Alex Rodrigues On

Type declarations in the PHP documentation are well explained, in one read.

You are trying to return an Object, but you are typing the return as Boolean

Returns Boolean

public function update(User $user, Post $post): bool
{
    return $user->id === $post->user_id;
}

Returns the Object

public function update(User $user, Post $post): Response
{
    return $user->id === $post->user_id
                ? Response::allow()
                : Response::deny('You do not own this post.');
}