Completed
Push — develop ( cbad9d...de1065 )
by Abdelrahman
13:09
created

Auditable::creating()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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\Relations\BelongsTo;
9
10
trait Auditable
11
{
12
    /**
13
     * Register a creating model event with the dispatcher.
14
     *
15
     * @param  \Closure|string $callback
16
     *
17
     * @return void
18
     */
19
    abstract public static function creating($callback);
20
21
    /**
22
     * Register an updating model event with the dispatcher.
23
     *
24
     * @param  \Closure|string $callback
25
     *
26
     * @return void
27
     */
28
    abstract public static function updating($callback);
29
30
    /**
31
     * Define an inverse one-to-one or many relationship.
32
     *
33
     * @param  string $related
34
     * @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...
35
     * @param  string $ownerKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $ownerKey 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 $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...
37
     *
38
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
39
     */
40
    abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = 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 || $model->created_by = auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
51
            $model->updated_by || $model->updated_by = auth()->id();
52
        });
53
54
        static::updating(function (Model $model) {
55
            $model->isDirty('updated_by') || $model->updated_by = auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
56
        });
57
    }
58
59
    /**
60
     * Get user model who created the record.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    public function creator(): BelongsTo
65
    {
66
        $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model');
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...
67
68
        return $this->belongsTo($userModel, 'created_by');
69
    }
70
71
    /**
72
     * Get user model who updated the record.
73
     *
74
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
75
     */
76
    public function updater(): BelongsTo
77
    {
78
        $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model');
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...
79
80
        return $this->belongsTo($userModel, 'updated_by');
81
    }
82
}
83