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 ( e7e058...ce7a57 )
by Aden
03:02
created

ModelAdmin::hasValidating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Models;
4
5
use Illuminate\Support\Str;
6
use LaravelFlare\Flare\Admin\Admin;
7
use LaravelFlare\Flare\Exceptions\ModelAdminException;
8
use LaravelFlare\Flare\Traits\ModelAdmin\ModelQuerying;
9
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelQueryable;
10
11
class ModelAdmin extends Admin implements ModelQueryable
12
{
13
    use ModelQuerying;
14
15
    /**
16
     * Class of Model to Manage.
17
     * 
18
     * @var string
19
     */
20
    protected $managedModel;
21
22
    /**
23
     * ModelAdmin Icon.
24
     *
25
     * Font Awesome Defined Icon, eg 'user' = 'fa-user'
26
     *
27
     * @var string
28
     */
29
    protected $icon = '';
30
31
    /**
32
     * Validation Rules for onCreate, onEdit actions.
33
     * 
34
     * @var array
35
     */
36
    protected $rules = [];
37
38
    /**
39
     * Columns for Model.
40
     *
41
     * Defines which fields to show in the listing tables output.
42
     * 
43
     * @var array
44
     */
45
    protected $columns = [];
46
47
    /**
48
     * Columns for Model are Sortable.
49
     *
50
     * @var bool
51
     */
52
    protected $sortable = true;
53
54
    /**
55
     * The Controller to be used by the Model Admin.
56
     *
57
     * This defaults to parent::getController()
58
     * if it has been left undefined. 
59
     * 
60
     * @var string
61
     */
62
    protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController';
63
64
    /**
65
     * The Policy used for the Model Authorization logic.
66
     *
67
     * This class should implement the ModelAdminPoliceable which
68
     * includes authorization checks for the create, view, edit and delete actions.
69
     * 
70
     * @var string
71
     */
72
    protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy';
73
74
    /**
75
     * The current model to be managed.
76
     * 
77
     * @var Model
78
     */
79
    public $model;
80
81
    /**
82
     * __construct.
83
     */
84
    public function __construct()
85
    {
86
        $this->getManagedModel();
87
88
        $this->model = $this->model();
89
    }
90
91
    /**
92
     * Returns a Model Instance.
93
     * 
94
     * @return Model
95
     */
96
    public function model()
97
    {
98
        if (!$this->model) {
99
            $class = $this->getManagedModel();
100
101
            return $this->model = new $class();
102
        }
103
104
        return $this->model;
105
    }
106
107
    /**
108
     * Returns a New Model Instance.
109
     *
110
     * @return Model
111
     */
112
    public function newModel()
113
    {
114
        $class = self::getManagedModel();
115
116
        return new $class();
117
    }
118
119
    /**
120
     * Returns the Managed Model Class.
121
     * 
122
     * @return string
123
     */
124
    public function getManagedModel()
125
    {
126
        if (!isset($this->managedModel) || $this->managedModel === null) {
127
            throw new ModelAdminException('You have a ModelAdmin which does not have a model assigned to it. ModelAdmins must include a model to manage.', 1);
128
        }
129
130
        return $this->managedModel;
131
    }
132
133
    /**
134
     * Set the Managed Model Class.
135
     * 
136
     * @param string $managedModel
137
     */
138
    public function setManagedModel($managedModel = null)
139
    {
140
        $this->managedModel = $managedModel;
141
    }
142
143
    /**
144
     * Returns the Route Paramets.
145
     * 
146
     * @return array
147
     */
148
    public function routeParameters()
149
    {
150
        return array_merge(parent::routeParameters(), [
151
                                                    'model' => $this->managedModel,
152
                                                ]);
153
    }
154
155
    /**
156
     * Formats and returns the Columns.
157
     *
158
     * This is really gross, I'm removing it soon.
159
     * 
160
     * @return
161
     */
162
    public function getColumns()
163
    {
164
        $columns = [];
165
166
        foreach ($this->columns as $field => $fieldTitle) {
167
            if (in_array($field, $this->model->getFillable())) {
168
                if (!$field) {
169
                    $field = $fieldTitle;
170
                    $fieldTitle = Str::title($fieldTitle);
171
                }
172
                $columns[$field] = $fieldTitle;
173
                continue;
174
            }
175
176
            // We can replace this with data_get() I believe.
177
            if (($methodBreaker = strpos($field, '.')) !== false) {
178
                $method = substr($field, 0, $methodBreaker);
179
                if (method_exists($this->model, $method)) {
180
                    if (method_exists($this->model->$method(), $submethod = str_replace($method.'.', '', $field))) {
181
                        $this->model->$method()->$submethod();
182
183
                        $columns[$field] = $fieldTitle;
184
                        continue;
185
                    }
186
                }
187
            }
188
189
            if (is_numeric($field)) {
190
                $field = $fieldTitle;
191
                $fieldTitle = Str::title($fieldTitle);
192
            }
193
194
            $columns[$field] = $fieldTitle;
195
        }
196
197
        if (count($columns)) {
198
            return $columns;
199
        }
200
201
        return [$this->model->getKeyName() => $this->model->getKeyName()];
202
    }
203
204
    /**
205
     * Gets an Attribute by the provided key
206
     * on either the current model or a provided model instance.
207
     * 
208
     * @param string $key
209
     * @param mixed  $model
210
     * 
211
     * @return mixed
212
     */
213
    public function getAttribute($key, $model = false)
214
    {
215
        if (!$model) {
216
            $model = $this->model;
217
        }
218
219
        if ($this->hasGetAccessor($key)) {
220
            $method = 'get'.Str::studly($key).'Attribute';
221
222
            return $this->{$method}($model);
223
        }
224
225
        if ($this->hasRelatedKey($key, $model)) {
226
            return $this->relatedKey($key, $model);
227
        }
228
229
        return $model->getAttribute($key);
230
    }
231
232
    /**
233
     * Determine if a get accessor exists for an attribute.
234
     *
235
     * @param string $key
236
     * 
237
     * @return bool
238
     */
239
    public function hasGetAccessor($key)
240
    {
241
        return method_exists($this, 'get'.Str::studly($key).'Attribute');
242
    }
243
244
    /**
245
     * Determines if a key resolved a related Model.
246
     * 
247
     * @param string $key
248
     * @param mixed  $model
249
     * 
250
     * @return bool
251
     */
252
    public function hasRelatedKey($key, $model = false)
253
    {
254
        if (!$model) {
255
            $model = $this->model;
256
        }
257
258
        if (($methodBreaker = strpos($key, '.')) !== false) {
259
            $method = substr($key, 0, $methodBreaker);
260
            if (method_exists($model, $method)) {
261
                return true;
262
            }
263
        }
264
265
        return false;
266
    }
267
268
    /**
269
     * Resolves a relation based on the key provided,
270
     * either on the current model or a provided model instance.
271
     * 
272
     * @param string $key
273
     * @param mixed  $model
274
     * 
275
     * @return mixed
276
     */
277
    public function relatedKey($key, $model = false)
278
    {
279
        if (!$model) {
280
            $model = $this->model;
281
        }
282
283
        if (($methodBreaker = strpos($key, '.')) !== false) {
284
            $method = substr($key, 0, $methodBreaker);
285
            if (method_exists($model, $method)) {
286
                if (method_exists($model->$method, $submethod = str_replace($method.'.', '', $key))) {
287
                    return $model->$method->$submethod();
288
                }
289
290
                if (isset($model->$method->$submethod)) {
291
                    return $model->$method->$submethod;
292
                }
293
294
                return $model->getRelationValue($method);
295
            }
296
        }
297
298
        return false;
299
    }
300
301
    /**
302
     * Set a given attribute on the model.
303
     *
304
     * @param string $key
305
     * @param mixed  $value
306
     */
307
    public function setAttribute($key, $value)
308
    {
309
        if ($this->hasSetMutator($key)) {
310
            $method = 'set'.Str::studly($key).'Attribute';
311
312
            return $this->{$method}($value);
313
        }
314
315
        $this->model->attributes[$key] = $value;
316
    }
317
318
    /**
319
     * Determine if a set mutator exists for an attribute.
320
     *
321
     * @param string $key
322
     * 
323
     * @return bool
324
     */
325
    public function hasSetMutator($key)
326
    {
327
        return method_exists($this, 'set'.Str::studly($key).'Attribute');
328
    }
329
330
    /**
331
     * Determine if a get mutator exists for an attribute.
332
     *
333
     * @param string $key
334
     * 
335
     * @return bool
336
     */
337
    public function hasGetMutator($key)
338
    {
339
        return method_exists($this, 'get'.Str::studly($key).'Attribute');
340
    }
341
342
    /**
343
     * Returns an array of Attribute Fields ready for output.
344
     *
345
     * @param  string $type 
346
     * 
347
     * @return array
348
     */
349
    public function outputFields($type = 'view')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
350
    {
351
        return $this->getFields();
352
    }
353
354
    /**
355
     * Gets the Managed Model Mapping.
356
     * 
357
     * @return array
358
     */
359
    public function getFields()
360
    {
361
        return $this->fields;
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...
362
    }
363
364
    /**
365
     * Gets the Managed Model Mapping.
366
     * 
367
     * @param array $fields
368
     */
369
    public function setFields($fields = [])
370
    {
371
        $this->fields = $fields;
372
    }
373
374
    /**
375
     * Determine if the Model Admin is sortable in it's list view.
376
     * 
377
     * @return bool
378
     */
379
    public function isSortable()
380
    {
381
        return isset($this->sortable) && $this->sortable ? true : false;
382
    }
383
384
    /**
385
     * Determine if the Model Admin has Viewing Capabilities.
386
     * 
387
     * @return bool
388
     */
389
    public function hasViewing()
390
    {
391
        return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelViewing::class);
392
    }
393
394
    /**
395
     * Determine if the Model Admin has Creating Capabilities.
396
     * 
397
     * @return bool
398
     */
399
    public function hasCreating()
400
    {
401
        return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelCreating::class);
402
    }
403
404
    /**
405
     * Determine if the Model Admin has Cloning Capabilities.
406
     * 
407
     * @return bool
408
     */
409
    public function hasCloning()
410
    {
411
        return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelCloning::class);
412
    }
413
414
    /**
415
     * Determine if the Model Admin has Editting Capabilities.
416
     * 
417
     * @return bool
418
     */
419
    public function hasEditting()
420
    {
421
        return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelEditting::class);
422
    }
423
424
    /**
425
     * Determine if the Model Admin has Deleting Capabilities.
426
     * 
427
     * @return bool
428
     */
429
    public function hasDeleting()
430
    {
431
        return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelDeleting::class);
432
    }
433
434
    /**
435
     * Determine if the Managed Model is using the SoftDeletes Trait.
436
     *
437
     * This is guarded by hasDeleting, since we shouldn't allow SoftDeleting
438
     * without the deleting trait (even though it isn't really required).
439
     *
440
     * @return bool
441
     */
442
    public function hasSoftDeletes()
443
    {
444
        if (!$this->hasDeleting()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->hasDeleting() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
445
            return false;
446
        }
447
448
        $managedModelClass = $this->getManagedModel();
449
450
        return in_array(
451
            \Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_class(new $managedModelClass()))
452
        ) && in_array(
453
            \LaravelFlare\Flare\Traits\ModelAdmin\ModelSoftDeleting::class, class_uses_recursive(get_class($this))
454
        );
455
    }
456
457
    /**
458
     * Determine if the Model Admin has Validating Capabilities.
459
     * 
460
     * @return bool
461
     */
462
    public function hasValidating()
463
    {
464
        return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelValidating::class);
465
    }
466
467
    /**
468
     * Determine if the Managed Model has a Trait and Contract
469
     *
470
     * @return bool
471
     */
472
    public function hasTraitAndContract($trait = null, $contract = null)
473
    {
474
        return ($this->hasTrait($trait) && $this->hasContract($contract));
475
    }
476
477
    /**
478
     * Returns whether the current ModelAdmin has a given trait.
479
     * 
480
     * @param  string  $trait  
481
     * 
482
     * @return boolean        
483
     */
484
    public function hasTrait($trait = null)
485
    {
486
        if (!$trait) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $trait of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
487
            return;
488
        }
489
        
490
        $managedModelClass = $this->getManagedModel();
491
492
        return in_array($trait, class_uses_recursive(get_class(new $managedModelClass())));
493
    }
494
495
    /**
496
     * Returns whether the current ModelAdmin has a given contract.
497
     * 
498
     * @param  string  $contract  
499
     * 
500
     * @return boolean        
501
     */
502
    public function hasContract($contract = null)
0 ignored issues
show
Unused Code introduced by
The parameter $contract is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
503
    {
504
        if (!$trait) {
0 ignored issues
show
Bug introduced by
The variable $trait does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
505
            return;
506
        }
507
        
508
        $managedModelClass = $this->getManagedModel();
0 ignored issues
show
Unused Code introduced by
$managedModelClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
509
    }
510
}
511