Completed
Pull Request — master (#74)
by
unknown
04:09
created

HasStatuses   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 7
dl 0
loc 177
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A statuses() 0 5 1
A status() 0 4 1
A setStatus() 0 8 2
A isValidStatus() 0 4 1
A latestStatus() 0 11 4
A deleteStatus() 0 9 3
A hasEverHadStatus() 0 6 2
A scopeOrCurrentStatus() 0 22 2
A scopeCurrentStatus() 0 22 2
A scopeOtherCurrentStatus() 0 23 2
A getStatusAttribute() 0 4 1
A forceSetStatus() 0 13 1
A getStatusTableName() 0 6 1
A getModelKeyColumnName() 0 4 1
A getStatusModelClassName() 0 4 1
A getStatusModelType() 0 4 2
1
<?php
2
3
namespace Spatie\ModelStatus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
use Illuminate\Database\Query\Builder as QueryBuilder;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Facades\DB;
11
use Spatie\ModelStatus\Events\StatusUpdated;
12
use Spatie\ModelStatus\Exceptions\InvalidStatus;
13
14
trait HasStatuses
15
{
16
    public function statuses(): MorphMany
17
    {
18
        return $this->morphMany($this->getStatusModelClassName(), 'model', 'model_type', $this->getModelKeyColumnName())
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
19
            ->latest('id');
20
    }
21
22
    public function status(): ?Status
23
    {
24
        return $this->latestStatus();
25
    }
26
27
    public function setStatus(string $name, ?string $reason = null): self
28
    {
29
        if (! $this->isValidStatus($name, $reason)) {
30
            throw InvalidStatus::create($name);
31
        }
32
33
        return $this->forceSetStatus($name, $reason);
34
    }
35
36
    public function isValidStatus(string $name, ?string $reason = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $reason is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        return true;
39
    }
40
41
    /**
42
     * @param string|array $names
43
     *
44
     * @return null|Status
45
     */
46
    public function latestStatus(...$names): ?Status
47
    {
48
        $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses();
0 ignored issues
show
Bug introduced by
The property statuses does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like relationLoaded() 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...
49
50
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
0 ignored issues
show
Documentation introduced by
$names is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
        if (count($names) < 1) {
52
            return $statuses->first();
53
        }
54
55
        return $statuses->whereIn('name', $names)->first();
56
    }
57
58
    public function hasEverHadStatus($name): bool
59
    {
60
        $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses();
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
61
62
        return $statuses->where('name', $name)->count() > 0;
63
    }
64
65
    public function deleteStatus(...$names)
66
    {
67
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
0 ignored issues
show
Documentation introduced by
$names is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
        if (count($names) < 1) {
69
            return $this;
70
        }
71
72
        $this->statuses()->whereIn('name', $names)->delete();
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Illuminate\Database\Eloquent\Relations\MorphMany. Did you maybe mean whereInMethod()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
73
    }
74
75
    public function scopeOrCurrentStatus(Builder $builder, ...$names)
76
    {
77
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
0 ignored issues
show
Documentation introduced by
$names is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        $builder
79
            ->orWhereHas(
80
                'statuses',
81
                function (Builder $query) use ($names) {
82
                    $query
83
                        ->whereIn('name', $names)
84
                        ->whereIn(
85
                            'id',
86
                            function (QueryBuilder $query) {
87
                                $query
88
                                    ->select(DB::raw('max(id)'))
89
                                    ->from($this->getStatusTableName())
90
                                    ->where('model_type', $this->getStatusModelType())
91
                                    ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName());
0 ignored issues
show
Bug introduced by
It seems like getQualifiedKeyName() 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...
92
                            }
93
                        );
94
                }
95
            );
96
    }
97
98
    public function scopeCurrentStatus(Builder $builder, ...$names)
99
    {
100
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
0 ignored issues
show
Documentation introduced by
$names is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
        $builder
102
            ->whereHas(
103
                'statuses',
104
                function (Builder $query) use ($names) {
105
                    $query
106
                        ->whereIn('name', $names)
107
                        ->whereIn(
108
                            'id',
109
                            function (QueryBuilder $query) {
110
                                $query
111
                                    ->select(DB::raw('max(id)'))
112
                                    ->from($this->getStatusTableName())
113
                                    ->where('model_type', $this->getStatusModelType())
114
                                    ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName());
0 ignored issues
show
Bug introduced by
It seems like getQualifiedKeyName() 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...
115
                            }
116
                        );
117
                }
118
            );
119
    }
120
121
    /**
122
     * @param string|array $names
123
     *
124
     * @return void
125
     **/
126
    public function scopeOtherCurrentStatus(Builder $builder, ...$names)
127
    {
128
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
0 ignored issues
show
Documentation introduced by
$names is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
        $builder
130
            ->whereHas(
131
                'statuses',
132
                function (Builder $query) use ($names) {
133
                    $query
134
                        ->whereNotIn('name', $names)
135
                        ->whereIn(
136
                            'id',
137
                            function (QueryBuilder $query) use ($names) {
138
                                $query
139
                                    ->select(DB::raw('max(id)'))
140
                                    ->from($this->getStatusTableName())
141
                                    ->where('model_type', $this->getStatusModelType())
142
                                    ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName());
0 ignored issues
show
Bug introduced by
It seems like getQualifiedKeyName() 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...
143
                            }
144
                        );
145
                }
146
            )
147
            ->orWhereDoesntHave('statuses');
148
    }
149
150
    public function getStatusAttribute(): string
151
    {
152
        return (string) $this->latestStatus();
153
    }
154
155
    public function forceSetStatus(string $name, ?string $reason = null): self
156
    {
157
        $oldStatus = $this->latestStatus();
158
159
        $newStatus = $this->statuses()->create([
160
            'name'   => $name,
161
            'reason' => $reason,
162
        ]);
163
164
        event(new StatusUpdated($oldStatus, $newStatus, $this));
165
166
        return $this;
167
    }
168
169
    protected function getStatusTableName(): string
170
    {
171
        $modelClass = $this->getStatusModelClassName();
172
173
        return (new $modelClass)->getTable();
174
    }
175
176
    protected function getModelKeyColumnName(): string
177
    {
178
        return config('model-status.model_primary_key_attribute') ?? 'model_id';
179
    }
180
181
    protected function getStatusModelClassName(): string
182
    {
183
        return config('model-status.status_model');
184
    }
185
186
    protected function getStatusModelType(): string
187
    {
188
        return array_search(static::class, Relation::morphMap()) ?: static::class;
189
    }
190
}
191