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 HubphAPI 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 HubphAPI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class HubphAPI |
||
9 | { |
||
10 | protected $config; |
||
11 | protected $token; |
||
12 | protected $gitHubAPI; |
||
13 | protected $eventLogger; |
||
14 | protected $as = 'default'; |
||
15 | |||
16 | /** |
||
17 | * HubphAPI constructor |
||
18 | */ |
||
19 | public function __construct(ConfigInterface $config) |
||
23 | |||
24 | public function startLogging($filename) |
||
30 | |||
31 | public function stopLogging() |
||
38 | |||
39 | public function setAs($as) |
||
47 | |||
48 | public function whoami() |
||
54 | |||
55 | public function repoCreate($org, $project) |
||
65 | |||
66 | public function repoDelete($org, $project) |
||
70 | |||
71 | public function prCreate($org, $project, $title, $body, $base, $head) |
||
83 | |||
84 | public function prClose($org, $project, PullRequests $prs) |
||
91 | |||
92 | public function prMerge($org, $project, PullRequests $prs, $message, $mergeMethod = 'squash', $title = null) |
||
114 | |||
115 | /** |
||
116 | * prCheck determines whether there are any open PRs that already exist |
||
117 | * that satisfy any of the provided $vids. |
||
118 | * |
||
119 | * @param string $projectWithOrg org/project to check |
||
120 | * @param VersionIdentifiers $vids |
||
121 | * @return [int $status, PullRequests $prs] status of PRs, and a list of PR numbers |
||
122 | * - If $status is 0, then the caller should go ahead and create a new PR. |
||
123 | * The existing pull requests that would be superceded by the new PR are |
||
124 | * returned in the second parameter. These PRs could all be closed. |
||
125 | * - If $status is >0, then there is no need to create a new PR, as there |
||
126 | * are already existing PRs that are equivalent to the one that would |
||
127 | * be open. The equivalent PRs are returned in the second parameter. |
||
128 | */ |
||
129 | public function prCheck($projectWithOrg, VersionIdentifiers $vids) |
||
140 | |||
141 | public function prStatuses($projectWithOrg, $number) |
||
167 | |||
168 | public function addTokenAuthentication($url) |
||
180 | |||
181 | View Code Duplication | protected function projectAndOrgFromUrl($remote) |
|
189 | |||
190 | protected function existingPRs($projectWithOrg, VersionIdentifiers $vids) |
||
194 | |||
195 | public function matchingPRs($projectWithOrg, $preamble, $pattern = '') |
||
205 | |||
206 | public function allPRs($projectWithOrg) |
||
215 | |||
216 | /** |
||
217 | * Pass an event of note to the event logger |
||
218 | * @param string $event_name |
||
219 | * @param array $args |
||
220 | * @param array $params |
||
221 | * @param array $response |
||
222 | */ |
||
223 | protected function logEvent($event_name, $args, $params, $response) |
||
229 | |||
230 | /** |
||
231 | * Authenticate and then return the gitHub API object. |
||
232 | */ |
||
233 | public function gitHubAPI() |
||
243 | |||
244 | /** |
||
245 | * Return a result pager object using our cached GitHub API client. |
||
246 | */ |
||
247 | public function resultPager() |
||
251 | |||
252 | /** |
||
253 | * Look up the GitHub token set either via environment variable or in the |
||
254 | * auth-token cache directory. |
||
255 | */ |
||
256 | public function gitHubToken() |
||
263 | |||
264 | protected function getGitHubToken() |
||
296 | |||
297 | protected function getConfig() |
||
301 | } |
||
302 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: