This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Tags; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Builder; |
||
6 | use Illuminate\Database\Eloquent\Collection; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Database\Eloquent\Relations\MorphToMany; |
||
9 | use InvalidArgumentException; |
||
10 | |||
11 | trait HasTags |
||
12 | { |
||
13 | protected $queuedTags = []; |
||
14 | |||
15 | public static function getTagClassName(): string |
||
16 | { |
||
17 | return Tag::class; |
||
18 | } |
||
19 | |||
20 | public static function bootHasTags() |
||
21 | { |
||
22 | static::created(function (Model $taggableModel) { |
||
23 | if (count($taggableModel->queuedTags) > 0) { |
||
24 | $taggableModel->attachTags($taggableModel->queuedTags); |
||
25 | |||
26 | $taggableModel->queuedTags = []; |
||
27 | } |
||
28 | }); |
||
29 | |||
30 | static::deleted(function (Model $deletedModel) { |
||
31 | $tags = $deletedModel->tags()->get(); |
||
32 | |||
33 | $deletedModel->detachTags($tags); |
||
34 | }); |
||
35 | } |
||
36 | |||
37 | public function tags(): MorphToMany |
||
38 | { |
||
39 | return $this |
||
0 ignored issues
–
show
|
|||
40 | ->morphToMany(self::getTagClassName(), 'taggable') |
||
41 | ->ordered(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $locale |
||
46 | */ |
||
47 | public function tagsTranslated($locale = null): MorphToMany |
||
48 | { |
||
49 | $locale = ! is_null($locale) ? $locale : app()->getLocale(); |
||
50 | |||
51 | return $this |
||
0 ignored issues
–
show
It seems like
morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
52 | ->morphToMany(self::getTagClassName(), 'taggable') |
||
53 | ->select('*') |
||
54 | ->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated") |
||
55 | ->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated") |
||
56 | ->ordered(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param string|array|\ArrayAccess|\Spatie\Tags\Tag $tags |
||
61 | */ |
||
62 | public function setTagsAttribute($tags) |
||
63 | { |
||
64 | if (! $this->exists) { |
||
0 ignored issues
–
show
The property
exists does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
65 | $this->queuedTags = $tags; |
||
66 | |||
67 | return; |
||
68 | } |
||
69 | |||
70 | $this->syncTags($tags); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
75 | * @param array|\ArrayAccess|\Spatie\Tags\Tag $tags |
||
76 | * |
||
77 | * @return \Illuminate\Database\Eloquent\Builder |
||
78 | */ |
||
79 | public function scopeWithAllTags(Builder $query, $tags, string $type = null): Builder |
||
80 | { |
||
81 | $tags = static::convertToTags($tags, $type); |
||
82 | |||
83 | collect($tags)->each(function ($tag) use ($query) { |
||
84 | $query->whereHas('tags', function (Builder $query) use ($tag) { |
||
85 | $query->where('tags.id', $tag ? $tag->id : 0); |
||
86 | }); |
||
87 | }); |
||
88 | |||
89 | return $query; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
94 | * @param array|\ArrayAccess|\Spatie\Tags\Tag $tags |
||
95 | * |
||
96 | * @return \Illuminate\Database\Eloquent\Builder |
||
97 | */ |
||
98 | View Code Duplication | public function scopeWithAnyTags(Builder $query, $tags, string $type = null): Builder |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
99 | { |
||
100 | $tags = static::convertToTags($tags, $type); |
||
101 | |||
102 | return $query->whereHas('tags', function (Builder $query) use ($tags) { |
||
103 | $tagIds = collect($tags)->pluck('id'); |
||
104 | |||
105 | $query->whereIn('tags.id', $tagIds); |
||
106 | }); |
||
107 | } |
||
108 | |||
109 | View Code Duplication | public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
110 | { |
||
111 | $tags = static::convertToTagsOfAnyType($tags); |
||
112 | |||
113 | collect($tags)->each(function ($tag) use ($query) { |
||
114 | $query->whereHas('tags', function (Builder $query) use ($tag) { |
||
115 | $query->where('tags.id', $tag ? $tag->id : 0); |
||
116 | }); |
||
117 | }); |
||
118 | |||
119 | return $query; |
||
120 | } |
||
121 | |||
122 | View Code Duplication | public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
123 | { |
||
124 | $tags = static::convertToTagsOfAnyType($tags); |
||
125 | |||
126 | return $query->whereHas('tags', function (Builder $query) use ($tags) { |
||
127 | $tagIds = collect($tags)->pluck('id'); |
||
128 | |||
129 | $query->whereIn('tags.id', $tagIds); |
||
130 | }); |
||
131 | } |
||
132 | |||
133 | public function tagsWithType(string $type = null): Collection |
||
134 | { |
||
135 | return $this->tags->filter(function (Tag $tag) use ($type) { |
||
0 ignored issues
–
show
The property
tags does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
136 | return $tag->type === $type; |
||
137 | }); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param array|\ArrayAccess|\Spatie\Tags\Tag $tags |
||
142 | * @param string|null $type |
||
143 | * @return $this |
||
144 | */ |
||
145 | View Code Duplication | public function attachTags($tags, string $type = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
146 | { |
||
147 | $className = static::getTagClassName(); |
||
148 | |||
149 | $tags = collect($className::findOrCreate($tags, $type)); |
||
150 | |||
151 | $this->tags()->syncWithoutDetaching($tags->pluck('id')->toArray()); |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string|\Spatie\Tags\Tag $tag |
||
158 | * |
||
159 | * @param string|null $type |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function attachTag($tag, string $type = null) |
||
163 | { |
||
164 | return $this->attachTags([$tag], $type); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param array|\ArrayAccess $tags |
||
169 | * |
||
170 | * @param string|null $type |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function detachTags($tags, string $type = null) |
||
174 | { |
||
175 | $tags = static::convertToTags($tags, $type); |
||
176 | |||
177 | collect($tags) |
||
178 | ->filter() |
||
179 | ->each(function (Tag $tag) { |
||
180 | $this->tags()->detach($tag); |
||
181 | }); |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param string|\Spatie\Tags\Tag $tag |
||
188 | * |
||
189 | * @param string|null $type |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function detachTag($tag, string $type = null) |
||
193 | { |
||
194 | return $this->detachTags([$tag], $type); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param array|\ArrayAccess $tags |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | View Code Duplication | public function syncTags($tags) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
203 | { |
||
204 | $className = static::getTagClassName(); |
||
205 | |||
206 | $tags = collect($className::findOrCreate($tags)); |
||
207 | |||
208 | $this->tags()->sync($tags->pluck('id')->toArray()); |
||
209 | |||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param array|\ArrayAccess $tags |
||
215 | * @param string|null $type |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | View Code Duplication | public function syncTagsWithType($tags, string $type = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
220 | { |
||
221 | $className = static::getTagClassName(); |
||
222 | |||
223 | $tags = collect($className::findOrCreate($tags, $type)); |
||
224 | |||
225 | $this->syncTagIds($tags->pluck('id')->toArray(), $type); |
||
226 | |||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | protected static function convertToTags($values, $type = null, $locale = null) |
||
231 | { |
||
232 | return collect($values)->map(function ($value) use ($type, $locale) { |
||
233 | if ($value instanceof Tag) { |
||
234 | if (isset($type) && $value->type != $type) { |
||
235 | throw new InvalidArgumentException("Type was set to {$type} but tag is of type {$value->type}"); |
||
236 | } |
||
237 | |||
238 | return $value; |
||
239 | } |
||
240 | |||
241 | $className = static::getTagClassName(); |
||
242 | |||
243 | return $className::findFromString($value, $type, $locale); |
||
244 | }); |
||
245 | } |
||
246 | |||
247 | protected static function convertToTagsOfAnyType($values, $locale = null) |
||
248 | { |
||
249 | return collect($values)->map(function ($value) use ($locale) { |
||
250 | if ($value instanceof Tag) { |
||
251 | return $value; |
||
252 | } |
||
253 | |||
254 | $className = static::getTagClassName(); |
||
255 | |||
256 | return $className::findFromStringOfAnyType($value, $locale); |
||
257 | }); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Use in place of eloquent's sync() method so that the tag type may be optionally specified. |
||
262 | * |
||
263 | * @param $ids |
||
264 | * @param string|null $type |
||
265 | * @param bool $detaching |
||
266 | */ |
||
267 | protected function syncTagIds($ids, string $type = null, $detaching = true) |
||
268 | { |
||
269 | $isUpdated = false; |
||
270 | |||
271 | // Get a list of tag_ids for all current tags |
||
272 | $current = $this->tags() |
||
273 | ->newPivotStatement() |
||
274 | ->where('taggable_id', $this->getKey()) |
||
0 ignored issues
–
show
It seems like
getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
275 | ->where('taggable_type', $this->getMorphClass()) |
||
0 ignored issues
–
show
It seems like
getMorphClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
276 | ->when($type !== null, function ($query) use ($type) { |
||
277 | $tagModel = $this->tags()->getRelated(); |
||
278 | |||
279 | return $query->join( |
||
280 | $tagModel->getTable(), |
||
281 | 'taggables.tag_id', |
||
282 | '=', |
||
283 | $tagModel->getTable().'.'.$tagModel->getKeyName() |
||
284 | ) |
||
285 | ->where('tags.type', $type); |
||
286 | }) |
||
287 | ->pluck('tag_id') |
||
288 | ->all(); |
||
289 | |||
290 | // Compare to the list of ids given to find the tags to remove |
||
291 | $detach = array_diff($current, $ids); |
||
292 | if ($detaching && count($detach) > 0) { |
||
293 | $this->tags()->detach($detach); |
||
294 | $isUpdated = true; |
||
295 | } |
||
296 | |||
297 | // Attach any new ids |
||
298 | $attach = array_unique(array_diff($ids, $current)); |
||
299 | if (count($attach) > 0) { |
||
300 | collect($attach)->each(function ($id) { |
||
301 | $this->tags()->attach($id, []); |
||
302 | }); |
||
303 | $isUpdated = true; |
||
304 | } |
||
305 | |||
306 | // Once we have finished attaching or detaching the records, we will see if we |
||
307 | // have done any attaching or detaching, and if we have we will touch these |
||
308 | // relationships if they are configured to touch on any database updates. |
||
309 | if ($isUpdated) { |
||
310 | $this->tags()->touchIfTouching(); |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.