Skip to content

Commit

Permalink
Merge pull request #157 from Askedio/fix/replace-try-catch-with-is_ca…
Browse files Browse the repository at this point in the history
…llable

fix: replace try...catch(BadMethodCallException) blocks with a check
  • Loading branch information
gcphost authored Oct 25, 2024
2 parents e963e24 + 56cc1ac commit 56d16b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/Listeners/CascadeQueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Askedio\SoftCascade\QueryBuilderSoftCascade;
use Askedio\SoftCascade\Traits\ChecksCascading;
use BadMethodCallException;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\Events\QueryExecuted;
Expand Down Expand Up @@ -77,11 +76,10 @@ public function handle(): void
if (!is_null($event)) {
$builder = $event['builder'];

try {
// add `withTrashed()`, if the model has SoftDeletes
// otherwise, we can just skip it
if (method_exists($builder, 'withTrashed') || $builder->hasMacro('withTrashed')) {
$builder->withTrashed();
} catch (BadMethodCallException $e) {
// add `withTrashed()`, if the model has SoftDeletes
// otherwise, we can just skip it
}

$keyName = $builder->getModel()->getKeyName();
Expand Down
7 changes: 3 additions & 4 deletions src/SoftCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Askedio\SoftCascade\Exceptions\SoftCascadeNonExistentRelationActionException;
use Askedio\SoftCascade\Exceptions\SoftCascadeRestrictedException;
use Askedio\SoftCascade\Traits\ChecksCascading;
use BadMethodCallException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
Expand Down Expand Up @@ -286,10 +285,10 @@ protected function withTrashed(Builder $builder): Builder
}

// if the Model does not use SoftDeletes, withTrashed() will be unavailable.
try {
if (method_exists($builder, 'withTrashed') || $builder->hasMacro('withTrashed')) {
return $builder->withTrashed();
} catch (BadMethodCallException) {
return $builder;
}

return $builder;
}
}

0 comments on commit 56d16b1

Please # to comment.