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.

Repository::whereFirst()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Milkmeowo\Framework\Base\Repositories\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Prettus\Repository\Eloquent\BaseRepository;
8
use Milkmeowo\Framework\Base\Presenters\Presenter;
9
use Prettus\Repository\Traits\CacheableRepository;
10
use Prettus\Validator\Contracts\ValidatorInterface;
11
use Illuminate\Database\Eloquent\Relations\Relation;
12
use Prettus\Repository\Contracts\CacheableInterface;
13
use Prettus\Repository\Events\RepositoryEntityDeleted;
14
use Prettus\Repository\Events\RepositoryEntityUpdated;
15
use Prettus\Repository\Exceptions\RepositoryException;
16
use Milkmeowo\Framework\Base\Traits\BaseRepositoryEventsTrait;
17
use Milkmeowo\Framework\Base\Models\Contracts\BaseModelEventObserverable;
18
use Milkmeowo\Framework\Base\Repositories\Interfaces\RepositoryInterface;
19
use Milkmeowo\Framework\Base\Repositories\Interfaces\BaseRepositoryEventsInterface;
20
21
abstract class Repository extends BaseRepository implements RepositoryInterface, CacheableInterface, BaseRepositoryEventsInterface
22
{
23
    use CacheableRepository;
24
    /* Model Observers */
25
    use BaseRepositoryEventsTrait;
26
27
    /**
28
     * @var Model
29
     */
30
    protected $relateModel;
31
32
    /**
33
     * @param array $attributes
34
     */
35
    public function validateCreate(array $attributes)
36
    {
37
        if (! is_null($this->validator)) {
38
            $this->validator->with($attributes)->passesOrFail(ValidatorInterface::RULE_CREATE);
39
        }
40
    }
41
42
    /**
43
     * @param array $attributes
44
     */
45
    public function validateUpdate(array $attributes)
46
    {
47
        if (! is_null($this->validator)) {
48
            $this->validator->with($attributes)->passesOrFail(ValidatorInterface::RULE_UPDATE);
49
        }
50
    }
51
52
    /**
53
     * @param $validator
54
     *
55
     * @return $this
56
     */
57
    public function setValidator($validator)
58
    {
59
        $this->validator = $validator;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param $results
66
     *
67
     * @return mixed
68
     */
69
    public function present($results)
70
    {
71
        return $this->parserResult($results);
72
    }
73
74
    /**
75
     * @param Model $relateModel
76
     *
77
     * @return $this
78
     */
79
    public function setRelateModel(Model $relateModel)
80
    {
81
        $this->relateModel = $relateModel;
82
        if ($relateModel) {
83
            $this->makeModel();
84
        }
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return Model
91
     * @throws RepositoryException
92
     */
93
    public function makeModel()
94
    {
95
        $model = $this->relateModel ? $this->relation() : $this->app->make($this->model());
96
        if (! ($model instanceof Model || $model instanceof Relation)) {
97
            throw new RepositoryException('Class '.get_class($model).' must be an instance of Illuminate\\Database\\Eloquent\\Model');
98
        }
99
100
        if ($model instanceof BaseModelEventObserverable) {
101
            $model->setRepository($this); //set repository
102
        }
103
104
        return $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model can also be of type object<Illuminate\Databa...ent\Relations\Relation>. However, the property $model is declared as type object<Illuminate\Database\Eloquent\Model>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Bug Compatibility introduced by
The expression $this->model = $model; of type Illuminate\Database\Eloq...uent\Relations\Relation adds the type Illuminate\Database\Eloquent\Relations\Relation to the return on line 104 which is incompatible with the return type documented by Milkmeowo\Framework\Base...t\Repository::makeModel of type Illuminate\Database\Eloquent\Model.
Loading history...
105
    }
106
107
    /**
108
     * @return Relation
109
     */
110
    public function relation()
111
    {
112
    }
113
114
    /**
115
     * Retrieve data array for populate field select.
116
     *
117
     * @param string      $column
118
     * @param string|null $key
119
     *
120
     * @return \Illuminate\Support\Collection|array
121
     */
122
    public function lists($column, $key = null)
123
    {
124
        $this->applyCriteria();
125
        $this->applyScope();
126
127
        $lists = $this->model->lists($column, $key);
128
129
        $this->resetModel();
130
131
        return $lists;
132
    }
133
134
    /**
135
     * Retrieve all data of repository.
136
     *
137
     * @param array $columns
138
     *
139
     * @return mixed
140
     */
141
    public function all($columns = ['*'])
142
    {
143
        $this->applyCriteria();
144
        $this->applyScope();
145
        if ($this->model instanceof Builder || $this->model instanceof Relation) {
146
            $results = $this->model->get($columns);
147
        } else {
148
            $results = $this->model->all($columns);
149
        }
150
        $this->resetModel();
151
152
        return $this->parserResult($results);
153
    }
154
155
    /**
156
     * @return \Prettus\Repository\Contracts\PresenterInterface
157
     */
158
    public function getPresenter()
159
    {
160
        return $this->presenter;
161
    }
162
163
    /**
164
     * @param array $meta
165
     */
166
    public function setPresenterMeta(array $meta)
167
    {
168
        if ($this->presenter instanceof Presenter) {
169
            $this->presenter->setMeta($meta);
170
        }
171
    }
172
173
    /**
174
     * Where first.
175
     *
176
     * @param array $where
177
     * @param array $columns
178
     *
179
     * @return mixed
180
     */
181
    public function whereFirst(array $where, $columns = ['*'])
182
    {
183
        return $this->where($where)->firstOrFail($columns = ['*']);
184
    }
185
186
    /**
187
     * Retrieve first data of repository with fail if not found.
188
     *
189
     * @param string[] $columns
190
     *
191
     * @return mixed
192
     */
193
    public function firstOrFail($columns = ['*'])
194
    {
195
        $this->applyCriteria();
196
        $this->applyScope();
197
        $results = $this->model->firstOrFail($columns);
198
        $this->resetModel();
199
200
        return $this->parserResult($results);
201
    }
202
203
    /**
204
     * Find data by where conditions.
205
     *
206
     * @param array $where
207
     *
208
     * @return $this
209
     */
210
    public function where(array $where)
211
    {
212
        $this->applyCriteria();
213
        $this->applyScope();
214
        $this->applyConditions($where);
215
216
        return $this;
217
    }
218
219
    /**
220
     * Add a basic where clause to the model.
221
     *
222
     * @param  string|array|\Closure $column
223
     * @param  mixed                 $value
224
     *
225
     * @return $this
226
     */
227
    protected function modelWhere($column, $value = null)
228
    {
229
        $this->model = $this->model->where($column, $value);
230
231
        return $this;
232
    }
233
234
    /**
235
     * withTrashed.
236
     *
237
     * @return $this
238
     */
239
    public function withTrashed()
240
    {
241
        $this->model = $this->model->withTrashed();
242
243
        return $this;
244
    }
245
246
    /**
247
     * without-trashed.
248
     *
249
     * @return $this
250
     */
251
    public function withoutTrashed()
252
    {
253
        $this->model = $this->model->withoutTrashed();
254
255
        return $this;
256
    }
257
258
    /**
259
     * onlyTrashed.
260
     *
261
     * @return $this
262
     */
263
    public function onlyTrashed()
264
    {
265
        $this->model = $this->model->onlyTrashed();
266
267
        return $this;
268
    }
269
270
    /**
271
     * Restore a entity in repository by id.
272
     *
273
     * @param $id
274
     *
275
     * @return mixed
276
     */
277 View Code Duplication
    public function restore($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...
278
    {
279
        $this->applyScope();
280
281
        $temporarySkipPresenter = $this->skipPresenter;
282
        $this->skipPresenter(true);
283
284
        $model = $this->withTrashed()->find($id);
285
286
        $this->skipPresenter($temporarySkipPresenter);
287
        $this->resetModel();
288
289
        $restored = $model->restore();
290
291
        event(new RepositoryEntityUpdated($this, $model));
292
293
        if ($restored) {
294
            $restored = $model;
295
        }
296
297
        return $this->parserResult($restored);
298
    }
299
300
    /**
301
     * restore multiple entities by given criteria.
302
     *
303
     * @param array $where
304
     *
305
     * @return int
306
     */
307
    public function restoreWhere(array $where)
308
    {
309
        $this->applyScope();
310
311
        $temporarySkipPresenter = $this->skipPresenter;
312
        $this->skipPresenter(true);
313
314
        $this->applyConditions($where);
315
316
        $restored = $this->model->withTrashed()->restore();
317
318
        event(new RepositoryEntityUpdated($this, $this->model));
319
320
        $this->skipPresenter($temporarySkipPresenter);
321
        $this->resetModel();
322
323
        if ($restored) {
324
            $restored = $this->model;
325
        }
326
327
        return $this->parserResult($restored);
328
    }
329
330
    /**
331
     * ForceDelete a entity in repository by id.
332
     *
333
     * @param $id
334
     *
335
     * @return int
336
     */
337 View Code Duplication
    public function ForceDelete($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...
338
    {
339
        $this->applyScope();
340
341
        $temporarySkipPresenter = $this->skipPresenter;
342
        $this->skipPresenter(true);
343
344
        $model = $this->find($id);
345
        $originalModel = clone $model;
346
347
        $this->skipPresenter($temporarySkipPresenter);
348
        $this->resetModel();
349
350
        $deleted = $model->ForceDelete();
351
352
        event(new RepositoryEntityDeleted($this, $originalModel));
353
354
        return $deleted;
355
    }
356
357
    /**
358
     * ForceDelete multiple entities by given criteria.
359
     *
360
     * @param array $where
361
     *
362
     * @return null|bool
363
     */
364
    public function forceDeleteWhere(array $where)
365
    {
366
        $this->applyScope();
367
368
        $temporarySkipPresenter = $this->skipPresenter;
369
        $this->skipPresenter(true);
370
371
        $this->applyConditions($where);
372
373
        $deleted = $this->model->ForceDelete();
374
375
        event(new RepositoryEntityDeleted($this, $this->model));
376
377
        $this->skipPresenter($temporarySkipPresenter);
378
        $this->resetModel();
379
380
        return $deleted;
381
    }
382
}
383