diff --git a/src/HasStatuses.php b/src/HasStatuses.php index bd06beb..31ebf06 100644 --- a/src/HasStatuses.php +++ b/src/HasStatuses.php @@ -9,19 +9,20 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use Spatie\ModelStatus\Events\StatusUpdated; +use Spatie\ModelStatus\Exceptions\InvalidEnumClass; use Spatie\ModelStatus\Exceptions\InvalidEnumType; use Spatie\ModelStatus\Exceptions\InvalidStatus; -use Spatie\ModelStatus\Exceptions\InvalidEnumClass; trait HasStatuses { - public abstract static function getStatusEnumClass(): string; + abstract public static function getStatusEnumClass(): string; public function statuses(): MorphMany { - if (!self::enumIsStringBacked()) + if (! self::enumIsStringBacked()) { throw InvalidEnumType::create(self::getStatusEnumClass()); + } return $this->morphMany($this->getStatusModelClassName(), 'model', 'model_type', $this->getModelKeyColumnName()) ->latest('id'); @@ -34,7 +35,7 @@ public function status(): ?Status public function setStatus($statusEnum, ?string $reason = null): self { - if (!$this->isValidStatus($statusEnum, $reason)) { + if (! $this->isValidStatus($statusEnum, $reason)) { throw InvalidStatus::create($statusEnum->value); } @@ -148,6 +149,7 @@ function (QueryBuilder $query) use ($statusEnums) { private static function isInstanceOfEnum($statusEnum): bool { $statusEnumType = self::getStatusEnumClass(); + return $statusEnum instanceof $statusEnumType; } @@ -159,8 +161,9 @@ private static function enumIsStringBacked(): bool public function forceSetStatus($statusEnum, ?string $reason = null): self { - if (!self::isInstanceOfEnum($statusEnum)) + if (! self::isInstanceOfEnum($statusEnum)) { throw InvalidEnumClass::create(self::getStatusEnumClass()); + } $oldStatus = $this->latestStatus(); diff --git a/tests/HasStatusesTest.php b/tests/HasStatusesTest.php index eccf2e8..1050ff3 100644 --- a/tests/HasStatusesTest.php +++ b/tests/HasStatusesTest.php @@ -1,9 +1,9 @@ testModel->setStatus(TestEnum2::TestStatus, 'test'); -})->throws(InvalidEnumClass::class);; +})->throws(InvalidEnumClass::class); +; it('can find the last status by enum', function () {