HasStatuses   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

15 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 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
A hasEverHadStatus() 0 6 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 scopeCurrentStatus(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
            ->whereHas(
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
    /**
99
     * @param string|array $names
100
     *
101
     * @return void
102
     **/
103
    public function scopeOtherCurrentStatus(Builder $builder, ...$names)
104
    {
105
        $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...
106
        $builder
107
            ->whereHas(
108
                'statuses',
109
                function (Builder $query) use ($names) {
110
                    $query
111
                        ->whereNotIn('name', $names)
112
                        ->whereIn(
113
                            'id',
114
                            function (QueryBuilder $query) use ($names) {
115
                                $query
116
                                    ->select(DB::raw('max(id)'))
117
                                    ->from($this->getStatusTableName())
118
                                    ->where('model_type', $this->getStatusModelType())
119
                                    ->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...
120
                            }
121
                        );
122
                }
123
            )
124
            ->orWhereDoesntHave('statuses');
125
    }
126
127
    public function getStatusAttribute(): string
128
    {
129
        return (string) $this->latestStatus();
130
    }
131
132
    public function forceSetStatus(string $name, ?string $reason = null): self
133
    {
134
        $oldStatus = $this->latestStatus();
135
136
        $newStatus = $this->statuses()->create([
137
            'name'   => $name,
138
            'reason' => $reason,
139
        ]);
140
141
        event(new StatusUpdated($oldStatus, $newStatus, $this));
142
143
        return $this;
144
    }
145
146
    protected function getStatusTableName(): string
147
    {
148
        $modelClass = $this->getStatusModelClassName();
149
150
        return (new $modelClass)->getTable();
151
    }
152
153
    protected function getModelKeyColumnName(): string
154
    {
155
        return config('model-status.model_primary_key_attribute') ?? 'model_id';
156
    }
157
158
    protected function getStatusModelClassName(): string
159
    {
160
        return config('model-status.status_model');
161
    }
162
163
    protected function getStatusModelType(): string
164
    {
165
        return array_search(static::class, Relation::morphMap()) ?: static::class;
166
    }
167
}
168