Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

TabPane   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 265
Duplicated Lines 6.04 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 7
dl 16
loc 265
ccs 79
cts 79
cp 1
rs 9.2
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A buildForm() 0 12 2
A bindRequest() 0 8 2
A persist() 0 6 2
A generateIdentifier() 0 4 1
A addTab() 0 15 6
A removeTab() 0 9 2
A removeTabByTitle() 0 13 3
A removeTabByPosition() 8 8 3
A getTabs() 0 4 1
A getTabByTitle() 0 10 3
A getTabByPosition() 8 8 3
A getActiveTab() 0 4 2
A getForm() 0 4 1
A getFormView() 0 8 2
A isValid() 0 4 1
A reindexTabs() 0 4 1
A getExtraParams() 0 9 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TabPane 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 TabPane, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
7
use Symfony\Component\Form\Extension\Core\Type\FormType;
8
use Symfony\Component\Form\Form;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * A tab pane is a container which holds tabs
16
 */
17
class TabPane
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $identifier;
23
24
    /**
25
     * @var TabInterface[]
26
     */
27
    protected $tabs = array();
28
29
    /**
30
     * @var string
31
     */
32
    protected $activeTab;
33
34
    /**
35
     * @var FormFactoryInterface
36
     */
37
    protected $formFactory;
38
39
    /**
40
     * @var Form
41
     */
42
    protected $form;
43
44
    /**
45
     * @var FormView
46
     */
47
    protected $formView;
48
49
    /**
50
     * @param string               $identifier  The identifier
51
     * @param Request              $request     The request
52
     * @param FormFactoryInterface $formFactory The form factory
53
     */
54 2
    public function __construct($identifier, Request $request, FormFactoryInterface $formFactory)
55
    {
56 2
        $this->identifier = $identifier;
57 2
        $this->formFactory = $formFactory;
58
59 2
        $this->slugifier = new Slugifier();
0 ignored issues
show
Bug introduced by
The property slugifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60 2
        if ($request->request->get('currenttab')) {
61 1
            $this->activeTab = $request->request->get('currenttab');
62 2
        } elseif ($request->get('currenttab')) {
63 1
            $this->activeTab = $request->get('currenttab');
64
        }
65 2
    }
66
67
    /**
68
     * @return FormInterface
69
     */
70 1
    public function buildForm()
71
    {
72 1
        $builder = $this->formFactory->createBuilder(FormType::class, null);
73
74 1
        foreach ($this->tabs as $tab) {
75 1
            $tab->buildForm($builder);
76
        }
77
78 1
        $this->form = $builder->getForm();
79
80 1
        return $this->form;
81
    }
82
83
    /**
84
     * @param Request $request
85
     */
86 1
    public function bindRequest(Request $request)
87
    {
88 1
        $this->form->handleRequest($request);
89
90 1
        foreach ($this->tabs as $tab) {
91 1
            $tab->bindRequest($request);
92
        }
93 1
    }
94
95
    /**
96
     * @param EntityManager $em The entity manager
97
     */
98 1
    public function persist(EntityManager $em)
99
    {
100 1
        foreach ($this->tabs as $tab) {
101 1
            $tab->persist($em);
102
        }
103 1
    }
104
105
    /**
106
     * @param TabInterface $tab
107
     *
108
     * @return string
109
     */
110 2
    private function generateIdentifier(TabInterface $tab)
111
    {
112 2
        return $this->slugifier->slugify($tab->getTitle());
113
    }
114
115
    /**
116
     * @param TabInterface $tab      The tab
117
     * @param null|int     $position The position
118
     *
119
     * @return TabPane
120
     */
121 2
    public function addTab(TabInterface $tab, $position = null)
122
    {
123 2
        $identifier = $tab->getIdentifier();
124 2
        if (!$identifier || empty($identifier)) {
125 2
            $tab->setIdentifier($this->generateIdentifier($tab));
126
        }
127
128 2
        if (!is_null($position) && is_numeric($position) && $position < count($this->tabs)) {
129 1
            array_splice($this->tabs, $position, 0, array($tab));
130
        } else {
131 2
            $this->tabs[] = $tab;
132
        }
133
134 2
        return $this;
135
    }
136
137
    /**
138
     * @param TabInterface $tab
139
     *
140
     * @return TabPane
141
     */
142 1
    public function removeTab(TabInterface $tab)
143
    {
144 1
        if (in_array($tab, $this->tabs)) {
145 1
            unset($this->tabs[array_search($tab, $this->tabs)]);
146 1
            $this->reindexTabs();
147
        }
148
149 1
        return $this;
150
    }
151
152
    /**
153
     * @param string $title
154
     *
155
     * @return TabPane
156
     */
157 1
    public function removeTabByTitle($title)
158
    {
159 1
        foreach ($this->tabs as $key => $tab) {
160 1
            if ($tab->getTitle() === $title) {
161 1
                unset($this->tabs[$key]);
162 1
                $this->reindexTabs();
163
164 1
                return $this;
165
            }
166
        }
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * @param int $position
173
     *
174
     * @return TabPane
175
     */
176 1 View Code Duplication
    public function removeTabByPosition($position)
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...
177
    {
178 1
        if (is_numeric($position) && $position < count($this->tabs)) {
179 1
            array_splice($this->tabs, $position, 1);
180
        }
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * @return TabInterface[]
187
     */
188 2
    public function getTabs()
189
    {
190 2
        return $this->tabs;
191
    }
192
193
    /**
194
     * @param string $title
195
     *
196
     * @return TabInterface|null
197
     */
198 1
    public function getTabByTitle($title)
199
    {
200 1
        foreach ($this->tabs as $key => $tab) {
201 1
            if ($tab->getTitle() === $title) {
202 1
                return $this->tabs[$key];
203
            }
204
        }
205
206 1
        return null;
207
    }
208
209
    /**
210
     * @param int $position
211
     *
212
     * @return TabInterface|null
213
     */
214 1 View Code Duplication
    public function getTabByPosition($position)
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...
215
    {
216 1
        if (is_numeric($position) && $position < count($this->tabs)) {
217 1
            return $this->tabs[$position];
218
        }
219
220 1
        return null;
221
    }
222
223
    /**
224
     * @return string
225
     */
226 1
    public function getActiveTab()
227
    {
228 1
        return !empty($this->activeTab) ? $this->activeTab : $this->tabs[0]->getIdentifier();
229
    }
230
231
    /**
232
     * @return Form
233
     */
234 1
    public function getForm()
235
    {
236 1
        return $this->form;
237
    }
238
239
    /**
240
     * @return FormView
241
     */
242 1
    public function getFormView()
243
    {
244 1
        if (is_null($this->formView)) {
245 1
            $this->formView = $this->form->createView();
246
        }
247
248 1
        return $this->formView;
249
    }
250
251
    /**
252
     * @return bool
253
     */
254 1
    public function isValid()
255
    {
256 1
        return $this->form->isValid();
257
    }
258
259
    /**
260
     * Reset the indexes of the tabs
261
     */
262 1
    private function reindexTabs()
263
    {
264 1
        $this->tabs = array_values($this->tabs);
265 1
    }
266
267
    /**
268
     * @param Request $request
269
     *
270
     * @return array
271
     */
272 1
    public function getExtraParams(Request $request)
273
    {
274 1
        $extraParams = array();
275 1
        foreach ($this->getTabs() as $tab) {
276 1
            $extraParams = array_merge($extraParams, $tab->getExtraParams($request));
277
        }
278
279 1
        return $extraParams;
280
    }
281
}
282