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

ModelWriting   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 4.05 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 19
c 2
b 0
f 1
lcom 1
cbo 8
dl 7
loc 173
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
hasSetMutator() 0 1 ?
setAttribute() 0 1 ?
A beforeSave() 0 6 1
A save() 0 16 3
B doSave() 4 25 6
A afterSave() 3 14 4
B saveRelation() 0 13 5

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\ModelSave;
6
use LaravelFlare\Flare\Events\AfterSave;
7
use LaravelFlare\Flare\Events\BeforeSave;
8
use LaravelFlare\Flare\Exceptions\ModelAdminWriteableException as WriteableException;
9
10
trait ModelWriting
11
{
12
    use ModelCreating, ModelEditting, ModelDeleting;
13
14
    /**
15
     * Used by beforeSave() to ensure child classes call parent::beforeSave().
16
     * 
17
     * @var bool
18
     */
19
    protected $brokenBeforeSave = false;
20
21
    /**
22
     * Used by afterSave() to ensure child classes call parent::afterSave().
23
     * 
24
     * @var bool
25
     */
26
    protected $brokenAfterSave = false;
27
28
    /**
29
     * Relations to Update during Save and
30
     * the appropriate method to fire the update with.
31
     * 
32
     * @var array
33
     */
34
    protected $doSaveRelations = ['BelongsTo' => 'associate'];
35
36
    /**
37
     * Relations to Update after Save and
38
     * the appropriate method to fire the update with.
39
     * 
40
     * @var array
41
     */
42
    protected $afterSaveRelations = ['BelongsToMany' => 'sync'];
43
44
    /**
45
     * Trait Requires hasSetMutator method.
46
     * 
47
     * @param string $key
48
     * 
49
     * @return
50
     */
51
    abstract public function hasSetMutator($key);
52
53
    /**
54
     * Trait Requires setAttribute method.
55
     *
56
     * @param string $key
57
     * @param string $value
58
     * 
59
     * @return
60
     */
61
    abstract public function setAttribute($key, $value);
62
63
    /**
64
     * Trait Requires Find Method (usually provided by ModelQuerying).
65
     *
66
     * @param int $modelitem_id
67
     * 
68
     * @return
69
     */
70
    abstract protected function find($modelitem_id);
71
72
    /**
73
     * Method fired before the Save action is undertaken.
74
     * 
75
     * @return
76
     */
77
    protected function beforeSave()
78
    {
79
        $this->brokenBeforeSave = false;
80
81
        event(new BeforeSave($this));
82
    }
83
84
    /**
85
     * Save Action.
86
     *
87
     * Fires off beforeSave(), doSave() and afterSave()
88
     * 
89
     * @return
90
     */
91
    public function save()
92
    {
93
        $this->brokenBeforeSave = true;
94
        $this->beforeSave();
95
        if ($this->brokenBeforeSave) {
96
            throw new WriteableException('ModelAdmin has a broken beforeSave method. Make sure you call parent::beforeSave() on all instances of beforeSave()', 1);
97
        }
98
99
        $this->doSave();
100
101
        $this->brokenAfterSave = true;
102
        $this->afterSave();
103
        if ($this->brokenAfterSave) {
104
            throw new WriteableException('ModelAdmin has a broken afterSave method. Make sure you call parent::afterSave() on all instances of afterSave()', 1);
105
        }
106
    }
107
108
    /**
109
     * The actual Save action, which does all of hte pre-processing
110
     * required before we are able to perform the save() function.
111
     * 
112
     * @return
113
     */
114
    private function doSave()
115
    {
116
        foreach (\Request::only(array_keys($this->fields)) as $key => $value) {
0 ignored issues
show
Bug introduced by
The property fields 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...
117
            if (!\Schema::hasColumn($this->model->getTable(), $key)) {
118
                continue;
119
            }
120
121
            if ($this->hasSetMutator($key)) {
122
                $this->setAttribute($key, $value);
123
                continue;
124
            }
125
126
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
127 View Code Duplication
            if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
                $this->saveRelation('doSave', $key, $value);
129
                continue;
130
            }
131
132
            $this->model->setAttribute($key, $value);
133
        }
134
135
        $this->model->save();
136
137
        event(new ModelSave($this));
138
    }
139
140
    /**
141
     * Method fired after the Save action is complete.
142
     * 
143
     * @return
144
     */
145
    protected function afterSave()
146
    {
147
        $this->brokenAfterSave = false;
148
149
        foreach (\Request::except('_token') as $key => $value) {
150
151
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
152 View Code Duplication
            if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
153
                $this->saveRelation('afterSave', $key, $value);
154
            }
155
        }
156
157
        event(new AfterSave($this));
158
    }
159
160
    /**
161
     * Method fired to Save Relations.
162
     *
163
     * @param string $action The current action (either doSave or afterSave)
164
     * @param string $key
165
     * @param string $value
166
     * 
167
     * @return
168
     */
169
    private function saveRelation($action, $key, $value)
170
    {
171
        // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
172
        if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
173
            foreach ($this->{$action.'Relations'} as $relationship => $method) {
174
                if (is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\\'.$relationship)) {
175
                    $this->model->$key()->$method($value);
176
177
                    return;
178
                }
179
            }
180
        }
181
    }
182
}
183