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 HubphCommands 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 HubphCommands, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class HubphCommands extends \Robo\Tasks implements ConfigAwareInterface, LoggerAwareInterface |
||
21 | { |
||
22 | use ConfigAwareTrait; |
||
23 | use LoggerAwareTrait; |
||
24 | |||
25 | /** |
||
26 | * Report who we have authenticated as |
||
27 | * |
||
28 | * @command whoami |
||
29 | */ |
||
30 | public function whoami($options = ['as' => 'default']) |
||
38 | |||
39 | /** |
||
40 | * @command pr:close |
||
41 | */ |
||
42 | View Code Duplication | public function prClose($projectWithOrg = '', $number = '', $options = ['as' => 'default']) |
|
|
|||
43 | { |
||
44 | if (empty($number) && preg_match('#^[0-9]*$#', $projectWithOrg)) { |
||
45 | $number = $projectWithOrg; |
||
46 | $projectWithOrg = ''; |
||
47 | } |
||
48 | $projectWithOrg = $this->projectWithOrg($projectWithOrg); |
||
49 | list($org, $project) = explode('/', $projectWithOrg, 2); |
||
50 | |||
51 | $api = $this->api($options['as']); |
||
52 | $api->prClose($org, $project, $number); |
||
53 | } |
||
54 | |||
55 | /* |
||
56 | * hubph pr:check --vid=php-7.0./31 --vid=php-7.1./20 |
||
57 | * |
||
58 | * status 0 and csv with PR numbers to close |
||
59 | * |
||
60 | * - or - |
||
61 | * |
||
62 | * status 1 if all vid/vvals exist and nothing more needs to be done |
||
63 | */ |
||
64 | |||
65 | /** |
||
66 | * @command pr:check |
||
67 | */ |
||
68 | public function prCheck( |
||
99 | |||
100 | /** |
||
101 | * @command pr:create |
||
102 | * @aliases pull-request |
||
103 | */ |
||
104 | public function prCreate( |
||
148 | |||
149 | protected function getMessage($options) |
||
159 | |||
160 | protected function getVids($options, $message) |
||
179 | |||
180 | protected function projectWithOrg($projectWithOrg = '') |
||
188 | |||
189 | protected function getProjectWithOrgFromRemote($remote = 'origin', $cwd = '') |
||
195 | |||
196 | View Code Duplication | protected function getProjectWithOrfFromUrl($remote) |
|
204 | |||
205 | protected function getRemote($remote = 'origin', $cwd = '') |
||
212 | |||
213 | /** |
||
214 | * @command pr:find |
||
215 | * @param $projectWithOrg The project to work on, e.g. org/project |
||
216 | * @option $q Query term |
||
217 | * @filter-output |
||
218 | * @field-labels |
||
219 | * url: Url |
||
220 | * id: ID |
||
221 | * node_id: Node ID |
||
222 | * html_url: HTML Url |
||
223 | * diff_url: Diff Url |
||
224 | * patch_url: Patch Url |
||
225 | * issue_url: Issue Url |
||
226 | * number: Number |
||
227 | * state: State |
||
228 | * locked: Locked |
||
229 | * title: Title |
||
230 | * user: User |
||
231 | * body: Boday |
||
232 | * created_at: Created |
||
233 | * updated_at: Updated |
||
234 | * closed_at: Closed |
||
235 | * merged_at: Merged |
||
236 | * merge_commit_sha: Merge Commit |
||
237 | * assignee: Assignee |
||
238 | * assignees: Assignees |
||
239 | * requested_reviewers: Requested Reviewers |
||
240 | * requested_teams: Requested Teams |
||
241 | * labels: Labels |
||
242 | * milestone: Milestone |
||
243 | * commits_url: Commit Url |
||
244 | * review_comments_url: Review Comments Url |
||
245 | * review_comment_url: Review Comment Url |
||
246 | * comments_url: Comments Url |
||
247 | * statuses_url: Statuses Url |
||
248 | * head: Head |
||
249 | * base: Base |
||
250 | * _links: Links |
||
251 | * @default-fields number,user,title |
||
252 | * @default-string-field number |
||
253 | * @return Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
254 | */ |
||
255 | public function prFind($projectWithOrg = '', $options = ['as' => 'default', 'format' => 'yaml', 'q' => '']) |
||
274 | |||
275 | /** |
||
276 | * @command org:repos |
||
277 | * @param $org The org to list |
||
278 | * @filter-output |
||
279 | * @field-labels |
||
280 | * url: Url |
||
281 | * id: ID |
||
282 | * owner: Owner |
||
283 | * name: Shortname |
||
284 | * full_name: Name |
||
285 | * private: Private |
||
286 | * fork: Fork |
||
287 | * created_at: Created |
||
288 | * updated_at: Updated |
||
289 | * pushed_at: Pushed |
||
290 | * git_url: Git URL |
||
291 | * ssh_url: SSH URL |
||
292 | * svn_url: SVN URL |
||
293 | * homepage: Homepage |
||
294 | * size: Size |
||
295 | * stargazers_count: Stargazers |
||
296 | * watchers_count: Watchers |
||
297 | * language: Language |
||
298 | * has_issues: Has Issues |
||
299 | * has_projects: Has Projects |
||
300 | * has_downloads: Has Downloads |
||
301 | * has_wiki: Has Wiki |
||
302 | * has_pages: Has Pages |
||
303 | * forks_count: Forks |
||
304 | * archived: Archived |
||
305 | * disabled: Disabled |
||
306 | * open_issues_count: Open Issues |
||
307 | * default_branch: Default Branch |
||
308 | * license: License |
||
309 | * permissions: Permissions |
||
310 | * @default-fields full_name,language,default_branch |
||
311 | * @default-string-field full_name |
||
312 | * |
||
313 | * @return Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
314 | */ |
||
315 | View Code Duplication | public function orgRepos($org, $options = ['as' => 'default', 'format' => 'table']) |
|
328 | |||
329 | /** |
||
330 | * @command repo:info |
||
331 | * @param $projectWithOrg The project to work on, e.g. org/project |
||
332 | * @field-labels |
||
333 | * url: Url |
||
334 | * id: ID |
||
335 | * owner: Owner |
||
336 | * name: Shortname |
||
337 | * full_name: Name |
||
338 | * private: Private |
||
339 | * fork: Fork |
||
340 | * created_at: Created |
||
341 | * updated_at: Updated |
||
342 | * pushed_at: Pushed |
||
343 | * git_url: Git URL |
||
344 | * ssh_url: SSH URL |
||
345 | * svn_url: SVN URL |
||
346 | * homepage: Homepage |
||
347 | * size: Size |
||
348 | * stargazers_count: Stargazers |
||
349 | * watchers_count: Watchers |
||
350 | * language: Language |
||
351 | * has_issues: Has Issues |
||
352 | * has_projects: Has Projects |
||
353 | * has_downloads: Has Downloads |
||
354 | * has_wiki: Has Wiki |
||
355 | * has_pages: Has Pages |
||
356 | * forks_count: Forks |
||
357 | * archived: Archived |
||
358 | * disabled: Disabled |
||
359 | * open_issues_count: Open Issues |
||
360 | * default_branch: Default Branch |
||
361 | * license: License |
||
362 | * permissions: Permissions |
||
363 | * |
||
364 | * @return Consolidation\OutputFormatters\StructuredData\PropertyList |
||
365 | */ |
||
366 | View Code Duplication | public function repoInfo($projectWithOrg, $options = ['as' => 'default', 'format' => 'table']) |
|
380 | |||
381 | /** |
||
382 | * @command pr:show |
||
383 | * @field-labels |
||
384 | * url: Url |
||
385 | * id: ID |
||
386 | * node_id: Node ID |
||
387 | * html_url: HTML Url |
||
388 | * diff_url: Diff Url |
||
389 | * patch_url: Patch Url |
||
390 | * issue_url: Issue Url |
||
391 | * number: Number |
||
392 | * state: State |
||
393 | * locked: Locked |
||
394 | * title: Title |
||
395 | * user: User |
||
396 | * body: Boday |
||
397 | * created_at: Created |
||
398 | * updated_at: Updated |
||
399 | * closed_at: Closed |
||
400 | * mergeable: Mergeable |
||
401 | * mergeable_state: Mergable State |
||
402 | * merged_at: Merged |
||
403 | * merge_commit_sha: Merge Commit |
||
404 | * assignee: Assignee |
||
405 | * assignees: Assignees |
||
406 | * requested_reviewers: Requested Reviewers |
||
407 | * requested_teams: Requested Teams |
||
408 | * labels: Labels |
||
409 | * milestone: Milestone |
||
410 | * commits_url: Commit Url |
||
411 | * review_comments_url: Review Comments Url |
||
412 | * review_comment_url: Review Comment Url |
||
413 | * comments_url: Comments Url |
||
414 | * statuses_url: Statuses Url |
||
415 | * head: Head |
||
416 | * base: Base |
||
417 | * _links: Links |
||
418 | * @return Consolidation\OutputFormatters\StructuredData\PropertyList |
||
419 | */ |
||
420 | public function prShow($projectWithOrg = '', $number = '', $options = ['as' => 'default', 'format' => 'table']) |
||
438 | |||
439 | /** |
||
440 | * @command pr:statuses |
||
441 | * @field-labels |
||
442 | * url: Url |
||
443 | * id: ID |
||
444 | * state: State |
||
445 | * description: Description |
||
446 | * node_id: Node ID |
||
447 | * context: Context |
||
448 | * avatar_url: Avatar URL |
||
449 | * target_url: Target URL |
||
450 | * creator: Creator |
||
451 | * created_at: Created |
||
452 | * updated_at: Updated |
||
453 | * @default-fields id,creator,state,description |
||
454 | * @default-string-field description |
||
455 | * @return Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
456 | */ |
||
457 | View Code Duplication | public function prStatuses($projectWithOrg = '', $number = '', $options = ['as' => 'default', 'format' => 'yaml']) |
|
473 | |||
474 | /** |
||
475 | * @command pr:list |
||
476 | * @param $projectWithOrg The project to work on, e.g. org/project |
||
477 | * @filter-output |
||
478 | * @field-labels |
||
479 | * url: Url |
||
480 | * id: ID |
||
481 | * node_id: Node ID |
||
482 | * html_url: HTML Url |
||
483 | * diff_url: Diff Url |
||
484 | * patch_url: Patch Url |
||
485 | * issue_url: Issue Url |
||
486 | * number: Number |
||
487 | * state: State |
||
488 | * locked: Locked |
||
489 | * title: Title |
||
490 | * user: User |
||
491 | * body: Boday |
||
492 | * created_at: Created |
||
493 | * updated_at: Updated |
||
494 | * closed_at: Closed |
||
495 | * merged_at: Merged |
||
496 | * merge_commit_sha: Merge Commit |
||
497 | * assignee: Assignee |
||
498 | * assignees: Assignees |
||
499 | * requested_reviewers: Requested Reviewers |
||
500 | * requested_teams: Requested Teams |
||
501 | * labels: Labels |
||
502 | * milestone: Milestone |
||
503 | * commits_url: Commit Url |
||
504 | * review_comments_url: Review Comments Url |
||
505 | * review_comment_url: Review Comment Url |
||
506 | * comments_url: Comments Url |
||
507 | * statuses_url: Statuses Url |
||
508 | * head: Head |
||
509 | * base: Base |
||
510 | * _links: Links |
||
511 | * @default-fields number,user,title |
||
512 | * @default-string-field number |
||
513 | * @return Consolidation\OutputFormatters\StructuredData\RowsOfFields |
||
514 | */ |
||
515 | public function prList($projectWithOrg = '', $options = ['state' => 'open', 'as' => 'default', 'format' => 'table']) |
||
531 | |||
532 | protected function addTableRenderFunction($data) |
||
563 | |||
564 | protected function keyById($data, $field) |
||
578 | |||
579 | protected function api($as = 'default') |
||
586 | } |
||
587 |
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.