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.
belongsToManydoesn't return anarray... relationship methods return Relationship Type objects@return arrayabove yourpublic function roles()is wrong; your code saysreturn $this->belongsToMany()->withTimestamps(); that is not an Array. Read your output; it's telling your exactly that. Is there something unclear about that?