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 Yajra\Auditable; |
||
4 | |||
5 | /** |
||
6 | * Class AuditableObserver |
||
7 | * |
||
8 | * @package Yajra\Auditable |
||
9 | */ |
||
10 | class AuditableTraitObserver |
||
11 | { |
||
12 | /** |
||
13 | * Model's creating event hook. |
||
14 | * |
||
15 | * @param \Yajra\Auditable\AuditableTrait $model |
||
16 | */ |
||
17 | public function creating($model) |
||
18 | { |
||
19 | if (! $model->created_by) { |
||
20 | $model->created_by = $this->getAuthenticatedUserId(); |
||
21 | } |
||
22 | |||
23 | if (! $model->updated_by) { |
||
24 | $model->updated_by = $this->getAuthenticatedUserId(); |
||
25 | } |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Get authenticated user id depending on model's auth guard. |
||
30 | * |
||
31 | * @return int |
||
32 | */ |
||
33 | protected function getAuthenticatedUserId() |
||
34 | { |
||
35 | return auth()->check() ? auth()->id() : 0; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Model's updating event hook. |
||
40 | * |
||
41 | * @param \Yajra\Auditable\AuditableTrait $model |
||
42 | */ |
||
43 | public function updating($model) |
||
44 | { |
||
45 | if (! $model->isDirty('updated_by')) { |
||
0 ignored issues
–
show
|
|||
46 | $model->updated_by = $this->getAuthenticatedUserId(); |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 |
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.