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 | /** |
||
50 | * @param string $identifier The identifier |
||
51 | * @param Request $request The request |
||
52 | * @param FormFactoryInterface $formFactory The form factory |
||
53 | */ |
||
54 | public function __construct($identifier, Request $request, FormFactoryInterface $formFactory) |
||
66 | |||
67 | /** |
||
68 | * @return FormInterface |
||
69 | */ |
||
70 | public function buildForm() |
||
82 | |||
83 | /** |
||
84 | * @param Request $request |
||
85 | */ |
||
86 | public function bindRequest(Request $request) |
||
94 | |||
95 | /** |
||
96 | * @param EntityManager $em The entity manager |
||
97 | */ |
||
98 | public function persist(EntityManager $em) |
||
104 | |||
105 | /** |
||
106 | * @param TabInterface $tab |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | private function generateIdentifier(TabInterface $tab) |
||
114 | |||
115 | /** |
||
116 | * @param TabInterface $tab The tab |
||
117 | * @param null|int $position The position |
||
118 | * |
||
119 | * @return TabPane |
||
120 | */ |
||
121 | public function addTab(TabInterface $tab, $position = null) |
||
136 | |||
137 | /** |
||
138 | * @param TabInterface $tab |
||
139 | * |
||
140 | * @return TabPane |
||
141 | */ |
||
142 | public function removeTab(TabInterface $tab) |
||
151 | |||
152 | /** |
||
153 | * @param string $title |
||
154 | * |
||
155 | * @return TabPane |
||
156 | */ |
||
157 | public function removeTabByTitle($title) |
||
170 | |||
171 | /** |
||
172 | * @param int $position |
||
173 | * |
||
174 | * @return TabPane |
||
175 | */ |
||
176 | View Code Duplication | public function removeTabByPosition($position) |
|
184 | |||
185 | /** |
||
186 | * @return TabInterface[] |
||
187 | */ |
||
188 | public function getTabs() |
||
192 | |||
193 | /** |
||
194 | * @param string $title |
||
195 | * |
||
196 | * @return TabInterface|null |
||
197 | */ |
||
198 | public function getTabByTitle($title) |
||
208 | |||
209 | /** |
||
210 | * @param int $position |
||
211 | * |
||
212 | * @return TabInterface|null |
||
213 | */ |
||
214 | View Code Duplication | public function getTabByPosition($position) |
|
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getActiveTab() |
||
230 | |||
231 | /** |
||
232 | * @return Form |
||
233 | */ |
||
234 | public function getForm() |
||
238 | |||
239 | /** |
||
240 | * @return FormView |
||
241 | */ |
||
242 | public function getFormView() |
||
250 | |||
251 | /** |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function isValid() |
||
258 | |||
259 | /** |
||
260 | * Reset the indexes of the tabs |
||
261 | */ |
||
262 | private function reindexTabs() |
||
266 | |||
267 | /** |
||
268 | * @param Request $request |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | public function getExtraParams(Request $request) |
||
281 | } |
||
282 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: