CascadeDelete   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 37
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bootCascadeDelete() 0 6 1
A deleteMorphResidual() 0 3 1
A getCascadeDeleteMorph() 0 3 1
1
<?php
2
3
namespace Cesargb\Database\Support;
4
5
trait CascadeDelete
6
{
7
    /**
8
     * Boot the trait.
9
     *
10
     * Listen for the deleted event of a model, and run
11
     * the delete operation for morphs configured relationship methods.
12
     */
13
    protected static function bootCascadeDelete()
14
    {
15
        static::deleted(function ($model) {
16
            $morph = new Morph();
17
18
            $morph->delete($model);
19
        });
20
    }
21
22
    /**
23
     * Fetch methods name than must return a morph relation.
24
     *
25
     * @return string[]
26
     */
27
    public function getCascadeDeleteMorph()
28
    {
29
        return (array) ($this->cascadeDeleteMorph ?? []);
30
    }
31
32
    /**
33
     * Clean residual morph relation from a model. Return number
34
     * of deleted rows.
35
     *
36
     * @param  bool  $dryRun
37
     * @return int
38
     */
39
    public function deleteMorphResidual(bool $dryRun = false)
40
    {
41
        return (new Morph())->cleanResidualByModel($this, $dryRun);
0 ignored issues
show
Bug introduced by
$this of type Cesargb\Database\Support\CascadeDelete is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Cesargb\Database\Support...:cleanResidualByModel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return (new Morph())->cleanResidualByModel(/** @scrutinizer ignore-type */ $this, $dryRun);
Loading history...
42
    }
43
}
44