Completed
Push — master ( 33f5e0...a59007 )
by Avtandil
03:17
created

UserIdentities::stopUserIdentities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\Eloquent;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Database\Eloquent\Relations\BelongsTo;
16
17
/**
18
 * @mixin \Illuminate\Database\Eloquent\Model
19
 * @property \Illuminate\Contracts\Auth\Authenticatable $creator
20
 * @property \Illuminate\Contracts\Auth\Authenticatable $editor
21
 * @property \Illuminate\Contracts\Auth\Authenticatable $destroyer
22
 */
23
trait UserIdentities
24
{
25
    protected $userIdentities = true;
26
    protected $columnCreatedBy = 'created_by';
27
    protected $columnUpdatedBy = 'updated_by';
28
    protected $columnDeletedBy = 'deleted_by';
29
30
    protected static function bootUserIdentities()
31
    {
32
        // Creating
33
        static::creating(function (Model $model) {
34
            if (! $model->usesUserIdentities()) {
35
                return;
36
            }
37
38
            if (is_null($model->{$model->columnCreatedBy})) {
39
                $model->{$model->columnCreatedBy} = auth()->id();
1 ignored issue
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...
40
            }
41
42
            if (is_null($model->{$model->columnUpdatedBy})) {
43
                $model->{$model->columnUpdatedBy} = auth()->id();
44
            }
45
        });
46
47
        // Updating
48
        static::updating(function (Model $model) {
49
            if (! $model->usesUserIdentities()) {
50
                return;
51
            }
52
53
            $model->{$model->columnUpdatedBy} = auth()->id();
1 ignored issue
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...
54
        });
55
56
        // Delete/Restore
57
        if (static::usingSoftDeletes()) {
58
59
            static::deleting(function (Model $model) {
60
                if (! $model->usesUserIdentities()) {
61
                    return;
62
                }
63
64
                if (is_null($model->{$model->columnDeletedBy})) {
65
                    $model->{$model->columnDeletedBy} = auth()->id();
1 ignored issue
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...
66
                }
67
68
                $model->save();
69
            });
70
71
            static::restoring(function (Model $model) {
72
                if (! $model->usesUserIdentities()) {
73
                    return;
74
                }
75
76
                $model->{$model->columnDeletedBy} = null;
77
            });
78
        }
79
80
    }
81
82
    public static function usingSoftDeletes(): bool
83
    {
84
        static $usingSoftDeletes;
85
86
        if (is_null($usingSoftDeletes)) {
87
            return $usingSoftDeletes = in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_called_class()));
88
        }
89
90
        return $usingSoftDeletes;
91
    }
92
93
    public function creator(): BelongsTo
94
    {
95
        return $this->belongsTo($this->getUserClass(), $this->columnCreatedBy);
0 ignored issues
show
Bug introduced by
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...
96
    }
97
98
    public function editor(): BelongsTo
99
    {
100
        return $this->belongsTo($this->getUserClass(), $this->columnUpdatedBy);
0 ignored issues
show
Bug introduced by
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...
101
    }
102
103
    public function destroyer(): BelongsTo
104
    {
105
        return $this->belongsTo($this->getUserClass(), $this->columnDeletedBy);
0 ignored issues
show
Bug introduced by
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...
106
    }
107
108
    public function usesUserIdentities(): bool
109
    {
110
        return $this->userIdentities;
111
    }
112
113
    public function stopUserIdentities(): self
114
    {
115
        $this->userIdentities = false;
116
117
        return $this;
118
    }
119
120
    public function startUserIdentities(): self
121
    {
122
        $this->userIdentities = true;
123
124
        return $this;
125
    }
126
127
    protected function getUserClass(): string
128
    {
129
        if (get_class(auth()) === \Illuminate\Auth\Guard::class) {
130
            return auth()->getProvider()->getModel();
131
        }
132
133
        return auth()->guard()->getProvider()->getModel();
1 ignored issue
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...
134
    }
135
}
136