Skip to content

Commit 15d1bbc

Browse files
committed
fix: enhance PHPDoc annotations and add validation for user model in Like trait
1 parent f8d59f8 commit 15d1bbc

File tree

6 files changed

+14
-6
lines changed

6 files changed

+14
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"phpstan/phpstan-phpunit": "^2.0"
5454
},
5555
"scripts": {
56+
"a": "vendor/bin/phpstan analyse",
5657
"analyse": "vendor/bin/phpstan analyse",
5758
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
5859
"test": "vendor/bin/pest"

phpstan-baseline.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ parameters:
55

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

src/Models/Like.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@ class Like extends Model
5252
/**
5353
* Get the user that owns the like.
5454
*
55-
* @return BelongsTo
55+
* @return BelongsTo<Model, self>
5656
*/
5757
public function user(): BelongsTo
5858
{
5959
$userModel = (string) (config('like.users.model') ?? config('auth.providers.users.model'));
60+
61+
if (!is_a($userModel, Model::class, true)) {
62+
throw new \InvalidArgumentException("The user model must be a valid Eloquent model class.");
63+
}
64+
6065
$userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id');
6166

6267
return $this->belongsTo($userModel, $userForeignKey);

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 likesTo()
15-
* @method MorphMany dislikesTo()
14+
* @method MorphMany<self, Model> likesTo()
15+
* @method MorphMany<self, Model> dislikesTo()
1616
*/
1717
trait LikeCount
1818
{

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 lovesTo()
14+
* @method MorphMany<self, Model> lovesTo()
1515
*/
1616
trait LoveCount
1717
{

src/UserHasInteraction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
*
1414
* @package CSlant\LaravelLike\Traits
1515
* @mixin Model
16-
* @method HasMany hasMany(string $related, string $foreignKey = null, string $localKey = null)
16+
* @method HasMany<Like, Model> hasMany(string $related, string $foreignKey = null, string $localKey = null)
1717
*/
1818
trait UserHasInteraction
1919
{
2020
/**
2121
* Get all likes of the user. This method is used for eager loading.
2222
*
23-
* @return HasMany
23+
* @return HasMany<Like, Model>
2424
*/
2525
public function likes(): HasMany
2626
{

0 commit comments

Comments
 (0)