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.
Passed
Push — master ( b2510c...ee0bb8 )
by
unknown
12:46
created

DisplayTab::getValidationRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use SleepingOwl\Admin\Form\FormPanel;
6
use Illuminate\Database\Eloquent\Model;
7
use SleepingOwl\Admin\Form\FormDefault;
8
use SleepingOwl\Admin\Navigation\Badge;
9
use SleepingOwl\Admin\Form\FormElements;
10
use SleepingOwl\Admin\Contracts\Validable;
11
use SleepingOwl\Admin\Contracts\WithModel;
12
use SleepingOwl\Admin\Form\Columns\Column;
13
use SleepingOwl\Admin\Form\Element\Hidden;
14
use SleepingOwl\Admin\Form\Columns\Columns;
15
use Illuminate\Contracts\Support\Renderable;
16
use Illuminate\Validation\ValidationException;
17
use SleepingOwl\Admin\Contracts\Initializable;
18
use SleepingOwl\Admin\Traits\VisibleCondition;
19
use SleepingOwl\Admin\Form\FormElementsCollection;
20
use SleepingOwl\Admin\Contracts\Form\FormInterface;
21
use SleepingOwl\Admin\Contracts\Display\TabInterface;
22
use SleepingOwl\Admin\Contracts\Form\ElementsInterface;
23
use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
24
use SleepingOwl\Admin\Contracts\Form\FormElementInterface;
25
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
26
use SleepingOwl\Admin\Exceptions\Display\DisplayTabException;
27
28
class DisplayTab implements TabInterface, DisplayInterface, FormInterface
29
{
30
    use VisibleCondition, \SleepingOwl\Admin\Traits\Renderable;
31
32
    /**
33
     * @var string
34
     */
35
    protected $label;
36
37
    /**
38
     * @var bool
39
     */
40
    protected $active = false;
41
42
    /**
43
     * @var string
44
     */
45
    protected $name;
46
47
    /**
48
     * @var string
49
     */
50
    protected $icon;
51
52
    /**
53
     * @var Renderable
54
     */
55
    protected $content;
56
57
    /**
58
     * @var
59
     */
60
    protected $badge;
61
62
    /**
63
     * @var string
64
     */
65
    protected $view = 'display.tab';
66
67
    /**
68
     * @param Renderable $content
69
     * @param string|null $label
70
     * @param string|null $icon
71
     * @param Badge|string|\Closure|null $badge
72
     */
73 27
    public function __construct(Renderable $content, $label = null, $icon = null, $badge = null)
74
    {
75 27
        $this->content = $content;
76
77 27
        if (! is_null($label)) {
78 1
            $this->setLabel($label);
79 1
        }
80
81 27
        if (! is_null($icon)) {
82 1
            $this->setIcon($icon);
83 1
        }
84
85 27
        if (! is_null($badge)) {
86
            $this->setBadge($badge);
87
        }
88 27
    }
89
90
    /**
91
     * @param Badge|string|\Closure|null $badge
92
     * @return $this
93
     */
94
    public function setBadge($badge)
95
    {
96
        $badgeData = null;
97
98
        if (is_string($badge) || is_callable($badge) || is_numeric($badge)) {
99
            $badgeData = new Badge();
100
            $badgeData->setView('_partials.tabs.badge');
101
            $badgeData->setValue($badge);
102
        }
103
104
        $this->badge = $badgeData;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112 1
    public function getBadge()
113
    {
114 1
        return $this->badge;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 4
    public function getLabel()
121
    {
122 4
        return $this->label;
123
    }
124
125
    /**
126
     * @param string $label
127
     *
128
     * @return $this
129
     */
130 3
    public function setLabel($label)
131
    {
132 3
        $this->label = $label;
133
134 3
        return $this;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140 2
    public function isActive()
141
    {
142 2
        return $this->active;
143
    }
144
145
    /**
146
     * @param bool $active
147
     *
148
     * @return $this
149
     */
150 1
    public function setActive($active = true)
151
    {
152 1
        $this->active = (bool) $active;
153
154 1
        return $this;
155
    }
156
157
    /**
158
     * @return $this
159
     */
160
    public function addTabElement()
161
    {
162
        if ($this->content instanceof FormInterface) {
163
            $this->content->addElement(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SleepingOwl\Admin\Contracts\Form\FormInterface as the method addElement() does only exist in the following implementations of said interface: SleepingOwl\Admin\Display\DisplayTabbed, SleepingOwl\Admin\Form\FormDefault, SleepingOwl\Admin\Form\FormPanel, SleepingOwl\Admin\Form\FormTabbed.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
164
                new FormElements([
165
                    (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()),
166
                ])
167
            );
168
        }
169
170
        if ($this->content instanceof FormElements) {
171
            foreach ($this->content->getElements() as $element) {
172
                if ($element instanceof FormDefault && $element instanceof FormPanel) {
173
                    $element->addElement(
174
                        new FormElements([
175
                            (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()),
176
                        ])
177
                    );
178
                }
179
180 View Code Duplication
                if ($element instanceof FormElements) {
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...
181
                    foreach ($element->getElements() as $subElement) {
182
                        if ($subElement instanceof FormDefault) {
183
                            $subElement->addElement(
184
                                new FormElements([
185
                                    (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()),
186
                                ])
187
                            );
188
                        }
189
                    }
190
                }
191
192
                if ($element instanceof Columns) {
193
                    foreach ($element->getElements() as $column) {
194 View Code Duplication
                        if ($column instanceof Column) {
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...
195
                            foreach ($column->getElements() as $columnElement) {
196
                                if ($columnElement instanceof FormInterface) {
197
                                    $columnElement->addElement(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SleepingOwl\Admin\Contracts\Form\FormInterface as the method addElement() does only exist in the following implementations of said interface: SleepingOwl\Admin\Display\DisplayTabbed, SleepingOwl\Admin\Form\FormDefault, SleepingOwl\Admin\Form\FormPanel, SleepingOwl\Admin\Form\FormTabbed.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
198
                                        new FormElements([
199
                                            (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()),
200
                                        ])
201
                                    );
202
                                }
203
                            }
204
                        }
205
                    }
206
                }
207
            }
208
        }
209
210
        return $this;
211
    }
212
213
    /**
214
     * @return string
215
     * @throws DisplayTabException
216
     */
217 3
    public function getName()
218
    {
219 3
        if (is_null($this->name) and is_null($this->getLabel())) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
220 1
            throw new DisplayTabException('You should set name or label');
221
        }
222
223 2
        return is_null($this->name)
224 2
            ? md5($this->getLabel())
225 2
            : $this->name;
226
    }
227
228
    /**
229
     * @param string $name
230
     *
231
     * @return $this
232
     */
233 1
    public function setName($name)
234
    {
235 1
        $this->name = $name;
236
237 1
        return $this;
238
    }
239
240
    /**
241
     * @return string
242
     */
243 2
    public function getIcon()
244
    {
245 2
        return $this->icon;
246
    }
247
248
    /**
249
     * @param string $icon
250
     *
251
     * @return $this
252
     */
253 1
    public function setIcon($icon)
254
    {
255 1
        $this->icon = $icon;
256
257 1
        return $this;
258
    }
259
260
    /**
261
     * @return Renderable
262
     */
263 19
    public function getContent()
264
    {
265 19
        return $this->content;
266
    }
267
268
    /**
269
     * @param string $class
270
     *
271
     * @return $this
272
     */
273 2
    public function setModelClass($class)
274
    {
275 2
        if (($content = $this->getContent()) instanceof DisplayInterface) {
276 1
            $content->setModelClass($class);
277 1
        }
278
279 2
        return $this;
280
    }
281
282
    /**
283
     * Initialize tab.
284
     *
285
     * @return $this
286
     */
287 2
    public function initialize()
288
    {
289 2
        if (($content = $this->getContent()) instanceof Initializable) {
290 1
            $content->initialize();
291 1
        }
292
293 2
        return $this;
294
    }
295
296
    /**
297
     * @param string $action
298
     *
299
     * @return $this
300
     */
301 2
    public function setAction($action)
302
    {
303 2
        if (($content = $this->getContent()) instanceof FormInterface) {
304 1
            $content->setAction($action);
305 1
        }
306
307 2
        return $this;
308
    }
309
310
    /**
311
     * @param int $id
312
     *
313
     * @return $this
314
     */
315 2
    public function setId($id)
316
    {
317 2
        if (($content = $this->getContent()) instanceof FormInterface) {
318 1
            $content->setId($id);
319 1
        }
320
321 2
        return $this;
322
    }
323
324
    /**
325
     * @param \Illuminate\Http\Request $request
326
     * @param ModelConfigurationInterface $model
327
     *
328
     * @throws ValidationException
329
     */
330 2
    public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null)
331
    {
332 2
        if (($content = $this->getContent()) instanceof FormInterface) {
333 1
            $content->validateForm($request, $model);
334 1
        }
335 2
    }
336
337
    /**
338
     * Save model.
339
     *
340
     * @param \Illuminate\Http\Request $request
341
     * @param ModelConfigurationInterface $model
342
     *
343
     * @return void
344
     */
345 2
    public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null)
346
    {
347 2
        if (($content = $this->getContent()) instanceof FormInterface) {
348 1
            $content->saveForm($request, $model);
349 1
        }
350 2
    }
351
352
    /**
353
     * Set currently rendered instance.
354
     *
355
     * @param Model $model
356
     *
357
     * @return $this
358
     */
359 2
    public function setModel(Model $model)
360
    {
361 2
        if (($content = $this->getContent()) instanceof WithModel) {
362 1
            $content->setModel($model);
363 1
        }
364
365 2
        return $this;
366
    }
367
368
    /**
369
     * @return Model $model
370
     */
371 2
    public function getModel()
372
    {
373 2
        if (($content = $this->getContent()) instanceof WithModel) {
374 1
            return $content->getModel();
375
        }
376 1
    }
377
378
    /**
379
     * Get form item validation rules.
380
     * @return array
381
     */
382 2
    public function getValidationRules()
383
    {
384 2
        if (($content = $this->getContent()) instanceof Validable) {
385 1
            return $content->getValidationRules();
386
        }
387
388 1
        return [];
389
    }
390
391
    /**
392
     * @return array
393
     */
394 2
    public function getValidationMessages()
395
    {
396 2
        if (($content = $this->getContent()) instanceof Validable) {
397 1
            return $content->getValidationMessages();
398
        }
399
400 1
        return [];
401
    }
402
403
    /**
404
     * @return array
405
     */
406 2
    public function getValidationLabels()
407
    {
408 2
        if (($content = $this->getContent()) instanceof Validable) {
409 1
            return $content->getValidationLabels();
410
        }
411
412 1
        return [];
413
    }
414
415
    /**
416
     * @param \Illuminate\Http\Request $request
417
     *
418
     * @return void
419
     */
420 2
    public function save(\Illuminate\Http\Request $request)
421
    {
422 2
        if (($content = $this->getContent()) instanceof FormElementInterface) {
423 1
            $content->save($request);
424 1
        }
425 2
    }
426
427
    /**
428
     * @param \Illuminate\Http\Request $request
429
     *
430
     * @return void
431
     */
432 2
    public function afterSave(\Illuminate\Http\Request $request)
433
    {
434 2
        if (($content = $this->getContent()) instanceof FormElementInterface) {
435 1
            $content->afterSave($request);
436 1
        }
437 2
    }
438
439
    /**
440
     * @return mixed
441
     */
442 2
    public function getValue()
443
    {
444 2
        if (($content = $this->getContent()) instanceof FormElementInterface) {
445 1
            return $content->getValue();
446
        }
447 1
    }
448
449
    /**
450
     * @return bool
451
     */
452 2
    public function isReadonly()
453
    {
454 2
        if (($content = $this->getContent()) instanceof FormElementInterface) {
455 1
            return $content->isReadonly();
456
        }
457
458 1
        return false;
459
    }
460
461
    /**
462
     * @return bool
463
     */
464
    public function isValueSkipped()
465
    {
466
        if (($content = $this->getContent()) instanceof FormElementInterface) {
467
            return $content->isValueSkipped();
468
        }
469
470
        return false;
471
    }
472
473
    /**
474
     * @param string $path
475
     *
476
     * @return FormElementInterface|null
477
     */
478 2
    public function getElement($path)
479
    {
480 2
        if (($content = $this->getContent()) instanceof ElementsInterface) {
481 1
            return $content->getElement($path);
482
        }
483 1
    }
484
485
    /**
486
     * @return FormElementsCollection
487
     */
488 2
    public function getElements()
489
    {
490 2
        if (($content = $this->getContent()) instanceof ElementsInterface) {
491 1
            return $content->getElements();
492
        }
493
494 1
        return new FormElementsCollection();
495
    }
496
497
    /**
498
     * @param array $elements
499
     *
500
     * @return $this
501
     */
502 2
    public function setElements(array $elements)
503
    {
504 2
        if (($content = $this->getContent()) instanceof ElementsInterface) {
505 1
            $content->setElements($elements);
506 1
        }
507
508 2
        return $this;
509
    }
510
511
    /**
512
     * @return array
513
     */
514 1
    public function toArray()
515
    {
516
        return [
517 1
            'label'  => $this->getLabel(),
518 1
            'active' => $this->isActive(),
519 1
            'name'   => $this->getName(),
520 1
            'icon'   => $this->getIcon(),
521 1
            'badge'  => $this->getBadge(),
522 1
        ];
523
    }
524
}
525