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 | /** |
||
4 | * @todo Mt bom, analisar |
||
5 | */ |
||
6 | namespace Telefonica\Traits; |
||
7 | |||
8 | use InvalidArgumentException; |
||
9 | use Illuminate\Database\Eloquent\Model; |
||
10 | use Illuminate\Database\Eloquent\Builder; |
||
11 | use Illuminate\Database\Eloquent\Collection; |
||
12 | use Illuminate\Database\Eloquent\Relations\MorphToMany; |
||
13 | use Casa\Models\Economic\Routine; |
||
14 | |||
15 | trait HasRoutine |
||
16 | { |
||
17 | protected $queuedRoutine = []; |
||
18 | |||
19 | public static function getRoutineClassName(): string |
||
20 | { |
||
21 | return Routine::class; |
||
22 | } |
||
23 | |||
24 | View Code Duplication | public static function bootHasRoutine() |
|
0 ignored issues
–
show
|
|||
25 | { |
||
26 | static::created( |
||
27 | function (Model $routineableModel) { |
||
28 | if (count($routineableModel->queuedRoutine) > 0) { |
||
29 | $routineableModel->attachRoutine($routineableModel->queuedRoutine); |
||
30 | |||
31 | $routineableModel->queuedRoutine = []; |
||
32 | } |
||
33 | } |
||
34 | ); |
||
35 | |||
36 | static::deleted( |
||
37 | function (Model $deletedModel) { |
||
38 | $routines = $deletedModel->routines()->get(); |
||
39 | |||
40 | $deletedModel->detachRoutine($routines); |
||
41 | } |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | public function routines(): MorphToMany |
||
46 | { |
||
47 | 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 ![]() |
|||
48 | ->morphToMany(self::getRoutineClassName(), 'routineable') |
||
49 | ->ordered(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $locale |
||
54 | */ |
||
55 | View Code Duplication | public function routinesTranslated($locale = null): MorphToMany |
|
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. ![]() |
|||
56 | { |
||
57 | $locale = ! is_null($locale) ? $locale : app()->getLocale(); |
||
58 | |||
59 | 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 ![]() |
|||
60 | ->morphToMany(self::getRoutineClassName(), 'routineable') |
||
61 | ->select('*') |
||
62 | ->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated") |
||
63 | ->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated") |
||
64 | ->ordered(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string|array|\ArrayAccess|\\App\Models\Routine $routines |
||
69 | */ |
||
70 | public function setRoutineAttribute($routines) |
||
71 | { |
||
72 | 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;
![]() |
|||
73 | $this->queuedRoutine = $routines; |
||
74 | |||
75 | return; |
||
76 | } |
||
77 | |||
78 | $this->attachRoutine($routines); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
83 | * @param array|\ArrayAccess|\\App\Models\Routine $routines |
||
84 | * |
||
85 | * @return \Illuminate\Database\Eloquent\Builder |
||
86 | */ |
||
87 | View Code Duplication | public function scopeWithAllRoutine(Builder $query, $routines, 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. ![]() |
|||
88 | { |
||
89 | $routines = static::convertToRoutine($routines, $type); |
||
90 | |||
91 | collect($routines)->each( |
||
92 | function ($routine) use ($query) { |
||
93 | $query->whereHas( |
||
94 | 'routines', function (Builder $query) use ($routine) { |
||
95 | $query->where('routines.id', $routine ? $routine->id : 0); |
||
96 | } |
||
97 | ); |
||
98 | } |
||
99 | ); |
||
100 | |||
101 | return $query; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
106 | * @param array|\ArrayAccess|\\App\Models\Routine $routines |
||
107 | * |
||
108 | * @return \Illuminate\Database\Eloquent\Builder |
||
109 | */ |
||
110 | View Code Duplication | public function scopeWithAnyRoutine(Builder $query, $routines, 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. ![]() |
|||
111 | { |
||
112 | $routines = static::convertToRoutine($routines, $type); |
||
113 | |||
114 | return $query->whereHas( |
||
115 | 'routines', function (Builder $query) use ($routines) { |
||
116 | $routineIds = collect($routines)->pluck('id'); |
||
117 | |||
118 | $query->whereIn('routines.id', $routineIds); |
||
119 | } |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | View Code Duplication | public function scopeWithAllRoutineOfAnyType(Builder $query, $routines): 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. ![]() |
|||
124 | { |
||
125 | $routines = static::convertToRoutineOfAnyType($routines); |
||
126 | |||
127 | collect($routines)->each( |
||
128 | function ($routine) use ($query) { |
||
129 | $query->whereHas( |
||
130 | 'routines', function (Builder $query) use ($routine) { |
||
131 | $query->where('routines.id', $routine ? $routine->id : 0); |
||
132 | } |
||
133 | ); |
||
134 | } |
||
135 | ); |
||
136 | |||
137 | return $query; |
||
138 | } |
||
139 | |||
140 | View Code Duplication | public function scopeWithAnyRoutineOfAnyType(Builder $query, $routines): 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. ![]() |
|||
141 | { |
||
142 | $routines = static::convertToRoutineOfAnyType($routines); |
||
143 | |||
144 | return $query->whereHas( |
||
145 | 'routines', function (Builder $query) use ($routines) { |
||
146 | $routineIds = collect($routines)->pluck('id'); |
||
147 | |||
148 | $query->whereIn('routines.id', $routineIds); |
||
149 | } |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | public function routinesWithType(string $type = null): Collection |
||
154 | { |
||
155 | return $this->routines->filter( |
||
0 ignored issues
–
show
The property
routines 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;
![]() |
|||
156 | function (Routine $routine) use ($type) { |
||
157 | return $routine->type === $type; |
||
158 | } |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param array|\ArrayAccess|\\App\Models\Routine $routines |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | View Code Duplication | public function attachRoutine($routines) |
|
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. ![]() |
|||
168 | { |
||
169 | $className = static::getRoutineClassName(); |
||
170 | |||
171 | $routines = collect($className::findOrCreate($routines)); |
||
172 | |||
173 | $this->routines()->syncWithoutDetaching($routines->pluck('id')->toArray()); |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string|\\App\Models\Routine $routine |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function detachRoutine($routine) |
||
184 | { |
||
185 | return $this->detachRoutine([$routine]); |
||
0 ignored issues
–
show
array($routine) is of type array<integer,string|obj...pp\\Models\\Routine>"}> , but the function expects a string|object<\App\Models\Routine> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param array|\ArrayAccess $routines |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | View Code Duplication | public function syncRoutine($routines) |
|
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. ![]() |
|||
194 | { |
||
195 | $className = static::getRoutineClassName(); |
||
196 | |||
197 | $routines = collect($className::findOrCreate($routines)); |
||
198 | |||
199 | $this->routines()->sync($routines->pluck('id')->toArray()); |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param array|\ArrayAccess $routines |
||
206 | * @param string|null $type |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | View Code Duplication | public function syncRoutineWithType($routines, 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. ![]() |
|||
211 | { |
||
212 | $className = static::getRoutineClassName(); |
||
213 | |||
214 | $routines = collect($className::findOrCreate($routines, $type)); |
||
215 | |||
216 | $this->syncRoutineIds($routines->pluck('id')->toArray(), $type); |
||
217 | |||
218 | return $this; |
||
219 | } |
||
220 | |||
221 | View Code Duplication | protected static function convertToRoutine($values, $type = null, $locale = 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. ![]() |
|||
222 | { |
||
223 | return collect($values)->map( |
||
224 | function ($value) use ($type, $locale) { |
||
225 | if ($value instanceof Routine) { |
||
0 ignored issues
–
show
The class
Casa\Models\Economic\Routine does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
226 | if (isset($type) && $value->type != $type) { |
||
227 | throw new InvalidArgumentException("Type was set to {$type} but routine is of type {$value->type}"); |
||
228 | } |
||
229 | |||
230 | return $value; |
||
231 | } |
||
232 | |||
233 | $className = static::getRoutineClassName(); |
||
234 | |||
235 | return $className::findFromString($value, $type, $locale); |
||
236 | } |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | View Code Duplication | protected static function convertToRoutineOfAnyType($values, $locale = 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. ![]() |
|||
241 | { |
||
242 | return collect($values)->map( |
||
243 | function ($value) use ($locale) { |
||
244 | if ($value instanceof Routine) { |
||
0 ignored issues
–
show
The class
Casa\Models\Economic\Routine does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
245 | return $value; |
||
246 | } |
||
247 | |||
248 | $className = static::getRoutineClassName(); |
||
249 | |||
250 | return $className::findFromStringOfAnyType($value, $locale); |
||
251 | } |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Use in place of eloquent's sync() method so that the routine type may be optionally specified. |
||
257 | * |
||
258 | * @param $ids |
||
259 | * @param string|null $type |
||
260 | * @param bool $detaching |
||
261 | */ |
||
262 | View Code Duplication | protected function syncRoutineIds($ids, string $type = null, $detaching = true) |
|
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. ![]() |
|||
263 | { |
||
264 | $isUpdated = false; |
||
265 | |||
266 | // Get a list of routine_ids for all current routines |
||
267 | $current = $this->routines() |
||
268 | ->newPivotStatement() |
||
269 | ->where('routineable_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 ![]() |
|||
270 | ->where('routineable_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 ![]() |
|||
271 | ->when( |
||
272 | $type !== null, function ($query) use ($type) { |
||
273 | $routineModel = $this->routines()->getRelated(); |
||
274 | |||
275 | return $query->join( |
||
276 | $routineModel->getTable(), |
||
277 | 'routineables.routine_id', |
||
278 | '=', |
||
279 | $routineModel->getTable().'.'.$routineModel->getKeyName() |
||
280 | ) |
||
281 | ->where('routines.type', $type); |
||
282 | } |
||
283 | ) |
||
284 | ->pluck('routine_id') |
||
285 | ->all(); |
||
286 | |||
287 | // Compare to the list of ids given to find the routines to remove |
||
288 | $detach = array_diff($current, $ids); |
||
289 | if ($detaching && count($detach) > 0) { |
||
290 | $this->routines()->detach($detach); |
||
291 | $isUpdated = true; |
||
292 | } |
||
293 | |||
294 | // Attach any new ids |
||
295 | $attach = array_diff($ids, $current); |
||
296 | if (count($attach) > 0) { |
||
297 | collect($attach)->each( |
||
298 | function ($id) { |
||
299 | $this->routines()->attach($id, []); |
||
300 | } |
||
301 | ); |
||
302 | $isUpdated = true; |
||
303 | } |
||
304 | |||
305 | // Once we have finished attaching or detaching the records, we will see if we |
||
306 | // have done any attaching or detaching, and if we have we will touch these |
||
307 | // relationships if they are configured to touch on any database updates. |
||
308 | if ($isUpdated) { |
||
309 | $this->routines()->touchIfTouching(); |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 |
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.