From 3fcf6b9c852489757fe0b9efddb81aaa282f5f1f Mon Sep 17 00:00:00 2001 From: yazeed Date: Thu, 23 May 2024 21:09:15 +0300 Subject: [PATCH 1/3] feat: Add method to check if a model has never had a specific status - Added `hasNeverHadStatus($name): bool` method to determine if the model has never had a status with the given name. - Included PHPDoc comments for both `hasEverHadStatus($name): bool` and `hasNeverHadStatus($name): bool` methods. - Added tests for `hasNeverHadStatus` method: - Test to return `false` if the specific status is found. - Test to return `true` if the specific status is not found. This contribution introduces the inverse of the `hasEverHadStatus` function and improves documentation. --- src/HasStatuses.php | 29 +++++++++++++++++++++++++++++ tests/HasStatusesTest.php | 10 ++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/HasStatuses.php b/src/HasStatuses.php index bc48659..2549a82 100644 --- a/src/HasStatuses.php +++ b/src/HasStatuses.php @@ -55,6 +55,17 @@ public function latestStatus(...$names): ?Status return $statuses->whereIn('name', $names)->first(); } + /** + * Check if the model has ever had a status with the given name. + * + * This method determines whether the current model instance has ever had a status + * with the specified name. + * + * @param string $name The name of the status to check for. + * + * @return bool Returns true if the model has ever had the status with the given name, + * otherwise returns false. + */ public function hasEverHadStatus($name): bool { $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses(); @@ -62,6 +73,24 @@ public function hasEverHadStatus($name): bool return $statuses->where('name', $name)->count() > 0; } + /** + * Check if the model has never had a status with the given name. + * + * This method determines whether the current model instance has never had a status + * with the specified name. + * + * @param string $name The name of the status to check for. + * + * @return bool Returns true if the model has never had the status with the given name, + * otherwise returns false. + */ + public function hasNeverHadStatus($name): bool + { + $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses(); + + return $statuses->where('name', $name)->count() === 0; + } + public function deleteStatus(...$names) { $names = is_array($names) ? Arr::flatten($names) : func_get_args(); diff --git a/tests/HasStatusesTest.php b/tests/HasStatusesTest.php index a127789..075630a 100644 --- a/tests/HasStatusesTest.php +++ b/tests/HasStatusesTest.php @@ -109,6 +109,16 @@ ->expect(fn () => $this->testModel->hasEverHadStatus('status 2')) ->toBeFalse(); +it('will return `false` if specific status is found') + ->tap(fn () => $this->testModel->setStatus('status 1')) + ->expect(fn () => $this->testModel->hasNeverHadStatus('status 1')) + ->toBeFalse(); + +it('will return `true` if specific status is not found') + ->tap(fn () => $this->testModel->setStatus('status 1')) + ->expect(fn () => $this->testModel->hasNeverHadStatus('status 2')) + ->toBeTrue(); + it('can delete a specific status', function () { $this->testModel->setStatus('status to delete'); From 083766bb693f3ae4dd613b6aeaa2e776687544e7 Mon Sep 17 00:00:00 2001 From: yazeed Date: Fri, 24 May 2024 12:32:58 +0300 Subject: [PATCH 2/3] refactor: Simplify hasNeverHadStatus method by reusing hasEverHadStatus - Updated `hasNeverHadStatus($name): bool` method to return the negation of `hasEverHadStatus($name)`, simplifying the logic. This change was made following a review suggestion from @freekmurze to avoid redundant logic. --- src/HasStatuses.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/HasStatuses.php b/src/HasStatuses.php index 2549a82..a75953e 100644 --- a/src/HasStatuses.php +++ b/src/HasStatuses.php @@ -77,7 +77,7 @@ public function hasEverHadStatus($name): bool * Check if the model has never had a status with the given name. * * This method determines whether the current model instance has never had a status - * with the specified name. + * with the specified name by negating the result of hasEverHadStatus. * * @param string $name The name of the status to check for. * @@ -86,9 +86,7 @@ public function hasEverHadStatus($name): bool */ public function hasNeverHadStatus($name): bool { - $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses(); - - return $statuses->where('name', $name)->count() === 0; + return !$this->hasEverHadStatus($name); } public function deleteStatus(...$names) From a1f8f01c35acc24081349b657699bb93d82d21d3 Mon Sep 17 00:00:00 2001 From: yazeed Date: Fri, 24 May 2024 18:25:49 +0300 Subject: [PATCH 3/3] - Updated README.md file to include documentation for `hasNeverHadStatus` method. --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 9818cb3..1ee2dc5 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,14 @@ You can check if a specific status has been set on the model at any time by usin $model->hasEverHadStatus('status 1'); ``` +### Check if status has never been assigned + +You can check if a specific status has never been set on the model at any time by using the `hasNeverHadStatus` method: + +```php +$model->hasNeverHadStatus('status 1'); +``` + ### Delete status from model You can delete any given status that has been set on the model at any time by using the `deleteStatus` method: