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 — development-bs4 ( bc8456...b9684a )
by butschster
29:24 queued 19:50
created

DisplayTabbed   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 288
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 78
dl 0
loc 288
rs 8.5599
c 2
b 0
f 0
wmc 48

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getTabs() 0 3 1
A getModel() 0 5 3
A appendTab() 0 7 1
A setTabs() 0 7 2
A getValue() 0 2 1
B setId() 0 23 8
A validateForm() 0 7 3
A isReadonly() 0 3 1
A __construct() 0 6 3
A setElements() 0 11 3
A saveForm() 0 7 3
A setAction() 0 9 2
A setModelClass() 0 9 2
B initialize() 0 32 10
A addElement() 0 5 1
A isVisibled() 0 3 1
A toArray() 0 4 1
A isValueSkipped() 0 3 1
A getElementContainer() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like DisplayTabbed often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DisplayTabbed, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Database\Eloquent\Model;
8
use SleepingOwl\Admin\Traits\FormElements;
9
use Illuminate\Contracts\Support\Renderable;
10
use SleepingOwl\Admin\Traits\VisibleCondition;
11
use SleepingOwl\Admin\Contracts\Form\FormInterface;
12
use SleepingOwl\Admin\Contracts\Display\TabInterface;
13
use SleepingOwl\Admin\Model\SectionModelConfiguration;
14
use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
15
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
16
17
/**
18
 * @property TabInterface[]|DisplayTabsCollection $elements
19
 */
20
class DisplayTabbed implements DisplayInterface, FormInterface
21
{
22
    use FormElements, VisibleCondition, \SleepingOwl\Admin\Traits\Renderable;
23
24
    /**
25
     * @var string
26
     */
27
    protected $view = 'display.tabbed';
28
29
    /**
30
     * DisplayTabbed constructor.
31
     *
32
     * @param Closure|TabInterface[] $tabs
33
     */
34
    public function __construct($tabs = null)
35
    {
36
        $this->elements = new DisplayTabsCollection();
37
38
        if (is_array($tabs) || is_callable($tabs)) {
39
            $this->setTabs($tabs);
40
        }
41
    }
42
43
    /**
44
     * Initialize tabbed interface.
45
     */
46
    public function initialize()
47
    {
48
        $this->initializeElements();
49
50
        $tabs = $this->getTabs();
51
52
        foreach ($tabs as $tab) {
53
            if (method_exists($tab->getContent(), 'getElements')) {
54
                $elements = $tab->getContent()->getElements();
55
                foreach ($elements as $element) {
56
                    if ($element instanceof self) {
57
                        foreach ($element->getTabs() as $subTab) {
58
                            if ($subTab->getName() == session('sleeping_owl_tab_id')) {
59
                                $tab->setActive(true);
60
                                $subTab->setActive(true);
61
                            }
62
                        }
63
                    }
64
                }
65
            }
66
67
            if ($tab->getName() == session('sleeping_owl_tab_id')) {
68
                $tab->setActive(true);
69
            }
70
        }
71
72
        $activeTabs = $this->getTabs()->filter(function (TabInterface $tab) {
73
            return $tab->isActive();
74
        })->count();
75
76
        if ($activeTabs === 0 && $firstTab = $this->getTabs()->first()) {
77
            $firstTab->setActive(true);
78
        }
79
    }
80
81
    /**
82
     * @return Model $model|null
83
     */
84
    public function getModel()
85
    {
86
        foreach ($this->getTabs() as $tab) {
87
            if ($tab->getContent() instanceof FormInterface) {
88
                return $tab->getContent()->getModel();
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param string $class
95
     *
96
     * @return $this
97
     */
98
    public function setModelClass($class)
99
    {
100
        $this->getTabs()->each(function (TabInterface $tab) use ($class) {
101
            if ($tab instanceof DisplayInterface) {
102
                $tab->setModelClass($class);
103
            }
104
        });
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return TabInterface[]|\SleepingOwl\Admin\Form\FormElementsCollection
111
     */
112
    public function getTabs()
113
    {
114
        return $this->getElements();
115
    }
116
117
    /**
118
     * @param Closure|TabInterface[] $tabs
119
     *
120
     * @return $this
121
     */
122
    public function setTabs($tabs)
123
    {
124
        if (is_callable($tabs)) {
125
            $tabs = call_user_func($tabs, $this);
126
        }
127
128
        return $this->setElements($tabs);
129
    }
130
131
    /**
132
     * @param array $elements
133
     *
134
     * @return $this
135
     */
136
    public function setElements(array $elements)
137
    {
138
        foreach ($elements as $label => $tab) {
139
            if ($tab instanceof TabInterface) {
140
                $this->addElement($tab);
141
            } else {
142
                $this->appendTab($tab, $label);
143
            }
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param Renderable $display
151
     * @param string $label
152
     * @param bool|false $active
153
     *
154
     * @return DisplayTab
155
     */
156
    public function appendTab(Renderable $display, $label, $active = false)
157
    {
158
        $this->addElement(
159
            $tab = app('sleeping_owl.display')->tab($display, $label)->setActive($active)
0 ignored issues
show
Bug introduced by
The method tab() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
            $tab = app('sleeping_owl.display')->/** @scrutinizer ignore-call */ tab($display, $label)->setActive($active)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
        );
161
162
        return $tab;
163
    }
164
165
    /**
166
     * @param TabInterface $element
167
     *
168
     * @return $this
169
     */
170
    public function addElement(TabInterface $element)
171
    {
172
        $this->elements->push($element);
173
174
        return $this;
175
    }
176
177
    /**
178
     * @param string $action
179
     *
180
     * @return $this
181
     */
182
    public function setAction($action)
183
    {
184
        $this->getTabs()->each(function (TabInterface $tab) use ($action) {
185
            if ($tab instanceof FormInterface) {
186
                $tab->setAction($action);
187
            }
188
        });
189
190
        return $this;
191
    }
192
193
    /**
194
     * @param int $id
195
     *
196
     * @return $this
197
     */
198
    public function setId($id)
199
    {
200
        $model_class = get_class($this->getModel());
201
        $this->getTabs()->each(function (TabInterface $tab) use ($id, $model_class) {
202
            if ($tab instanceof FormInterface) {
203
                if (! $tab->getExternalForm()) {
0 ignored issues
show
Bug introduced by
The method getExternalForm() does not exist on SleepingOwl\Admin\Contracts\Display\TabInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Display\TabInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
                if (! $tab->/** @scrutinizer ignore-call */ getExternalForm()) {
Loading history...
Bug introduced by
The method getExternalForm() does not exist on SleepingOwl\Admin\Contracts\Form\FormInterface. It seems like you code against a sub-type of SleepingOwl\Admin\Contracts\Form\FormInterface such as SleepingOwl\Admin\Display\DisplayTab. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
                if (! $tab->/** @scrutinizer ignore-call */ getExternalForm()) {
Loading history...
204
                    $tab_content = $tab->getContent();
205
                    if ($tab_content instanceof FormInterface) {
206
                        $tab_model = $tab->getModel();
207
                        $set_id = $model_class == get_class($tab_model);
208
                        $tab_model_section = \AdminSection::getModel($tab_model);
0 ignored issues
show
Bug introduced by
The type AdminSection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
209
                        if (is_object($tab_model_section) && $tab_model_section instanceof SectionModelConfiguration) {
210
                            $set_id = $set_id && $tab->getContent()->getAction() == $tab_model_section->getUpdateUrl($id);
211
                        }
212
                        if ($set_id) {
213
                            $tab->setId($id);
214
                        }
215
                    }
216
                }
217
            }
218
        });
219
220
        return $this;
221
    }
222
223
    /**
224
     * @param \Illuminate\Http\Request $request
225
     * @param ModelConfigurationInterface $model
226
     *
227
     * @return void
228
     */
229
    public function validateForm(Request $request, ModelConfigurationInterface $model = null)
230
    {
231
        $this->getTabs()->each(function ($tab) use ($request, $model) {
232
            $tabId = $request->get('sleeping_owl_tab_id');
233
234
            if ($tab instanceof FormInterface && $tab->getName() == $tabId) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on SleepingOwl\Admin\Contracts\Form\FormInterface. It seems like you code against a sub-type of SleepingOwl\Admin\Contracts\Form\FormInterface such as SleepingOwl\Admin\Display\DisplayTab. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

234
            if ($tab instanceof FormInterface && $tab->/** @scrutinizer ignore-call */ getName() == $tabId) {
Loading history...
235
                $tab->validateForm($request, $model);
236
            }
237
        });
238
    }
239
240
    /**
241
     * @param \Illuminate\Http\Request $request
242
     * @param ModelConfigurationInterface $model
243
     *
244
     * @return void
245
     */
246
    public function saveForm(Request $request, ModelConfigurationInterface $model = null)
247
    {
248
        $this->getTabs()->each(function (TabInterface $tab) use ($request, $model) {
249
            $tabId = $request->get('sleeping_owl_tab_id');
250
251
            if ($tab instanceof FormInterface && $tab->getName() == $tabId) {
252
                $tab->saveForm($request, $model);
253
            }
254
        });
255
    }
256
257
    /**
258
     * @return null
259
     */
260
    public function getValue()
261
    {
262
    }
263
264
    /**
265
     * @return bool
266
     */
267
    public function isReadonly()
268
    {
269
        return false;
270
    }
271
272
    /**
273
     * @return bool
274
     */
275
    public function isVisibled()
276
    {
277
        return true;
278
    }
279
280
    /**
281
     * @return bool
282
     */
283
    public function isValueSkipped()
284
    {
285
        return false;
286
    }
287
288
    /**
289
     * @return array
290
     */
291
    public function toArray()
292
    {
293
        return [
294
            'tabs' => $this->getTabs()->onlyVisible(),
295
        ];
296
    }
297
298
    /**
299
     * Using in trait FormElements;.
300
     *
301
     * @param $object
302
     *
303
     * @return mixed
304
     */
305
    protected function getElementContainer($object)
306
    {
307
        return $object->getContent();
308
    }
309
}
310