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
Branch master (9e3162)
by Dave
63:34
created

DisplayTabbed   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 266
ccs 0
cts 105
cp 0
rs 9.1199
c 0
b 0
f 0
wmc 41

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getTabs() 0 3 1
A __construct() 0 6 3
A getModel() 0 5 3
A appendTab() 0 7 1
A setTabs() 0 7 2
A getValue() 0 2 1
A setId() 0 9 2
A validateForm() 0 7 3
A toArray() 0 4 1
A isReadonly() 0 3 1
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

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\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) || is_callable($tabs)) {
37
            $this->setTabs($tabs);
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 && $firstTab = $this->getTabs()->first()) {
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)
0 ignored issues
show
Bug introduced by
The method tab() does not exist on Illuminate\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

157
            $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...
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
    public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null)
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
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

218
            if ($tab instanceof FormInterface && $tab->/** @scrutinizer ignore-call */ getName() == $tabId) {
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
    public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null)
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