GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 508770...7d3333 )
by Aden
03:03
created

ModelSoftDeleting   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 15 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 10
c 4
b 2
f 1
lcom 1
cbo 5
dl 18
loc 120
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 4 1
A findWithTrashed() 0 6 1
A findOnlyTrashed() 0 6 1
A beforeDelete() 0 6 1
A delete() 18 18 3
A doDelete() 0 12 2
A afterDelete() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace LaravelFlare\Flare\Traits\ModelAdmin;
4
5
use LaravelFlare\Flare\Events\ModelDelete;
6
use LaravelFlare\Flare\Events\AfterDelete;
7
use LaravelFlare\Flare\Events\BeforeDelete;
8
use LaravelFlare\Flare\Events\ModelSoftDelete;
9
use LaravelFlare\Flare\Exceptions\ModelAdminWriteableException as WriteableException;
10
11
trait ModelSoftDeleting
12
{
13
    /**
14
     * Shows that this is a soft deleting model.
15
     * 
16
     * @var bool
17
     */
18
    public $softDeletingModel = true;
19
20
    /**
21
     * Overrides the ModelQuerying provided method 
22
     * with one which searches withTrashed scope.
23
     * 
24
     * @param int $modelitem_id
25
     * 
26
     * @return
27
     */
28
    public function find($modelitem_id)
29
    {
30
        return $this->findWithTrashed($modelitem_id);
31
    }
32
33
    /**
34
     * Finds model and includes withTrashed() scope in query.
35
     *
36
     * @param int $modelitem_id
37
     * 
38
     * @return
39
     */
40
    public function findWithTrashed($modelitem_id)
41
    {
42
        $this->model = $this->model->withTrashed()->findOrFail($modelitem_id);
43
44
        return $this->model;
45
    }
46
47
    /**
48
     * Finds model and includes onlyTrashed() scope in query.
49
     *
50
     * @param int $modelitem_id
51
     * 
52
     * @return
53
     */
54
    public function findOnlyTrashed($modelitem_id)
55
    {
56
        $this->model = $this->model->onlyTrashed()->findOrFail($modelitem_id);
57
58
        return $this->model;
59
    }
60
61
    /**
62
     * Method fired before the Delete action is undertaken.
63
     * 
64
     * @return
65
     */
66
    protected function beforeDelete()
67
    {
68
        $this->brokenBeforeDelete = false;
0 ignored issues
show
Bug introduced by
The property brokenBeforeDelete does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
70
        event(new BeforeDelete($this));
71
    }
72
73
    /**
74
     * Delete Action.
75
     *
76
     * Fires off beforeDelete(), doDelete() and afterDelete()
77
     * 
78
     * @param int $modelitem_id
79
     * 
80
     * @return
81
     */
82 View Code Duplication
    public function delete($modelitem_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $this->findWithTrashed($modelitem_id);
85
86
        $this->brokenBeforeDelete = true;
87
        $this->beforeDelete();
88
        if ($this->brokenBeforeDelete) {
89
            throw new WriteableException('ModelAdmin has a broken beforeDelete method. Make sure you call parent::beforeDelete() on all instances of beforeDelete()', 1);
90
        }
91
92
        $this->doDelete();
93
94
        $this->brokenAfterDelete = true;
0 ignored issues
show
Bug introduced by
The property brokenAfterDelete does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
95
        $this->afterDelete();
96
        if ($this->brokenAfterDelete) {
97
            throw new WriteableException('ModelAdmin has a broken afterDelete method. Make sure you call parent::afterDelete() on all instances of afterDelete()', 1);
98
        }
99
    }
100
101
    /**
102
     * The actual delete action.
103
     * 
104
     * @return
105
     */
106
    private function doDelete()
107
    {
108
        if (!$this->model->trashed()) {
109
            $this->model->delete();
110
            event(new ModelSoftDelete($this));
111
112
            return;
113
        }
114
115
        $this->model->forceDelete();
116
        event(new ModelDelete($this));
117
    }
118
119
    /**
120
     * Method fired after the Delete action is complete.
121
     * 
122
     * @return
123
     */
124
    protected function afterDelete()
125
    {
126
        $this->brokenAfterDelete = false;
127
128
        event(new AfterDelete($this));
129
    }
130
}
131