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.

BaseModel   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 208
Duplicated Lines 2.88 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 6
loc 208
rs 10
c 1
b 1
f 0
wmc 24
lcom 1
cbo 7

10 Methods

Rating   Name   Duplication   Size   Complexity  
A api_auth() 0 4 1
A getRelatedUserId() 0 4 1
B updateUsers() 6 15 7
A getAuthUser() 0 11 3
A getAuthUserId() 0 10 2
A isAuthUserOwner() 0 4 1
A getTableColumns() 0 4 1
A hasTableColumn() 0 4 1
A hasTableColumns() 0 4 1
B updateIps() 0 12 6

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 Milkmeowo\Framework\Base\Models;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Database\Eloquent\Model;
7
use Prettus\Repository\Contracts\Transformable;
8
use Prettus\Repository\Traits\TransformableTrait;
9
use Milkmeowo\Framework\Base\Traits\BaseModelEventsTrait;
10
use Milkmeowo\Framework\Base\Models\Contracts\BaseModelEventObserverable;
11
12
class BaseModel extends Model implements BaseModelEventObserverable, Transformable
13
{
14
    use BaseModelEventsTrait, TransformableTrait;
15
16
    /**
17
     * Indicates if the model should be auto set user_id.
18
     *
19
     * @var bool
20
     */
21
    protected $autoRelatedUserId = true;
22
23
    /**
24
     * The name of the "related user id" column.
25
     *
26
     * @var string
27
     */
28
    const RELATED_USER_ID = 'user_id';
29
30
    /**
31
     * Indicates if the model should be recorded users.
32
     *
33
     * @var bool
34
     */
35
    protected $userstamps = true;
36
37
    /**
38
     * The name of the "created by" column.
39
     *
40
     * @var string
41
     */
42
    const CREATED_BY = 'created_by';
43
44
    /**
45
     * The name of the "updated by" column.
46
     *
47
     * @var string
48
     */
49
    const UPDATED_BY = 'updated_by';
50
51
    /**
52
     * Indicates if the model should be recorded ips.
53
     *
54
     * @var bool
55
     */
56
    protected $ipstamps = true;
57
58
    /**
59
     * The name of the "created ip" column.
60
     *
61
     * @var string
62
     */
63
    const CREATED_IP = 'created_ip';
64
65
    /**
66
     * The name of the "updated ip" column.
67
     *
68
     * @var string
69
     */
70
    const UPDATED_IP = 'updated_ip';
71
72
    /**
73
     * The name of the "deleted ip" column.
74
     *
75
     * @var string
76
     */
77
    const DELETED_IP = 'deleted_ip';
78
79
    /**
80
     * The name of the "deleted by" column.
81
     *
82
     * @var string
83
     */
84
    const DELETED_BY = 'deleted_by';
85
86
    /**
87
     * @return \Dingo\Api\Auth\Auth
88
     */
89
    protected function api_auth()
90
    {
91
        return app('Dingo\Api\Auth\Auth');
92
    }
93
94
    /**
95
     * Update the creation and update ips.
96
     *
97
     * @return void
98
     */
99
    protected function updateIps()
100
    {
101
        $ip = smart_get_client_ip();
102
103
        if (! $this->isDirty(static::UPDATED_IP) && $this->hasTableColumn(static::UPDATED_IP)) {
104
            $this->{static::UPDATED_IP} = $ip;
105
        }
106
107
        if (! $this->exists && ! $this->isDirty(static::CREATED_IP) && $this->hasTableColumn(static::CREATED_IP)) {
108
            $this->{static::CREATED_IP} = $ip;
109
        }
110
    }
111
112
    /**
113
     * Get current model's user_id.
114
     *
115
     * @return mixed|null
116
     */
117
    public function getRelatedUserId()
118
    {
119
        return $this->{static::RELATED_USER_ID};
120
    }
121
122
    /**
123
     * Update the creation and update by users.
124
     *
125
     * @return void
126
     */
127
    protected function updateUsers()
128
    {
129
        $user_id = $this->getAuthUserId();
130
        if (! ($user_id > 0)) {
131
            return;
132
        }
133
134 View Code Duplication
        if (! $this->isDirty(static::UPDATED_BY) && $this->hasTableColumn(static::UPDATED_BY)) {
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...
135
            $this->{static::UPDATED_BY} = $user_id;
136
        }
137
138 View Code Duplication
        if (! $this->exists && ! $this->isDirty(static::CREATED_BY) && $this->hasTableColumn(static::CREATED_BY)) {
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...
139
            $this->{static::CREATED_BY} = $user_id;
140
        }
141
    }
142
143
    /**
144
     * Get current auth user.
145
     *
146
     * @return \Illuminate\Auth\GenericUser|Model|null
147
     */
148
    public function getAuthUser()
149
    {
150
        $user = null;
151
        if ($this->api_auth()->check()) {
152
            $user = $this->api_auth()->user();
153
        } elseif (Auth::check()) {
154
            $user = Auth::user();
155
        }
156
157
        return $user;
158
    }
159
160
    /**
161
     * Get current auth user_id.
162
     *
163
     * @return mixed|null
164
     */
165
    public function getAuthUserId()
166
    {
167
        $user_id = null;
168
        $user = $this->getAuthUser();
169
        if ($user) {
170
            $user_id = $user->getKey();
0 ignored issues
show
Bug introduced by
The method getKey does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Contracts\Auth\Authenticatable.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
171
        }
172
173
        return $user_id;
174
    }
175
176
    /**
177
     * check auth user owner the current model.
178
     *
179
     * @return bool
180
     */
181
    public function isAuthUserOwner()
182
    {
183
        return $this->getAuthUserId() == $this->getRelatedUserId();
184
    }
185
186
    /**
187
     * get all the database table columns listing.
188
     *
189
     * @return array
190
     */
191
    public function getTableColumns()
192
    {
193
        return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
194
    }
195
196
    /**
197
     * check column exist in table.
198
     *
199
     * @param string $column
200
     *
201
     * @return bool
202
     */
203
    public function hasTableColumn($column)
204
    {
205
        return $this->getConnection()->getSchemaBuilder()->hasColumn($this->getTable(), $column);
206
    }
207
208
    /**
209
     * check columns exist in table.
210
     *
211
     * @param array $columns
212
     *
213
     * @return bool
214
     */
215
    public function hasTableColumns(array $columns)
216
    {
217
        return $this->getConnection()->getSchemaBuilder()->hasColumns($this->getTable(), $columns);
218
    }
219
}
220