Bug 32528: Make safe_to_delete exit earlier on error conditions
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 26 Dec 2022 18:43:16 +0000 (15:43 -0300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Wed, 11 Jan 2023 23:47:09 +0000 (20:47 -0300)
This patch makes the `Koha::Item->safe_to_delete` method return right
after one of the checked conditions is met. This way we avoid further DB
queries and processing.

No behavior change is expected

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Item.t
=> SUCCESS: Tests pass!
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests STILL pass!
4. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Bug 32528: Change return logic

Back to 1 return statement
Prevent the Koha::Result::Boolean instanciation duplication

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/Item.pm

index 1174519..dae9df8 100644 (file)
@@ -293,20 +293,20 @@ sub safe_to_delete {
 
     $error = "book_on_loan" if $self->checkout;
 
-    $error = "not_same_branch"
+    $error //= "not_same_branch"
       if defined C4::Context->userenv
-      and !C4::Context->IsSuperLibrarian()
-      and C4::Context->preference("IndependentBranches")
-      and ( C4::Context->userenv->{branch} ne $self->homebranch );
+      && !C4::Context->IsSuperLibrarian()
+      && C4::Context->preference("IndependentBranches")
+      && ( C4::Context->userenv->{branch} ne $self->homebranch );
 
     # check it doesn't have a waiting reserve
-    $error = "book_reserved"
+    $error //= "book_reserved"
       if $self->holds->filter_by_found->count;
 
-    $error = "linked_analytics"
+    $error //= "linked_analytics"
       if C4::Items::GetAnalyticsCount( $self->itemnumber ) > 0;
 
-    $error = "last_item_for_hold"
+    $error //= "last_item_for_hold"
       if $self->biblio->items->count == 1
       && $self->biblio->holds->search(
           {