1 | <?php |
||
11 | trait HasStatuses |
||
12 | { |
||
13 | public function statuses(): MorphMany |
||
17 | |||
18 | public function status(): ?Status |
||
22 | |||
23 | public function setStatus(string $name, string $reason = ''): self |
||
36 | |||
37 | public function isValidStatus(string $name, string $reason = ''): bool |
||
41 | |||
42 | /** |
||
43 | * @param string|array $names |
||
44 | * |
||
45 | * @return null|Status |
||
46 | */ |
||
47 | public function latestStatus(...$names): ?Status |
||
57 | |||
58 | public function scopeCurrentStatus(Builder $builder, string $name) |
||
73 | |||
74 | /** |
||
75 | * @param string|array $names |
||
76 | * |
||
77 | * @return void |
||
78 | **/ |
||
79 | public function scopeOtherCurrentStatus(Builder $builder, ...$names) |
||
80 | { |
||
81 | $names = is_array($names) ? array_flatten($names) : func_get_args(); |
||
82 | $builder |
||
83 | ->whereHas('statuses', function (Builder $query) use ($names) { |
||
84 | $query |
||
85 | ->whereNotIn('name', $names) |
||
86 | ->whereIn('id', function (QueryBuilder $query) use ($names) { |
||
87 | $query |
||
88 | ->select(DB::raw('max(id)')) |
||
89 | ->from($this->getStatusTableName()) |
||
90 | ->where('model_type', static::class) |
||
91 | ->groupBy('model_id'); |
||
92 | }); |
||
93 | }) |
||
94 | ->orWhereDoesntHave('statuses'); |
||
95 | } |
||
96 | |||
97 | public function getStatusAttribute(): string |
||
101 | |||
102 | protected function getStatusTableName(): string |
||
108 | |||
109 | protected function getStatusModelClassName(): string |
||
113 | } |
||
114 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.