Completed
Push — develop ( d114fd...73c793 )
by Abdelrahman
16:17
created

Auditable::bootAuditable()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 9
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
11
trait Auditable
12
{
13
    /**
14
     * Register a creating model event with the dispatcher.
15
     *
16
     * @param \Closure|string $callback
17
     *
18
     * @return void
19
     */
20
    abstract public static function creating($callback);
21
22
    /**
23
     * Register an updating model event with the dispatcher.
24
     *
25
     * @param \Closure|string $callback
26
     *
27
     * @return void
28
     */
29
    abstract public static function updating($callback);
30
31
    /**
32
     * Define a polymorphic, inverse one-to-one or many relationship.
33
     *
34
     * @param  string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name 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...
35
     * @param  string $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type 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...
36
     * @param  string $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id 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...
37
     *
38
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
39
     */
40
    abstract public function morphTo($name = null, $type = null, $id = null);
41
42
    /**
43
     * Boot the Auditable trait for the model.
44
     *
45
     * @return void
46
     */
47
    public static function bootAuditable()
48
    {
49
        static::creating(function (Model $model) {
50
            $model->created_by_id || $model->created_by_id = optional(auth()->guard(request('guard'))->user())->getKey();
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
51
            $model->created_by_type || $model->created_by_type = optional(auth()->guard(request('guard'))->user())->getMorphClass();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
52
53
            $model->updated_by_id || $model->updated_by_id = optional(auth()->guard(request('guard'))->user())->getKey();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
54
            $model->updated_by_type || $model->updated_by_type = optional(auth()->guard(request('guard'))->user())->getMorphClass();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
55
        });
56
57
        static::updating(function (Model $model) {
58
            $model->isDirty('updated_by_id') || $model->updated_by_id = optional(auth()->guard(request('guard'))->user())->getKey();
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
59
            $model->isDirty('updated_by_type') || $model->updated_by_type = optional(auth()->guard(request('guard'))->user())->getMorphClass();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
        });
61
    }
62
63
    /**
64
     * Get the owning creator.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
67
     */
68
    public function creator(): MorphTo
69
    {
70
        return $this->morphTo();
71
    }
72
73
    /**
74
     * Get the owning updater.
75
     *
76
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
77
     */
78
    public function updater(): MorphTo
79
    {
80
        return $this->morphTo();
81
    }
82
83
    /**
84
     * Get audits of the given creator.
85
     *
86
     * @param \Illuminate\Database\Eloquent\Builder $builder
87
     * @param \Illuminate\Database\Eloquent\Model   $user
88
     *
89
     * @return \Illuminate\Database\Eloquent\Builder
90
     */
91
    public function scopeOfCreator(Builder $builder, Model $user): Builder
92
    {
93
        return $builder->where('created_by_type', $user->getMorphClass())->where('created_by_id', $user->getKey());
94
    }
95
96
    /**
97
     * Get audits of the given updater.
98
     *
99
     * @param \Illuminate\Database\Eloquent\Builder $builder
100
     * @param \Illuminate\Database\Eloquent\Model   $user
101
     *
102
     * @return \Illuminate\Database\Eloquent\Builder
103
     */
104
    public function scopeOfUpdater(Builder $builder, Model $user): Builder
105
    {
106
        return $builder->where('updated_by_type', $user->getMorphClass())->where('updated_by_id', $user->getKey());
107
    }
108
}
109