Skip to content

Commit c0d0285

Browse files
authored
Merge pull request #159 from Askedio/fix/cleanup
fix: cleanup
2 parents 56d16b1 + 6b0be8b commit c0d0285

12 files changed

+159
-79
lines changed

src/Contracts/SoftCascadeable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ interface SoftCascadeable
77
/**
88
* Cascade over Eloquent items.
99
*
10-
* @param Illuminate\Database\Eloquent\Model $models
11-
* @param string $direction update|delete|restore
12-
* @param array $directionData
10+
* @param \Illuminate\Database\Eloquent\Model $models
11+
* @param 'update'|'delete'|'restore' $direction
12+
* @param array $directionData
1313
*
1414
* @return void
1515
*/

src/Exceptions/SoftCascadeRestrictedException.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,38 @@
55
use Illuminate\Support\Arr;
66
use RuntimeException;
77

8+
/**
9+
* @template TModelString of class-string<\Illuminate\Database\Eloquent\Model>
10+
*/
811
class SoftCascadeRestrictedException extends RuntimeException
912
{
1013
/**
1114
* Name of the affected Eloquent model.
1215
*
13-
* @var string
16+
* @var TModelString
1417
*/
1518
protected $model;
1619

1720
/**
1821
* The affected model foreignKey.
1922
*
20-
* @var int|array
23+
* @var string
2124
*/
2225
protected $foreignKey;
2326

2427
/**
2528
* The affected model foreignKeyIds.
2629
*
27-
* @var int|array
30+
* @var int[]
2831
*/
2932
protected $foreignKeyIds;
3033

3134
/**
3235
* Set the affected Eloquent model and instance foreignKeyIds.
3336
*
34-
* @param string $model
35-
* @param string $foreignKey
36-
* @param int|array $foreignKeyIds
37+
* @param TModelString $model
38+
* @param string $foreignKey
39+
* @param int | int[] $foreignKeyIds
3740
*
3841
* @return $this
3942
*/
@@ -43,25 +46,26 @@ public function setModel($model, $foreignKey, $foreignKeyIds)
4346
$this->foreignKey = $foreignKey;
4447
$this->foreignKeyIds = Arr::wrap($foreignKeyIds);
4548

46-
$this->message = "Integrity constraint violation [{$model}] where $foreignKey in (".implode(', ', $foreignKeyIds).')';
49+
$this->message =
50+
"Integrity constraint violation [{$model}] where $foreignKey in (".implode(', ', $foreignKeyIds).')';
4751

4852
return $this;
4953
}
5054

5155
/**
5256
* Get the affected Eloquent model.
5357
*
54-
* @return string
58+
* @return TModelString
5559
*/
5660
public function getModel()
5761
{
5862
return $this->model;
5963
}
6064

6165
/**
62-
* Get the affected Eloquent model foreignKeyIds.
66+
* Get the affected Eloquent model foreignKey.
6367
*
64-
* @return int|array
68+
* @return string
6569
*/
6670
public function getForeignKey()
6771
{
@@ -71,7 +75,7 @@ public function getForeignKey()
7175
/**
7276
* Get the affected Eloquent model foreignKeyIds.
7377
*
74-
* @return int|array
78+
* @return int[]
7579
*/
7680
public function getForeignKeyIds()
7781
{

src/Listeners/CascadeDeleteListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ class CascadeDeleteListener
99
/**
1010
* Handel the event for eloquent delete.
1111
*
12-
* @param $event
13-
* @param $model
12+
* @param string $event
13+
* @param \Illuminate\Database\Eloquent\Model $model
1414
*
1515
* @return void
1616
*
1717
* @SuppressWarnings(PHPMD.UnusedFormalParameter("event"))
18+
*
19+
* @noinspection PhpUnusedParameterInspection
1820
*/
1921
public function handle($event, $model)
2022
{

src/Listeners/CascadeQueryListener.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,39 @@
55
use Askedio\SoftCascade\QueryBuilderSoftCascade;
66
use Askedio\SoftCascade\Traits\ChecksCascading;
77
use Illuminate\Database\Connection;
8+
use Illuminate\Database\Eloquent\Builder;
89
use Illuminate\Database\Eloquent\SoftDeletingScope;
910
use Illuminate\Database\Events\QueryExecuted;
1011
use Illuminate\Support\Str;
1112

1213
class CascadeQueryListener
1314
{
1415
use ChecksCascading;
15-
const EVENT = QueryExecuted::class;
16+
17+
public const EVENT = QueryExecuted::class;
1618

1719
/**
1820
* Check in the backtrace, if models where updated by Builder::delete() or Builder::update().
21+
*
22+
* @return array{
23+
* builder: object,
24+
* direction: string,
25+
* directionData: array,
26+
* } | null
1927
*/
2028
private function checkForCascadeEvent(): ?array
2129
{
30+
/**
31+
* @var \Illuminate\Support\Collection<array-key, array{
32+
* function: string,
33+
* line: int,
34+
* file: string,
35+
* class?: string,
36+
* object?: object,
37+
* type: string,
38+
* args?: array,
39+
* }> $traces
40+
*/
2241
$traces = collect(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 30));
2342

2443
// we limit the backtrace to the current and the previous query (=2 "QueryExecuted"-events),
@@ -29,15 +48,15 @@ private function checkForCascadeEvent(): ?array
2948
$btFunction = $backtrace['function'] ?? null;
3049
$btEvent = $backtrace['args'][0] ?? null;
3150
if ($btClass === Connection::class && $btFunction === 'event' && $btEvent === static::EVENT) {
32-
$queryExecutedEventLimit = $queryExecutedEventLimit - 1;
51+
$queryExecutedEventLimit--;
3352
}
3453

3554
return $queryExecutedEventLimit <= 0;
3655
});
3756

3857
foreach ($traces as $backtrace) {
3958
$btClass = $backtrace['class'] ?? null;
40-
if (!is_a($btClass, \Illuminate\Database\Eloquent\Builder::class, true)) {
59+
if (!is_a($btClass, Builder::class, true)) {
4160
continue;
4261
}
4362

@@ -73,7 +92,7 @@ public function handle(): void
7392
{
7493
$event = $this->checkForCascadeEvent();
7594

76-
if (!is_null($event)) {
95+
if ($event !== null) {
7796
$builder = $event['builder'];
7897

7998
// add `withTrashed()`, if the model has SoftDeletes
@@ -84,7 +103,7 @@ public function handle(): void
84103

85104
$keyName = $builder->getModel()->getKeyName();
86105

87-
if (!$this->hasCascadingRelations($builder->getModel()) or $keyName === null) {
106+
if ($keyName === null || !$this->hasCascadingRelations($builder->getModel())) {
88107
// If model doesn't have any primary key, there will be no relations
89108
return;
90109
}

src/Listeners/CascadeRestoreListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ class CascadeRestoreListener
99
/**
1010
* Handel the event for eloquent restore.
1111
*
12-
* @param $event
13-
* @param $model
12+
* @param string $event
13+
* @param \Illuminate\Database\Eloquent\Model $model
14+
*
15+
* @throws \Askedio\SoftCascade\Exceptions\SoftCascadeLogicException
1416
*
1517
* @return void
1618
*
1719
* @SuppressWarnings(PHPMD.UnusedFormalParameter("event"))
20+
*
21+
* @noinspection PhpUnusedParameterInspection
1822
*/
1923
public function handle($event, $model)
2024
{

src/Providers/EventServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class EventServiceProvider extends ServiceProvider
1212
/**
1313
* The event listener mappings for the application.
1414
*
15-
* @var array
15+
* @var array<string, array<int, class-string>>
1616
*/
1717
protected $listen = [
18-
'eloquent.deleting: *' => [CascadeDeleteListener::class],
19-
'eloquent.restoring: *' => [CascadeRestoreListener::class],
20-
CascadeQueryListener::EVENT => [CascadeQueryListener::class],
18+
'eloquent.deleting: *' => [CascadeDeleteListener::class],
19+
'eloquent.restoring: *' => [CascadeRestoreListener::class],
20+
CascadeQueryListener::EVENT => [CascadeQueryListener::class],
2121
];
2222
}

src/Providers/GenericServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class GenericServiceProvider extends ServiceProvider
1313
*/
1414
public function register()
1515
{
16-
//
1716
}
1817

1918
/**

src/Providers/LumenEventServiceProvider.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Askedio\SoftCascade\Providers;
44

5+
use Askedio\SoftCascade\Listeners\CascadeDeleteListener;
6+
use Askedio\SoftCascade\Listeners\CascadeQueryListener;
7+
use Askedio\SoftCascade\Listeners\CascadeRestoreListener;
8+
use Illuminate\Database\Events\QueryExecuted;
59
use Illuminate\Support\Facades\Event;
610
use Illuminate\Support\ServiceProvider;
711

@@ -10,21 +14,12 @@ class LumenEventServiceProvider extends ServiceProvider
1014
/**
1115
* The event listener mappings for the application.
1216
*
13-
* @var array
17+
* @return void
1418
*/
1519
public function register()
1620
{
17-
Event::listen(
18-
'eloquent.deleting: *',
19-
['Askedio\SoftCascade\Listeners\CascadeDeleteListener', 'handle']
20-
);
21-
Event::listen(
22-
'eloquent.restoring: *',
23-
['Askedio\SoftCascade\Listeners\CascadeRestoreListener', 'handle']
24-
);
25-
Event::listen(
26-
'Illuminate\Database\Events\QueryExecuted',
27-
['Askedio\SoftCascade\Listeners\CascadeQueryListener', 'handle']
28-
);
21+
Event::listen('eloquent.deleting: *', [CascadeDeleteListener::class, 'handle']);
22+
Event::listen('eloquent.restoring: *', [CascadeRestoreListener::class, 'handle']);
23+
Event::listen(QueryExecuted::class, [CascadeQueryListener::class, 'handle']);
2924
}
3025
}

src/Providers/LumenServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class LumenServiceProvider extends ServiceProvider
1313
*/
1414
public function register()
1515
{
16-
//
1716
}
1817

1918
/**

0 commit comments

Comments
 (0)