Completed
Push — master ( aead87...7136fd )
by Arjay
11:27
created

src/AuditableTrait.php (2 issues)

undocumented requirements in traits.

Bug Minor

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