Passed
Push — master ( a3e60c...72c09c )
by Richan
12:04
created

BlameableTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 139
rs 10
c 1
b 0
f 0
wmc 9
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A bootBlameableTrait() 0 4 1
A buildBlameableScope() 0 8 2
A creator() 0 7 1
A updater() 0 7 1
A scopeCreatedBy() 0 4 1
A scopeUpdatedBy() 0 4 1
A silentUpdate() 0 9 2
belongsTo() 0 1 ?
getDirty() 0 1 ?
getKeyName() 0 1 ?
getKey() 0 1 ?
newQueryWithoutScopes() 0 1 ?
1
<?php
2
3
namespace RichanFongdasen\EloquentBlameable;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait BlameableTrait
9
{
10
    /**
11
     * Boot the Blameable service by attaching
12
     * a new observer into the current model object.
13
     *
14
     * @return void
15
     */
16
    public static function bootBlameableTrait()
17
    {
18
        static::observe(app(BlameableObserver::class));
19
    }
20
21
    /**
22
     * Build blameable query scope.
23
     *
24
     * @param \Illuminate\Database\Eloquent\Builder $query
25
     * @param mixed                                 $userId
26
     * @param string                                $key
27
     *
28
     * @return \Illuminate\Database\Eloquent\Builder
29
     */
30
    private function buildBlameableScope(Builder $query, $userId, $key)
31
    {
32
        if ($userId instanceof Model) {
33
            $userId = $userId->getKey();
34
        }
35
36
        return $query->where(app(BlameableService::class)->getConfiguration($this, $key), $userId);
37
    }
38
39
    /**
40
     * Get the user who created the record.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Model
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Database\Eloquent\Relations\BelongsTo?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
43
     */
44
    public function creator()
45
    {
46
        return $this->belongsTo(
47
            app(BlameableService::class)->getConfiguration($this, 'user'),
48
            app(BlameableService::class)->getConfiguration($this, 'createdBy')
49
        );
50
    }
51
52
    /**
53
     * Get the user who updated the record for the last time.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Model
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Database\Eloquent\Relations\BelongsTo?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
     */
57
    public function updater()
58
    {
59
        return $this->belongsTo(
60
            app(BlameableService::class)->getConfiguration($this, 'user'),
61
            app(BlameableService::class)->getConfiguration($this, 'updatedBy')
62
        );
63
    }
64
65
    /**
66
     * createdBy Query Scope.
67
     *
68
     * @param \Illuminate\Database\Eloquent\Builder $query
69
     * @param mixed                                 $userId
70
     *
71
     * @return \Illuminate\Database\Eloquent\Builder
72
     */
73
    public function scopeCreatedBy(Builder $query, $userId)
74
    {
75
        return $this->buildBlameableScope($query, $userId, 'createdBy');
76
    }
77
78
    /**
79
     * updatedBy Query Scope.
80
     *
81
     * @param \Illuminate\Database\Eloquent\Builder $query
82
     * @param mixed                                 $userId
83
     *
84
     * @return \Illuminate\Database\Eloquent\Builder
85
     */
86
    public function scopeUpdatedBy(Builder $query, $userId)
87
    {
88
        return $this->buildBlameableScope($query, $userId, 'updatedBy');
89
    }
90
91
    /**
92
     * Silently update the model without firing any
93
     * events.
94
     *
95
     * @return void
96
     */
97
    public function silentUpdate()
98
    {
99
        $query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in RichanFongdasen\EloquentBlameable\BlameableTrait.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
        $dirty = $this->getDirty();
101
102
        if (!empty($dirty)) {
103
            $query->update($dirty);
104
        }
105
    }
106
107
    /**
108
     * Define an inverse one-to-one or many relationship.
109
     *
110
     * @param string $related
111
     * @param string $foreignKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $foreignKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
112
     * @param string $otherKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $otherKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
113
     * @param string $relation
0 ignored issues
show
Documentation introduced by
Should the type for parameter $relation not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
114
     *
115
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
116
     */
117
    abstract public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null);
118
119
    /**
120
     * Get the attributes that have been changed since last sync.
121
     *
122
     * @return array
123
     */
124
    abstract public function getDirty();
125
126
    /**
127
     * Get the primary key for the model.
128
     *
129
     * @return string
130
     */
131
    abstract public function getKeyName();
132
133
    /**
134
     * Get the value of the model's primary key.
135
     *
136
     * @return mixed
137
     */
138
    abstract public function getKey();
139
140
    /**
141
     * Get a new query builder that doesn't have any global scopes.
142
     *
143
     * @return \Illuminate\Database\Eloquent\Builder|static
144
     */
145
    abstract public function newQueryWithoutScopes();
146
}
147