Skip to content

Commit 5c0a1d7

Browse files
committed
fix: update type hints in model relationships for better type safety
1 parent e48e749 commit 5c0a1d7

File tree

8 files changed

+14
-25
lines changed

8 files changed

+14
-25
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ parameters:
33
- message: '#Cannot cast mixed to string#'
44
path: src/*
55

6-
- message: '#Method CSlant\\LaravelLike\\Models\\Like::user\(\) return type with generic class Illuminate\\Database\\Eloquent\\Relations\\BelongsTo does not specify its types: TRelatedModel, TChildModel#'
7-
path: src/Models/Like.php
86
-
97
identifier: trait.unused

src/Models/Like.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CSlant\LaravelLike\Traits\InteractionRelationship;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Illuminate\Database\Eloquent\Relations\MorphTo;
910
use Illuminate\Database\Query\Builder;
1011
use Illuminate\Support\Carbon;
1112

@@ -27,23 +28,13 @@
2728
*/
2829
class Like extends Model
2930
{
30-
/**
31-
* The attributes that are mass assignable.
32-
*
33-
* @var array<int, string>
34-
*/
3531
protected $fillable = [
3632
'user_id',
3733
'model_id',
3834
'model_type',
3935
'type',
4036
];
4137

42-
/**
43-
* The attributes that should be cast.
44-
*
45-
* @var array<string, string>
46-
*/
4738
protected $casts = [
4839
'model_type' => 'string',
4940
'type' => InteractionTypeEnum::class,
@@ -52,7 +43,7 @@ class Like extends Model
5243
/**
5344
* Get the user that owns the like.
5445
*
55-
* @return BelongsTo<Model, self>
46+
* @return BelongsTo<*, *>
5647
*/
5748
public function user(): BelongsTo
5849
{
@@ -70,9 +61,9 @@ public function user(): BelongsTo
7061
/**
7162
* Get the model that the like belongs to.
7263
*
73-
* @return BelongsTo<Model, self>
64+
* @return MorphTo<*, *>
7465
*/
75-
public function model(): BelongsTo
66+
public function model(): MorphTo
7667
{
7768
return $this->morphTo();
7869
}

src/Traits/InteractionRelationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* @package CSlant\LaravelLike\Traits
1515
* @mixin Model
1616
*
17-
* @method MorphOne<self, Model> morphOne(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
18-
* @method MorphMany<self, Model> morphMany(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
17+
* @method MorphOne<self, *> morphOne(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
18+
* @method MorphMany<self, *> morphMany(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
1919
*/
2020
trait InteractionRelationship
2121
{

src/Traits/Like/LikeCount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* @package CSlant\LaravelLike\Traits\Like
1212
* @mixin Model
1313
*
14-
* @method MorphMany<self, Model> likesTo()
15-
* @method MorphMany<self, Model> dislikesTo()
14+
* @method MorphMany<self, *> likesTo()
15+
* @method MorphMany<self, *> dislikesTo()
1616
*/
1717
trait LikeCount
1818
{

src/Traits/Like/LikeScopes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* @package CSlant\LaravelLike\Traits\Like
1414
* @mixin Model
1515
*
16-
* @method MorphOne<self, Model> likeOne()
17-
* @method MorphMany<self, Model> likes()
16+
* @method MorphOne<self, *> likeOne()
17+
* @method MorphMany<self, *> likes()
1818
* @method bool isInteractedBy(int $userId, null|InteractionTypeEnum $type)
1919
*/
2020
trait LikeScopes

src/Traits/Love/LoveCount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @package CSlant\LaravelLike\Traits\Love
1212
* @mixin Model
1313
*
14-
* @method MorphMany<self, Model> lovesTo()
14+
* @method MorphMany<self, *> lovesTo()
1515
*/
1616
trait LoveCount
1717
{

src/Traits/Love/LoveScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* @package CSlant\LaravelLike\Traits\Love
1414
* @mixin Model
1515
*
16-
* @method MorphOne<self, Model> likeOne()
17-
* @method MorphMany<self, Model> likes()
16+
* @method MorphOne<self, *> likeOne()
17+
* @method MorphMany<self, *> likes()
1818
* @method bool isInteractedBy(int $userId, null|InteractionTypeEnum $type)
1919
*/
2020
trait LoveScope

src/UserHasInteraction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait UserHasInteraction
2020
/**
2121
* Get all likes of the user. This method is used for eager loading.
2222
*
23-
* @return HasMany<Like, Model>
23+
* @return HasMany<self, Model>
2424
*/
2525
public function likes(): HasMany
2626
{

0 commit comments

Comments
 (0)