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
Pull Request — master (#688)
by
unknown
18:52
created

Page::getModelConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Navigation;
4
5
use SleepingOwl\Admin\Contracts\Navigation\PageInterface;
6
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
7
8
class Page extends \KodiComponents\Navigation\Page implements PageInterface
9
{
10
    /**
11
     * Menu item related model class.
12
     * @var string
13
     */
14
    protected $model;
15
16
    /**
17
     * Menu item by url id.
18
     * @var string
19
     */
20
    protected $aliasId;
21
22
    /**
23
     * @param string|null $modelClass
24
     */
25 1
    public function __construct($modelClass = null)
26
    {
27 1
        parent::__construct();
28
29 1
        $this->setModel($modelClass);
30
31 1
        if ($this->hasModel()) {
32 1
            $this->setIcon($this->getModelConfiguration()->getIcon());
33 1
        }
34 1
    }
35
36
    /**
37
     * Set Alias Id.
38
     */
39
    public function setAliasId()
40
    {
41
        $url = parse_url($this->getUrl(), PHP_URL_PATH);
42
        if ($url) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false 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...
43
            $this->aliasId = md5($url);
44
        }
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getAliasId()
51
    {
52
        return $this->aliasId;
53
    }
54
55
    /**
56
     * @return ModelConfigurationInterface
57
     */
58 1
    public function getModelConfiguration()
59
    {
60 1
        if (! $this->hasModel()) {
61
            return;
62
        }
63
64 1
        return app('sleeping_owl')->getModel($this->model);
65
    }
66
67
    /**
68
     * @return bool
69
     */
70 1
    public function hasModel()
71
    {
72 1
        return ! is_null($this->model) && class_exists($this->model);
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getId()
79
    {
80
        if (is_null($this->id) && $this->hasModel()) {
81
            return $this->model;
82
        }
83
84
        return parent::getId();
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getTitle()
91
    {
92
        if (is_null($this->title) && $this->hasModel()) {
93
            return $this->getModelConfiguration()->getTitle();
94
        }
95
96
        return parent::getTitle();
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getUrl()
103
    {
104
        if (is_null($this->url) && $this->hasModel()) {
105
            return $this->getModelConfiguration()->getDisplayUrl();
106
        }
107
108
        return parent::getUrl();
109
    }
110
111
    /**
112
     * @return \Closure
113
     */
114
    public function getAccessLogic()
115
    {
116
        if (! is_callable($this->accessLogic)) {
117
            if ($this->hasModel()) {
118
                return function () {
119
                    return $this->getModelConfiguration()->isDisplayable();
120
                };
121
            }
122
        }
123
124
        return parent::getAccessLogic();
125
    }
126
127
    /**
128
     * @param string|null $view
129
     *
130
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
131
     */
132
    public function render($view = null)
133
    {
134
        $data = $this->toArray();
135
136
        if (! is_null($view)) {
137
            return view($view, $data)->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
138
        }
139
140
        return app('sleeping_owl.template')->view('_partials.navigation.page', $data)->render();
141
    }
142
143
    /**
144
     * @param string $model
145
     *
146
     * @return $this
147
     */
148 1
    protected function setModel($model)
149
    {
150 1
        $this->model = $model;
151
152 1
        return $this;
153
    }
154
}
155