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 ( 9e7309...ca86c4 )
by Aden
39:37 queued 06:18
created

ModelSaving   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 5.98 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 4
dl 7
loc 117
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 3 1
A save() 0 12 1
B doSave() 4 21 5
A afterSave() 3 13 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\Admin\Models\Traits;
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 ModelSaving
11
{
12
    /**
13
     * Relations to Update during Save and
14
     * the appropriate method to fire the update with.
15
     * 
16
     * @var array
17
     */
18
    protected $doSaveRelations = ['BelongsTo' => 'associate'];
19
20
    /**
21
     * Relations to Update after Save and
22
     * the appropriate method to fire the update with.
23
     * 
24
     * @var array
25
     */
26
    protected $afterSaveRelations = ['BelongsToMany' => 'sync'];
27
28
    /**
29
     * Method fired before the Save action is undertaken.
30
     * 
31
     * @return
32
     */
33
    protected function beforeSave()
34
    {
35
    }
36
37
    /**
38
     * Save Action.
39
     *
40
     * Fires off beforeSave(), doSave() and afterSave()
41
     * 
42
     * @return
43
     */
44
    public function save()
45
    {
46
        event(new BeforeSave($this));
47
48
        $this->beforeSave();
49
50
        $this->doSave();
51
52
        $this->afterSave();
53
54
        event(new AfterSave($this));
55
    }
56
57
    /**
58
     * The actual Save action, which does all of hte pre-processing
59
     * required before we are able to perform the save() function.
60
     * 
61
     * @return
62
     */
63
    private function doSave()
64
    {
65
        foreach (\Request::only(array_keys($this->fields->toArray())) 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...
66
            if ($this->hasSetMutator($key)) {
0 ignored issues
show
Bug introduced by
It seems like hasSetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
                $this->setAttribute($key, $value);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
                continue;
69
            }
70
71
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
72 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...
73
                $this->saveRelation('doSave', $key, $value);
74
                continue;
75
            }
76
77
            $this->model->setAttribute($key, $value);
78
        }
79
80
        $this->model->save();
81
82
        event(new ModelSave($this));
83
    }
84
85
    /**
86
     * Method fired after the Save action is complete.
87
     * 
88
     * @return
89
     */
90
    protected function afterSave()
91
    {
92
        $this->brokenAfterSave = false;
0 ignored issues
show
Bug introduced by
The property brokenAfterSave 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...
93
94
        foreach (\Request::except('_token') as $key => $value) {
95
96
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
97 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...
98
                $this->saveRelation('afterSave', $key, $value);
99
            }
100
        }
101
102
    }
103
104
    /**
105
     * Method fired to Save Relations.
106
     *
107
     * @param string $action The current action (either doSave or afterSave)
108
     * @param string $key
109
     * @param string $value
110
     * 
111
     * @return
112
     */
113
    private function saveRelation($action, $key, $value)
114
    {
115
        // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
116
        if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
117
            foreach ($this->{$action.'Relations'} as $relationship => $method) {
118
                if (is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\\'.$relationship)) {
119
                    $this->model->$key()->$method($value);
120
121
                    return;
122
                }
123
            }
124
        }
125
    }
126
}
127