@tisuchi I have a shockingly similar implementation and I'm running into this:
Method `App\Models\Mod::comments()` should return `Illuminate\Database\Eloquent\Relations\MorphMany<App\Models\Comment, App\Models\Mod>` but returns `Illuminate\Database\Eloquent\Relations\MorphMany<App\Models\Comment, $this(App\Models\Mod)>`.
🪪 return.type
💡 Template type `TDeclaringModel` on class `Illuminate\Database\Eloquent\Relations\MorphMany` is not covariant. Learn more: https://phpstan.org/blog/whats-up-with-template-covariant
Am I missing something in the implementation?
namespace App\Contracts;
use Illuminate\Database\Eloquent\Relations\MorphMany;
/**
* @template TModel of \Illuminate\Database\Eloquent\Model
*/
interface Commentable
{
/**
* Get all comments for this commentable model.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<\App\Models\Comment, TModel>
*/
public function comments(): MorphMany;
/**
* Get all root comments for this commentable model.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<\App\Models\Comment, TModel>
*/
public function rootComments(): MorphMany;
}
namespace App\Traits;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Relations\MorphMany;
/**
* @template TModel of \Illuminate\Database\Eloquent\Model
* @mixin TModel
*/
trait HasComments
{
/**
* The relationship between a model and its comments.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<\App\Models\Comment, TModel>
*/
public function comments(): MorphMany
{
return $this->morphMany(Comment::class, 'commentable');
}
/**
* The relationship between a model and its root comments.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<\App\Models\Comment, TModel>
*/
public function rootComments(): MorphMany
{
return $this->morphMany(Comment::class, 'commentable')
->whereNull('parent_id')
->whereNull('root_id')
->orderBy('created_at', 'desc');
}
}
/**
* @implements Commentable<self>
*/
class Mod extends Model implements Commentable
{
/** @use HasComments<self> */
use HasComments;
}