Completed
Push — master ( 9f9877...f2c61d )
by Freek
06:20
created

HasStatuses::status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ModelStatus;
4
5
use Spatie\ModelStatus\Models\Status;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Spatie\ModelStatus\Exceptions\InvalidStatus;
8
9
trait HasStatuses
10
{
11
    public function statuses(): MorphMany
12
    {
13
        return $this->morphMany(Status::class, 'model');
0 ignored issues
show
Bug introduced by TVke
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...
14
    }
15
16
    public function status(): ?Status
17
    {
18
        return $this->latestStatus();
19
    }
20
21
    public function setStatus(string $name, string $reason = ''): self
22
    {
23
        if (! $this->isValidStatus($name, $reason)) {
24
            throw InvalidStatus::create($name, $reason);
0 ignored issues
show
Unused Code introduced by Freek
The call to InvalidStatus::create() has too many arguments starting with $reason.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
25
        }
26
27
        $attributes = compact('name', 'reason');
28
29
        $this->statuses()->create($attributes);
30
31
        return $this;
32
    }
33
34
    public function isValidStatus(string $name, string $reason = ''): bool
0 ignored issues
show
Unused Code introduced by Freek
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 Freek
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...
35
    {
36
        return true;
37
    }
38
39
    public function latestStatus(string ...$name): ?Status
40
    {
41
        if (empty($name)) {
42
            return $this->statuses()->latest()->orderByDesc('id')->first();
43
        }
44
45
        return $this->statuses()->whereIn('name', $name)->latest()->orderByDesc('id')->first();
46
    }
47
}
48