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 — analysis-m4VopE ( 4da4ca )
by butschster
12:05
created

DisplayTabbed   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Importance

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

161
            $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...
162
        );
163
164
        return $tab;
165
    }
166
167
    /**
168
     * @param TabInterface $element
169
     *
170
     * @return $this
171
     */
172
    public function addElement(TabInterface $element)
173
    {
174
        $this->elements->push($element);
175
176
        return $this;
177
    }
178
179
    /**
180
     * @param string $action
181
     *
182
     * @return $this
183
     */
184
    public function setAction($action)
185
    {
186
        $this->getTabs()->each(function (TabInterface $tab) use ($action) {
187
            if ($tab instanceof FormInterface) {
188
                $tab->setAction($action);
189
            }
190
        });
191
192
        return $this;
193
    }
194
195
    /**
196
     * @param int $id
197
     *
198
     * @return $this
199
     */
200
    public function setId($id)
201
    {
202
        $model_class = get_class($this->getModel());
203
        $this->getTabs()->each(function (TabInterface $tab) use ($id, $model_class) {
204
            if ($tab instanceof FormInterface) {
205
                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

205
                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

205
                if (! $tab->/** @scrutinizer ignore-call */ getExternalForm()) {
Loading history...
206
                    $tab_content = $tab->getContent();
207
                    if ($tab_content instanceof FormInterface) {
208
                        $tab_model = $tab->getModel();
209
                        $set_id = $model_class == get_class($tab_model);
210
                        $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...
211
                        if (is_object($tab_model_section) && $tab_model_section instanceof SectionModelConfiguration) {
212
                            $set_id = $set_id && $tab->getContent()->getAction() == $tab_model_section->getUpdateUrl($id);
213
                        }
214
                        if ($set_id) {
215
                            $tab->setId($id);
216
                        }
217
                    }
218
                }
219
            }
220
        });
221
222
        return $this;
223
    }
224
225
    /**
226
     * @param \Illuminate\Http\Request $request
227
     * @param ModelConfigurationInterface $model
228
     *
229
     * @return void
230
     */
231
    public function validateForm(Request $request, ModelConfigurationInterface $model = null)
232
    {
233
        $this->getTabs()->each(function ($tab) use ($request, $model) {
234
            $tabId = $request->get('sleeping_owl_tab_id');
235
236
            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

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