Issues (80)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Traits/HasRoutine.php (21 issues)

Upgrade to new PHP Analysis Engine

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
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...
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 Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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.

Loading history...
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 Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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;
Loading history...
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.

Loading history...
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.

Loading history...
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.

Loading history...
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.

Loading history...
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;
Loading history...
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.

Loading history...
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);
Loading history...
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.

Loading history...
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.

Loading history...
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.

Loading history...
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 dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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.

Loading history...
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 dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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.

Loading history...
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 Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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 Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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