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 GherkinToDusk 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 GherkinToDusk, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class GherkinToDusk extends BaseGherkinToDusk |
||
22 | { |
||
23 | use BuildOutContent; |
||
24 | |||
25 | protected $component = false; |
||
26 | |||
27 | protected $string_contents = null; |
||
28 | |||
29 | /** |
||
30 | * Yml Content of a test yml |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $feature_content; |
||
34 | |||
35 | /** |
||
36 | * @var \Behat\Gherkin\Node\FeatureNode |
||
37 | */ |
||
38 | protected $parsed_feature; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | */ |
||
43 | protected $dusk_class_and_methods; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * @var WritePHPUnitFile |
||
48 | */ |
||
49 | protected $write_unit_test; |
||
50 | |||
51 | /** |
||
52 | * @var AppendFile |
||
53 | */ |
||
54 | protected $append_unit_test; |
||
55 | |||
56 | /** |
||
57 | * @var WriteBrowserFile |
||
58 | */ |
||
59 | protected $write_browser_test; |
||
60 | |||
61 | /** |
||
62 | * @var \GD\Helpers\AppendBrowserFile |
||
63 | */ |
||
64 | protected $append_browser_test; |
||
65 | |||
66 | 24 | View Code Duplication | public function appendFeatures() |
87 | |||
88 | 27 | View Code Duplication | public function initializeFeature() |
112 | |||
113 | 6 | protected function featureToBrowser() |
|
123 | |||
124 | 12 | protected function featureAppendToBrowser() |
|
132 | |||
133 | 12 | protected function featureAppendToUnit() |
|
141 | |||
142 | 21 | protected function featureToUnit() |
|
152 | |||
153 | |||
154 | /** |
||
155 | * @return boolean |
||
156 | */ |
||
157 | public function isComponent() |
||
161 | |||
162 | /** |
||
163 | * @param boolean $component |
||
164 | */ |
||
165 | public function setComponent($component) |
||
169 | |||
170 | /** |
||
171 | * @return mixed |
||
172 | */ |
||
173 | 3 | public function getFeatureContent() |
|
177 | |||
178 | /** |
||
179 | * @param mixed $feature_content |
||
180 | */ |
||
181 | public function setFeatureContent($feature_content) |
||
185 | |||
186 | 51 | private function loadFileContent() |
|
191 | |||
192 | 51 | private function passThroughParser() |
|
196 | |||
197 | /** |
||
198 | * @return \Behat\Gherkin\Node\FeatureNode |
||
199 | */ |
||
200 | 3 | public function getParsedFeature() |
|
204 | |||
205 | /** |
||
206 | * @param \Behat\Gherkin\Node\FeatureNode $parsed_feature |
||
207 | */ |
||
208 | public function setParsedFeature($parsed_feature) |
||
212 | |||
213 | 51 | private function breakIntoMethods() |
|
217 | |||
218 | 51 | private function iterateOverScenariosAndBuildUpClassMethods() |
|
234 | |||
235 | /** |
||
236 | * @param $scenario \Behat\Gherkin\Node\ScenarioNode |
||
237 | */ |
||
238 | 51 | protected function buildOutSteps($scenario, $scenario_index) |
|
249 | |||
250 | |||
251 | |||
252 | /** |
||
253 | * @return mixed |
||
254 | */ |
||
255 | 48 | public function getDuskClassAndMethods() |
|
259 | |||
260 | /** |
||
261 | * @param mixed $dusk_class_and_methods |
||
262 | */ |
||
263 | public function setDuskClassAndMethods($dusk_class_and_methods) |
||
267 | |||
268 | |||
269 | |||
270 | 6 | public function getWriteBrowserTest() |
|
279 | |||
280 | /** |
||
281 | * @param null $write_browser_test |
||
282 | * @return GherkinToDusk |
||
283 | * @internal param WritePHPUnitFile $write_unit_test |
||
284 | */ |
||
285 | 6 | public function setWriteBrowserTest($write_browser_test = null) |
|
294 | |||
295 | |||
296 | 18 | public function getWriteUnitTest() |
|
305 | |||
306 | /** |
||
307 | * @param WritePHPUnitFile $write_unit_test |
||
308 | * @return GherkinToDusk |
||
309 | */ |
||
310 | 18 | public function setWriteUnitTest($write_unit_test = null) |
|
319 | |||
320 | /** |
||
321 | * @return $append_unit_test AppendFile |
||
322 | */ |
||
323 | 12 | public function getAppendUnitTest() |
|
332 | |||
333 | /** |
||
334 | * @param AppendFile $append_unit_test |
||
335 | * @return GherkinToDusk |
||
336 | */ |
||
337 | 12 | public function setAppendUnitTest($append_unit_test = null) |
|
346 | |||
347 | 27 | private function checkIfFileExists() |
|
355 | |||
356 | /** |
||
357 | * @return Helpers\AppendBrowserFile |
||
358 | */ |
||
359 | 12 | public function getAppendBrowserTest() |
|
367 | |||
368 | /** |
||
369 | * @param Helpers\AppendBrowserFile $append_browser_test |
||
370 | * @return $this |
||
371 | */ |
||
372 | 12 | public function setAppendBrowserTest($append_browser_test = null) |
|
381 | } |
||
382 |
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.