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 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 |
||
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 | /** @var Slugifier */ |
||
50 | private $slugifier; |
||
51 | |||
52 | /** |
||
53 | * @param string $identifier The identifier |
||
54 | * @param Request $request The request |
||
55 | * @param FormFactoryInterface $formFactory The form factory |
||
56 | */ |
||
57 | 2 | public function __construct($identifier, Request $request, FormFactoryInterface $formFactory) |
|
69 | |||
70 | /** |
||
71 | * @return FormInterface |
||
72 | */ |
||
73 | 1 | public function buildForm() |
|
85 | |||
86 | /** |
||
87 | * @param Request $request |
||
88 | */ |
||
89 | 1 | public function bindRequest(Request $request) |
|
97 | |||
98 | /** |
||
99 | * @param EntityManager $em The entity manager |
||
100 | */ |
||
101 | 1 | public function persist(EntityManager $em) |
|
107 | |||
108 | /** |
||
109 | * @param TabInterface $tab |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 2 | private function generateIdentifier(TabInterface $tab) |
|
117 | |||
118 | /** |
||
119 | * @param TabInterface $tab The tab |
||
120 | * @param int|null $position The position |
||
121 | * |
||
122 | * @return TabPane |
||
123 | */ |
||
124 | 2 | public function addTab(TabInterface $tab, $position = null) |
|
139 | |||
140 | /** |
||
141 | * @param TabInterface $tab |
||
142 | * |
||
143 | * @return TabPane |
||
144 | */ |
||
145 | 1 | public function removeTab(TabInterface $tab) |
|
154 | |||
155 | /** |
||
156 | * @param string $title |
||
157 | * |
||
158 | * @return TabPane |
||
159 | */ |
||
160 | 1 | public function removeTabByTitle($title) |
|
173 | |||
174 | /** |
||
175 | * @param int $position |
||
176 | * |
||
177 | * @return TabPane |
||
178 | */ |
||
179 | 1 | View Code Duplication | public function removeTabByPosition($position) |
187 | |||
188 | /** |
||
189 | * @return TabInterface[] |
||
190 | */ |
||
191 | 2 | public function getTabs() |
|
195 | |||
196 | /** |
||
197 | * @param string $title |
||
198 | * |
||
199 | * @return TabInterface|null |
||
200 | */ |
||
201 | 1 | public function getTabByTitle($title) |
|
211 | |||
212 | /** |
||
213 | * @param int $position |
||
214 | * |
||
215 | * @return TabInterface|null |
||
216 | */ |
||
217 | 1 | View Code Duplication | public function getTabByPosition($position) |
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | 1 | public function getActiveTab() |
|
233 | |||
234 | /** |
||
235 | * @return Form |
||
236 | */ |
||
237 | 1 | public function getForm() |
|
241 | |||
242 | /** |
||
243 | * @return FormView |
||
244 | */ |
||
245 | 1 | public function getFormView() |
|
253 | |||
254 | /** |
||
255 | * @return bool |
||
256 | */ |
||
257 | 1 | public function isValid() |
|
261 | |||
262 | /** |
||
263 | * Reset the indexes of the tabs |
||
264 | */ |
||
265 | 1 | private function reindexTabs() |
|
269 | |||
270 | /** |
||
271 | * @param Request $request |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | 1 | public function getExtraParams(Request $request) |
|
284 | } |
||
285 |
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.