-2

I'm running php ./vendor/bin/phpstan analyse --error-format github

I get this issues:

::error file=app/Models/User.php,line=55,col=0::Method App\Models\User::roles() return type with generic class Illuminate\Database\Eloquent\Relations\BelongsToMany does not specify its types: TRelatedModel
::error file=app/Models/User.php,line=66,col=0::Method App\Models\User::authorizeRoles() has parameter $roles with no value type specified in iterable type array.
::error file=app/Models/User.php,line=80,col=0::Method App\Models\User::hasAnyRole() has parameter $roles with no value type specified in iterable type array.

this's user.php model

<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Ramsey\Uuid\Type\Integer;

class User extends Authenticatable {
    use HasApiTokens;
    use HasFactory;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'image',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * 
     * 
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */

    public function roles()
    {
        return $this->belongsToMany('App\Models\Role')->withTimestamps();
    }

    /**
     * 
     * @param array $roles
     * @return boolean
     */

    public function authorizeRoles(array $roles)
    {
        if ($this->hasAnyRole($roles)) {
            return true;
        }
        abort(401, 'Esta acción no está autorizada.');
    }

    /**
     * 
     * @param string|array $roles
     * @return boolean
     */

    public function hasAnyRole(string|array $roles)
    {
        if (is_array($roles)) {
            foreach ($roles as $role) {
                if ($this->hasRole($role)) {
                    return true;
                }
            }
        } else {
            if ($this->hasRole($roles)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 
     * @param string $role
     * @return boolean
     */

    public function hasRole(string $role)
    {
        if ($this->roles()->where('name', $role)->first()) {
            return true;
        }
        return false;
    }

    /**
     * 
     *
     * @return boolean
     */

    public function isAdmin()
    {
        if ($this->roles()->where('name', 'Administrador')->first()) {
            return true;
        }
        return false;
    }
}

what's wrong??

PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.

5
  • 1
    belongsToMany doesn't return an array ... relationship methods return Relationship Type objects Commented Jun 21, 2022 at 17:39
  • It fixed, I update the question Commented Jun 21, 2022 at 17:40
  • 1
    @return array above your public function roles() is wrong; your code says return $this->belongsToMany()->withTimestamps(); that is not an Array. Read your output; it's telling your exactly that. Is there something unclear about that? Commented Jun 21, 2022 at 17:44
  • github.com/laravel/framework/blob/9.x/src/Illuminate/Database/… that is what it returns Commented Jun 21, 2022 at 17:45
  • I fixed that issue, added type object..... but I'm having others error Commented Jun 21, 2022 at 17:47

1 Answer 1

0
  1. This error happens because BelongsToMany is a generic class in Larastan. And you need to provide the generic types. So change your docblock to BelongsToMany<Role> Or you can disable this check with checkGenericClassInNonGenericObjectType: false More information here: https://phpstan.org/config-reference#vague-typehints

  2. The remaining errors are because PHPStan wants you to specify types for array type. There is a blog post with more information about it here: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type Basically you need to do string[] or array<int, string> or something similar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.