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

DisplayTabbed::setModelClass()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Model;
7
use SleepingOwl\Admin\Traits\FormElements;
8
use Illuminate\Contracts\Support\Renderable;
9
use SleepingOwl\Admin\Traits\VisibleCondition;
10
use SleepingOwl\Admin\Contracts\Form\FormInterface;
11
use SleepingOwl\Admin\Contracts\Display\TabInterface;
12
use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
13
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
14
15
/**
16
 * @property TabInterface[]|DisplayTabsCollection $elements
17
 */
18
class DisplayTabbed implements DisplayInterface, FormInterface
19
{
20
    use FormElements, VisibleCondition, \SleepingOwl\Admin\Traits\Renderable;
21
22
    /**
23
     * @var string
24
     */
25
    protected $view = 'display.tabbed';
26
27
    /**
28
     * DisplayTabbed constructor.
29
     *
30
     * @param Closure|TabInterface[] $tabs
31
     */
32
    public function __construct($tabs = null)
33
    {
34
        $this->elements = new DisplayTabsCollection();
35
36
        if (is_array($tabs) or is_callable($tabs)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or 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...
37
            $this->setTabs($tabs);
0 ignored issues
show
Bug introduced by
It seems like $tabs defined by parameter $tabs on line 32 can also be of type array; however, SleepingOwl\Admin\Display\DisplayTabbed::setTabs() does only seem to accept object<Closure>|array<in...\Display\TabInterface>>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
38
        }
39
    }
40
41
    /**
42
     * Initialize tabbed interface.
43
     */
44
    public function initialize()
45
    {
46
        $this->initializeElements();
47
48
        $tabs = $this->getTabs();
49
50
        foreach ($tabs as $tab) {
51
            if (method_exists($tab->getContent(), 'getElements')) {
52
                $elements = $tab->getContent()->getElements();
53
                foreach ($elements as $element) {
54
                    if ($element instanceof self) {
55
                        foreach ($element->getTabs() as $subTab) {
56
                            if ($subTab->getName() == session('sleeping_owl_tab_id')) {
57
                                $tab->setActive(true);
58
                                $subTab->setActive(true);
59
                            }
60
                        }
61
                    }
62
                }
63
            }
64
65
            if ($tab->getName() == session('sleeping_owl_tab_id')) {
66
                $tab->setActive(true);
67
            }
68
        }
69
70
        $activeTabs = $this->getTabs()->filter(function (TabInterface $tab) {
71
            return $tab->isActive();
72
        })->count();
73
74
        if ($activeTabs === 0 and $firstTab = $this->getTabs()->first()) {
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...
75
            $firstTab->setActive(true);
76
        }
77
    }
78
79
    /**
80
     * @return Model $model|null
81
     */
82
    public function getModel()
83
    {
84
        foreach ($this->getTabs() as $tab) {
85
            if ($tab->getContent() instanceof FormInterface) {
86
                return $tab->getContent()->getModel();
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param string $class
93
     *
94
     * @return $this
95
     */
96
    public function setModelClass($class)
97
    {
98
        $this->getTabs()->each(function (TabInterface $tab) use ($class) {
99
            if ($tab instanceof DisplayInterface) {
100
                $tab->setModelClass($class);
101
            }
102
        });
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return TabInterface[]|DisplayTabsCollection
109
     */
110
    public function getTabs()
111
    {
112
        return $this->getElements();
113
    }
114
115
    /**
116
     * @param Closure|TabInterface[] $tabs
117
     *
118
     * @return $this
119
     */
120
    public function setTabs($tabs)
121
    {
122
        if (is_callable($tabs)) {
123
            $tabs = call_user_func($tabs, $this);
124
        }
125
126
        return $this->setElements($tabs);
127
    }
128
129
    /**
130
     * @param array $elements
131
     *
132
     * @return $this
133
     */
134
    public function setElements(array $elements)
135
    {
136
        foreach ($elements as $label => $tab) {
137
            if ($tab instanceof TabInterface) {
138
                $this->addElement($tab);
139
            } else {
140
                $this->appendTab($tab, $label);
141
            }
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param Renderable $display
149
     * @param string $label
150
     * @param bool|false $active
151
     *
152
     * @return TabInterface
153
     */
154
    public function appendTab(Renderable $display, $label, $active = false)
155
    {
156
        $this->addElement(
157
            $tab = app('sleeping_owl.display')->tab($display, $label)->setActive($active)
158
        );
159
160
        return $tab;
161
    }
162
163
    /**
164
     * @param TabInterface $element
165
     *
166
     * @return $this
167
     */
168
    public function addElement(TabInterface $element)
169
    {
170
        $this->elements->push($element);
171
172
        return $this;
173
    }
174
175
    /**
176
     * @param string $action
177
     *
178
     * @return $this
179
     */
180
    public function setAction($action)
181
    {
182
        $this->getTabs()->each(function (TabInterface $tab) use ($action) {
183
            if ($tab instanceof FormInterface) {
184
                $tab->setAction($action);
185
            }
186
        });
187
188
        return $this;
189
    }
190
191
    /**
192
     * @param int $id
193
     *
194
     * @return $this
195
     */
196
    public function setId($id)
197
    {
198
        $this->getTabs()->each(function (TabInterface $tab) use ($id) {
199
            if ($tab instanceof FormInterface) {
200
                $tab->setId($id);
201
            }
202
        });
203
204
        return $this;
205
    }
206
207
    /**
208
     * @param \Illuminate\Http\Request $request
209
     * @param ModelConfigurationInterface $model
210
     *
211
     * @return void
212
     */
213 View Code Duplication
    public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null)
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...
214
    {
215
        $this->getTabs()->each(function ($tab) use ($request, $model) {
216
            $tabId = $request->get('sleeping_owl_tab_id');
217
218
            if ($tab instanceof FormInterface && $tab->getName() == $tabId) {
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 getName() does only exist in the following implementations of said interface: SleepingOwl\Admin\Display\DisplayTab.

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...
219
                $tab->validateForm($request, $model);
220
            }
221
        });
222
    }
223
224
    /**
225
     * @param \Illuminate\Http\Request $request
226
     * @param ModelConfigurationInterface $model
227
     *
228
     * @return void
229
     */
230 View Code Duplication
    public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null)
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...
231
    {
232
        $this->getTabs()->each(function (TabInterface $tab) use ($request, $model) {
233
            $tabId = $request->get('sleeping_owl_tab_id');
234
235
            if ($tab instanceof FormInterface && $tab->getName() == $tabId) {
236
                $tab->saveForm($request, $model);
237
            }
238
        });
239
    }
240
241
    /**
242
     * @return null
243
     */
244
    public function getValue()
245
    {
246
    }
247
248
    /**
249
     * @return bool
250
     */
251
    public function isReadonly()
252
    {
253
        return false;
254
    }
255
256
    /**
257
     * @return bool
258
     */
259
    public function isValueSkipped()
260
    {
261
        return false;
262
    }
263
264
    /**
265
     * @return array
266
     */
267
    public function toArray()
268
    {
269
        return [
270
            'tabs' => $this->getTabs()->onlyVisible(),
271
        ];
272
    }
273
274
    /**
275
     * Using in trait FormElements;.
276
     *
277
     * @param $object
278
     *
279
     * @return mixed
280
     */
281
    protected function getElementContainer($object)
282
    {
283
        return $object->getContent();
284
    }
285
}
286