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 GithubMilestoneConverterSpec 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 GithubMilestoneConverterSpec, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class GithubMilestoneConverterSpec extends ObjectBehavior |
||
11 | { |
||
12 | public function it_is_initializable() |
||
16 | |||
17 | public function let(GithubRepo $githubRepo, GithubRepoId $repoId) |
||
22 | |||
23 | /** |
||
24 | * @dataProvider provideAllMilestones |
||
25 | */ |
||
26 | public function it_returns_github_milestone_source_as_result($arrayData) |
||
31 | |||
32 | /** |
||
33 | * @dataProvider provideAllMilestones |
||
34 | */ |
||
35 | public function it_will_have_milestone_id_in_converted_result($arrayData) |
||
42 | |||
43 | /** |
||
44 | * @dataProvider provideAllMilestones |
||
45 | */ |
||
46 | public function it_will_have_repo_id_in_converted_result($arrayData) |
||
53 | |||
54 | /** |
||
55 | * @dataProvider provideAllMilestones |
||
56 | */ |
||
57 | public function it_will_have_repo_in_converted_result($arrayData) |
||
64 | |||
65 | /** |
||
66 | * @dataProvider provideAllMilestones |
||
67 | */ |
||
68 | public function it_will_have_milestone_number_in_converted_result($arrayData) |
||
73 | |||
74 | /** |
||
75 | * @dataProvider provideAllMilestones |
||
76 | */ |
||
77 | public function it_will_have_milestone_state_in_converted_result($arrayData) |
||
83 | |||
84 | /** |
||
85 | * @dataProvider provideOpenMilestones |
||
86 | */ |
||
87 | public function it_will_have_open_milestone_state_in_converted_result($arrayData) |
||
93 | |||
94 | /** |
||
95 | * @dataProvider provideClosedMilestones |
||
96 | */ |
||
97 | public function it_will_have_closed_milestone_state_in_converted_result($arrayData) |
||
103 | |||
104 | /** |
||
105 | * @dataProvider provideAllMilestones |
||
106 | */ |
||
107 | public function it_will_have_milestone_title_in_converted_result($arrayData) |
||
112 | |||
113 | /** |
||
114 | * @dataProvider provideAllMilestones |
||
115 | */ |
||
116 | public function it_will_have_milestone_description_in_converted_result($arrayData) |
||
121 | |||
122 | /** |
||
123 | * @dataProvider provideMilestonesWithCreator |
||
124 | */ |
||
125 | public function it_will_have_user_id_of_creator_in_converted_result($arrayData) |
||
132 | |||
133 | /** |
||
134 | * @dataProvider provideMilestonesWithCreator |
||
135 | */ |
||
136 | public function it_will_have_creator_in_converted_result($arrayData) |
||
143 | |||
144 | /** |
||
145 | * @dataProvider provideMilestonesWithOutCreator |
||
146 | */ |
||
147 | public function it_will_have_null_for_user_id_of_creator_in_converted_result($arrayData) |
||
153 | |||
154 | /** |
||
155 | * @dataProvider provideMilestonesWithOutCreator |
||
156 | */ |
||
157 | public function it_will_have_null_for_creator_in_converted_result($arrayData) |
||
163 | |||
164 | /** |
||
165 | * @dataProvider provideAllMilestones |
||
166 | */ |
||
167 | public function it_will_have_open_issue_count_in_converted_result($arrayData) |
||
172 | |||
173 | /** |
||
174 | * @dataProvider provideAllMilestones |
||
175 | */ |
||
176 | public function it_will_have_closed_issue_count_in_converted_result($arrayData) |
||
181 | |||
182 | /** |
||
183 | * @dataProvider provideMilestonesWithDueDate |
||
184 | */ |
||
185 | public function it_will_have_due_date_in_converted_result($arrayData) |
||
194 | |||
195 | /** |
||
196 | * @dataProvider provideMilestonesWithOutDueDate |
||
197 | */ |
||
198 | public function it_will_have_null_if_no_due_date_in_converted_result($arrayData) |
||
206 | |||
207 | /** |
||
208 | * @dataProvider provideAllMilestones |
||
209 | */ |
||
210 | View Code Duplication | public function it_will_have_github_created_datetime_in_converted_result($arrayData) |
|
217 | |||
218 | /** |
||
219 | * @dataProvider provideAllMilestones |
||
220 | */ |
||
221 | View Code Duplication | public function it_will_have_github_last_updated_datetime_in_converted_result($arrayData) |
|
228 | |||
229 | /** |
||
230 | * @dataProvider provideClosedMilestones |
||
231 | */ |
||
232 | View Code Duplication | public function it_will_have_github_closed_datetime_for_closed_milestones_in_converted_result($arrayData) |
|
239 | |||
240 | /** |
||
241 | * @dataProvider provideOpenMilestones |
||
242 | */ |
||
243 | public function it_will_have_null_for_github_closed_datetime_on_open_milestones_in_converted_result($arrayData) |
||
249 | |||
250 | public function provideAllMilestones() |
||
260 | |||
261 | public function provideOpenMilestones() |
||
273 | |||
274 | public function provideClosedMilestones() |
||
286 | |||
287 | public function provideMilestonesWithDueDate() |
||
299 | |||
300 | public function provideMilestonesWithOutDueDate() |
||
312 | |||
313 | public function provideMilestonesWithCreator() |
||
325 | |||
326 | public function provideMilestonesWithOutCreator() |
||
338 | |||
339 | protected function getDataProvider() |
||
343 | } |
||
344 |
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.