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 | use Illuminate\Database\Eloquent\Builder; |
||
6 | |||
7 | trait AuditableTrait |
||
8 | { |
||
9 | /** |
||
10 | * Boot the audit trait for a model. |
||
11 | * |
||
12 | * @return void |
||
13 | */ |
||
14 | public static function bootAuditableTrait() |
||
15 | { |
||
16 | static::observe(new AuditableTraitObserver); |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * Get user model who created the record. |
||
21 | * |
||
22 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
23 | */ |
||
24 | public function creator() |
||
25 | { |
||
26 | return $this->belongsTo($this->getUserInstance(), $this->getCreatedByColumn()); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Get column name for created by. |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getCreatedByColumn() |
||
35 | { |
||
36 | return defined('static::CREATED_BY') ? static::CREATED_BY : 'created_by'; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Get user model who updated the record. |
||
41 | * |
||
42 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
43 | */ |
||
44 | public function updater() |
||
45 | { |
||
46 | return $this->belongsTo($this->getUserInstance(), $this->getUpdatedByColumn()); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get column name for updated by. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getUpdatedByColumn() |
||
55 | { |
||
56 | return defined('static::UPDATED_BY') ? static::UPDATED_BY : 'updated_by'; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get user model who deleted the record. |
||
61 | * |
||
62 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
63 | */ |
||
64 | public function deleter() |
||
65 | { |
||
66 | return $this->belongsTo($this->getUserInstance(), $this->getDeletedByColumn()); |
||
0 ignored issues
–
show
|
|||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get column name for deleted by. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getDeletedByColumn() |
||
75 | { |
||
76 | return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by'; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get created by user full name. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getCreatedByNameAttribute() |
||
85 | { |
||
86 | if ($this->{$this->getCreatedByColumn()}) { |
||
87 | return $this->creator->first_name . ' ' . $this->creator->last_name; |
||
88 | } |
||
89 | |||
90 | return ''; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get Laravel's user class instance. |
||
95 | * |
||
96 | * @return \Illuminate\Database\Eloquent\Model |
||
97 | */ |
||
98 | public function getUserInstance() |
||
99 | { |
||
100 | $class = config('auth.providers.users.model', 'App\User'); |
||
101 | |||
102 | return new $class; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get updated by user full name. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getUpdatedByNameAttribute() |
||
111 | { |
||
112 | if ($this->{$this->getUpdatedByColumn()}) { |
||
113 | return $this->updater->first_name . ' ' . $this->updater->last_name; |
||
114 | } |
||
115 | |||
116 | return ''; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get deleted by user full name. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getDeletedByNameAttribute() |
||
125 | { |
||
126 | if ($this->{$this->getDeletedByColumn()}) { |
||
127 | return $this->deleter->first_name . ' ' . $this->deleter->last_name; |
||
0 ignored issues
–
show
The property
deleter 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...
|
|||
128 | } |
||
129 | |||
130 | return ''; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Query scope to limit results to own records. |
||
135 | * |
||
136 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
137 | * @return \Illuminate\Database\Eloquent\Builder |
||
138 | */ |
||
139 | public function scopeOwned(Builder $query) |
||
140 | { |
||
141 | return $query->where($this->getQualifiedUserIdColumn(), auth()->id()); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get qualified column name for user id. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getQualifiedUserIdColumn() |
||
150 | { |
||
151 | return $this->getTable() . '.' . $this->getUserInstance()->getKey(); |
||
152 | } |
||
153 | } |
||
154 |
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.