1 | <?php |
||||
2 | |||||
3 | namespace Hayrullah\Likes\Traits; |
||||
4 | |||||
5 | use Hayrullah\LaravelLikes\Models\Like; |
||||
0 ignored issues
–
show
|
|||||
6 | use Illuminate\Database\Eloquent\Collection; |
||||
7 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
||||
8 | use Illuminate\Support\Facades\Auth; |
||||
9 | |||||
10 | /** |
||||
11 | * |
||||
12 | * @license MIT |
||||
13 | * @package Hayrullah/laravel-likes |
||||
14 | * |
||||
15 | * Copyright @2020 Zaher Khirullah |
||||
16 | */ |
||||
17 | trait Likable |
||||
18 | { |
||||
19 | /** |
||||
20 | * Add deleted observer to delete likes registers |
||||
21 | * |
||||
22 | * @return void |
||||
23 | */ |
||||
24 | public static function bootLikable() |
||||
25 | { |
||||
26 | static::deleted(function ($model) { |
||||
27 | $model->likes()->delete(); |
||||
28 | }); |
||||
29 | } |
||||
30 | |||||
31 | /** |
||||
32 | * Toggle the like status from this Object |
||||
33 | * |
||||
34 | * @param int $user_id |
||||
35 | */ |
||||
36 | public function toggleLike($user_id = null) |
||||
37 | { |
||||
38 | $this->isLiked($user_id) ? $this->removeLike($user_id) : $this->addLike($user_id); |
||||
39 | } |
||||
40 | |||||
41 | /** |
||||
42 | * Check if the user has liked this Object |
||||
43 | * |
||||
44 | * @param int $user_id |
||||
45 | * |
||||
46 | * @return boolean |
||||
47 | */ |
||||
48 | public function isLiked($user_id = null) |
||||
49 | { |
||||
50 | return $this->likes()->where('user_id', $this->getUserId($user_id))->exists(); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->getUserId($user_id) targeting Hayrullah\Likes\Traits\Likable::getUserId() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() It seems like
$user_id can also be of type integer ; however, parameter $user_id of Hayrullah\Likes\Traits\Likable::getUserId() does only seem to accept null , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
51 | } |
||||
52 | |||||
53 | /** |
||||
54 | * Define a polymorphic one-to-many relationship. |
||||
55 | * |
||||
56 | * @return MorphMany |
||||
57 | */ |
||||
58 | public function likes() |
||||
59 | { |
||||
60 | return $this->morphMany(Like::class, 'likable'); |
||||
0 ignored issues
–
show
It seems like
morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * @param null $user_id (if null its added to the auth user) |
||||
0 ignored issues
–
show
|
|||||
65 | * |
||||
66 | * @return null |
||||
67 | */ |
||||
68 | private function getUserId($user_id = null) |
||||
69 | { |
||||
70 | $user_id = ($user_id) ? $user_id : null; |
||||
0 ignored issues
–
show
|
|||||
71 | if (!$user_id) { |
||||
0 ignored issues
–
show
|
|||||
72 | \auth()->check() ? Auth::id() : null; |
||||
73 | } |
||||
74 | |||||
75 | return $user_id; |
||||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * Remove this Object from the user likes |
||||
80 | * |
||||
81 | * @param int $user_id [if null its added to the auth user] |
||||
82 | * |
||||
83 | */ |
||||
84 | public function removeLike($user_id = null) |
||||
85 | { |
||||
86 | $this->likes()->where('user_id', $this->getUserId($user_id))->delete(); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->getUserId($user_id) targeting Hayrullah\Likes\Traits\Likable::getUserId() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() It seems like
$user_id can also be of type integer ; however, parameter $user_id of Hayrullah\Likes\Traits\Likable::getUserId() does only seem to accept null , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
87 | } |
||||
88 | |||||
89 | /** |
||||
90 | * Add this Object to the user likes |
||||
91 | * |
||||
92 | * @param int $user_id |
||||
93 | */ |
||||
94 | public function addLike($user_id = null) |
||||
95 | { |
||||
96 | $like = new Like(['user_id' => $this->getUserId($user_id)]); |
||||
0 ignored issues
–
show
It seems like
$user_id can also be of type integer ; however, parameter $user_id of Hayrullah\Likes\Traits\Likable::getUserId() does only seem to accept null , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() Are you sure the usage of
$this->getUserId($user_id) targeting Hayrullah\Likes\Traits\Likable::getUserId() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
97 | $this->likes()->save($like); |
||||
98 | } |
||||
99 | |||||
100 | /** |
||||
101 | * Return a collection with the Users who marked as like this Object. |
||||
102 | * |
||||
103 | * @return Collection |
||||
104 | */ |
||||
105 | public function likedBy() |
||||
106 | { |
||||
107 | $likes = $this->likes()->with('user')->get(); |
||||
108 | |||||
109 | return $likes->mapWithKeys(function ($like) { |
||||
110 | return [$like['user']->id => $like['user']]; |
||||
111 | }); |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * Count the number of likes |
||||
116 | * |
||||
117 | * @return int |
||||
118 | */ |
||||
119 | public function getLikesCountAttribute() |
||||
120 | { |
||||
121 | return $this->likes()->count(); |
||||
122 | } |
||||
123 | |||||
124 | /** |
||||
125 | * @return likesCount attribute |
||||
0 ignored issues
–
show
The type
Hayrullah\Likes\Traits\likesCount was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
126 | */ |
||||
127 | public function likesCount() |
||||
128 | { |
||||
129 | return $this->likes_count; |
||||
130 | } |
||||
131 | } |
||||
132 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths