Complex classes like Github 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 Github, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Github implements ProviderInterface |
||
| 16 | { |
||
| 17 | public function tplUser($obj) |
||
| 18 | { |
||
| 19 | return [ |
||
| 20 | 'provider_id' => $obj->id, |
||
| 21 | 'provider' => 'github', |
||
| 22 | 'username' => isset($obj->login) ? $obj->login : $obj->nickname, |
||
| 23 | 'name' => isset($obj->name) ? $obj->name : null, |
||
| 24 | 'token' => isset($obj->token) ? $obj->token : null, |
||
| 25 | 'avatar' => isset($obj->user['avatar_url']) ? $obj->user['avatar_url'] : $obj->avatar_url, |
||
| 26 | 'html_url' => isset($obj->user['html_url']) ? $obj->user['html_url'] : $obj->html_url, |
||
| 27 | 'bio' => isset($obj->user['bio']) ? $obj->user['bio'] : null, |
||
| 28 | 'since' => isset($obj->user['created_at']) ? Carbon::parse($obj->user['created_at'])->toDateTimeString() : Carbon::now(), |
||
| 29 | 'location' => isset($obj->user['location']) ? $obj->user['location'] : null, |
||
| 30 | 'blog' => isset($obj->user['blog']) ? $obj->user['blog'] : null, |
||
| 31 | 'email' => isset($obj->email) ? $obj->email : null, |
||
| 32 | ]; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function tplRepository($repo, $slug = false) |
||
| 57 | |||
| 58 | public function tplIssue($obj, $productBracklogId) |
||
| 59 | { |
||
| 60 | if (isset($obj->user->login)) { |
||
| 61 | $user = User::where('username', $obj->user->login) |
||
| 62 | ->where('provider', 'github')->first(); |
||
| 63 | } |
||
| 64 | |||
| 65 | return [ |
||
| 66 | 'provider_id' => $obj->id, |
||
| 67 | 'user_id' => isset($user->id) ? $user->id : Auth::user()->id, |
||
| 68 | 'product_backlog_id' => $productBracklogId, |
||
| 69 | 'effort' => 0, |
||
| 70 | 'config_issue_effort_id' => 1, |
||
| 71 | 'issue_type_id' => 1, |
||
| 72 | 'number' => $obj->number, |
||
| 73 | 'title' => $obj->title, |
||
| 74 | 'description' => $obj->body, |
||
| 75 | 'state' => $obj->state, |
||
| 76 | 'html_url' => $obj->html_url, |
||
| 77 | 'created_at' => $obj->created_at, |
||
| 78 | 'updated_at' => $obj->updated_at, |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function tplOrganization($obj) |
||
| 83 | { |
||
| 84 | return [ |
||
| 85 | 'provider_id' => $obj->id, |
||
| 86 | 'username' => $obj->login, |
||
| 87 | 'url' => isset($obj->url) ? $obj->url : null, |
||
| 88 | 'repos_url' => isset($obj->repos_url) ? $obj->repos_url : null, |
||
| 89 | 'events_url' => isset($obj->events_url) ? $obj->events_url : null, |
||
| 90 | 'hooks_url' => isset($obj->hooks_url) ? $obj->hooks_url : null, |
||
| 91 | 'issues_url' => isset($obj->issues_url) ? $obj->issues_url : null, |
||
| 92 | 'members_url' => isset($obj->members_url) ? $obj->members_url : null, |
||
| 93 | 'public_members_url' => isset($obj->public_members_url) ? $obj->public_members_url : null, |
||
| 94 | 'avatar_url' => isset($obj->avatar_url) ? $obj->avatar_url : null, |
||
| 95 | 'description' => isset($obj->description) ? $obj->description : null, |
||
| 96 | 'title' => isset($obj->name) ? $obj->name : null, |
||
| 97 | 'blog' => isset($obj->blog) ? $obj->blog : null, |
||
| 98 | 'location' => isset($obj->location) ? $obj->location : null, |
||
| 99 | 'email' => isset($obj->email) ? $obj->email : null, |
||
| 100 | 'public_repos' => isset($obj->public_repos) ? $obj->public_repos : null, |
||
| 101 | 'html_url' => isset($obj->html_url) ? $obj->html_url : null, |
||
| 102 | 'total_private_repos' => isset($obj->total_private_repos) ? $obj->total_private_repos : null, |
||
| 103 | 'since' => Carbon::parse((isset($obj->created_at) ? $obj->created_at : Carbon::now()))->toDateTimeString(), |
||
| 104 | 'disk_usage' => isset($obj->disk_usage) ? $obj->disk_usage : null, |
||
| 105 | ]; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function readRepositories($page = 1, &$repos = null) |
||
| 109 | { |
||
| 110 | $response = collect(Helper::request('https://api.github.com/user/repos?page='. $page))->map(function ($repo) { |
||
| 111 | return $this->tplRepository($repo); |
||
| 112 | }); |
||
| 113 | |||
| 114 | if (is_null($repos)) { |
||
| 115 | $repos = collect(); |
||
| 116 | } |
||
| 117 | |||
| 118 | $repos->push($response); |
||
| 119 | |||
| 120 | if ($response->count() == 30) { |
||
| 121 | $this->readRepositories(++$page, $repos); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $repos->flatten(1)->sortBy('title'); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function createOrUpdateRepository($owner, $obj, $oldTitle = null) |
||
| 128 | { |
||
| 129 | $params = [ |
||
| 130 | 'name' => str_slug($obj->title, '-'), |
||
| 131 | 'description' => $obj->description, |
||
| 132 | ]; |
||
| 133 | |||
| 134 | if (is_null($oldTitle)) { |
||
| 135 | $endpoint = 'https://api.github.com/orgs/'.$owner.'/repos'; |
||
| 136 | |||
| 137 | if (Auth::user()->username == $owner) { |
||
| 138 | $endpoint = 'https://api.github.com/user/repos'; |
||
| 139 | } |
||
| 140 | |||
| 141 | $response = Helper::request($endpoint, true, 'POST', $params); |
||
| 142 | } else { |
||
| 143 | $oldTitle = str_slug($oldTitle, '-'); |
||
| 144 | $response = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$oldTitle, true, 'POST', $params); |
||
| 145 | } |
||
| 146 | |||
| 147 | return (object) $response; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function organization($login) |
||
| 151 | { |
||
| 152 | $organization = Organization::where('username', $login) |
||
| 153 | ->where('provider', 'github')->first(); |
||
| 154 | |||
| 155 | if (!isset($organization)) { |
||
| 156 | $response = Helper::request('https://api.github.com/orgs/'.$login); |
||
| 157 | |||
| 158 | if (!isset($response->id)) { |
||
| 159 | $response = Helper::request('https://api.github.com/users/'.$login); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (isset($response->id)) { |
||
| 163 | $organization = Organization::create($this->tplOrganization($response)); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | if (is_null($organization->users()->where('users_has_organizations.user_id', Auth::id()) |
||
| 168 | ->where('users_has_organizations.organization_id', $organization->id)->first())) { |
||
| 169 | $organization->users()->attach(Auth::id()); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $organization->id; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function readCollaborators($owner, $repo, $providerId = null) |
||
| 176 | { |
||
| 177 | $ids = collect(); |
||
| 178 | |||
| 179 | collect(Helper::request('https://api.github.com/repos/'.$owner.'/'.$repo.'/collaborators')) |
||
| 180 | ->map(function ($collaborator) use ($ids) { |
||
| 181 | $user = User::where('username', $collaborator->login) |
||
| 182 | ->where('provider', 'github')->first(); |
||
| 183 | |||
| 184 | if (!isset($user)) { |
||
| 185 | $user = User::create($this->tplUser($collaborator)); |
||
| 186 | } |
||
| 187 | |||
| 188 | $ids->push($user->id); |
||
| 189 | }); |
||
| 190 | |||
| 191 | $organization = Organization::where('username', $owner) |
||
| 192 | ->where('provider', 'github')->first()->users(); |
||
| 193 | |||
| 194 | $organization->syncWithoutDetaching($ids->diff($organization->pluck('user_id')->toArray())); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function createBranches($owner, $productBacklogId, $repo, $providerId = null, $page = 1) |
||
| 214 | |||
| 215 | public function readIssues($productBacklogId = null) |
||
| 216 | { |
||
| 217 | if (is_null($productBacklogId)) { |
||
| 218 | $productBacklog = ProductBacklog::all(); |
||
| 219 | } else { |
||
| 220 | $productBacklog = ProductBacklog::find($productBacklogId); |
||
| 234 | |||
| 235 | public function createOrUpdateIssue($obj) |
||
| 249 | |||
| 250 | public function createOrUpdateIssueComment($obj, $verb = 'POST') |
||
| 264 | |||
| 265 | public function deleteIssueComment($obj) |
||
| 269 | } |
||
| 270 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.