Complex classes like Gitlab 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 Gitlab, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Gitlab implements ProviderInterface |
||
| 15 | { |
||
| 16 | private $gitlabGroups; |
||
| 17 | |||
| 18 | public function tplUser($obj) |
||
| 35 | |||
| 36 | public function tplRepository($repo, $slug = false) |
||
| 64 | |||
| 65 | public function tplIssue($obj, $productBacklogId) |
||
| 88 | |||
| 89 | public function tplOrganization($obj) |
||
| 114 | |||
| 115 | public function readRepositories($page = 1, &$repos = null) |
||
| 125 | |||
| 126 | public function createOrUpdateRepository($owner, $obj, $oldTitle = null) |
||
| 129 | |||
| 130 | public function organization($obj) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get all members from a specific group in gitlab. |
||
| 169 | * |
||
| 170 | * @param $group |
||
| 171 | * |
||
| 172 | * @return \Illuminate\Support\Collection |
||
| 173 | */ |
||
| 174 | private function getGroupsMembers($group) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get all members from the project in gitlab. |
||
| 183 | * |
||
| 184 | * @param $projectId |
||
| 185 | * |
||
| 186 | * @return \Illuminate\Support\Collection |
||
| 187 | */ |
||
| 188 | private function getProjectMembers($projectId) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * A project can be shared with many groups and each group has its members |
||
| 197 | * This method retrieves all members from the groups that the project is shared with. |
||
| 198 | * |
||
| 199 | * @param $projectId |
||
| 200 | * |
||
| 201 | * @return \Illuminate\Support\Collection|static |
||
| 202 | */ |
||
| 203 | private function getProjectSharedGroupsMembers($projectId) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Retrives all project members from three pespectives |
||
| 222 | * Members from the project itself |
||
| 223 | * Members of the groups that the project is owned by |
||
| 224 | * Members by the groups that the project is shared with. |
||
| 225 | * |
||
| 226 | * @param $owner |
||
| 227 | * @param $repo |
||
| 228 | * @param null $providerId |
||
| 229 | */ |
||
| 230 | public function readCollaborators($owner, $repo, $providerId = null) |
||
| 231 | { |
||
| 232 | $collaborators = $this->getGroupsMembers($owner); |
||
| 233 | |||
| 234 | if ($providerId) { |
||
| 235 | $projectMembers = $this->getProjectMembers($providerId); |
||
| 236 | $collaborators = $collaborators->merge($projectMembers); |
||
| 237 | |||
| 238 | $projectSharedGroupsMembers = $this->getProjectSharedGroupsMembers($providerId); |
||
| 239 | $collaborators = $collaborators->merge($projectSharedGroupsMembers); |
||
| 240 | } |
||
| 241 | |||
| 242 | foreach ($collaborators as $collaborator) { |
||
| 243 | if (isset($collaborator->id)) { |
||
| 244 | $data = [ |
||
| 245 | 'provider_id' => $collaborator->id, |
||
| 246 | 'provider' => 'gitlab', |
||
| 247 | 'username' => $collaborator->username, |
||
| 248 | 'name' => $collaborator->name, |
||
| 249 | 'avatar' => $collaborator->avatar_url, |
||
| 250 | 'html_url' => $collaborator->web_url, |
||
| 251 | 'email' => null, |
||
| 252 | 'remember_token' => null, |
||
| 253 | 'bio' => null, |
||
| 254 | 'location' => null, |
||
| 255 | 'blog' => null, |
||
| 256 | 'since' => null, |
||
| 257 | 'token' => null, |
||
| 258 | 'position_held' => null, |
||
| 259 | ]; |
||
| 260 | |||
| 261 | try { |
||
| 262 | $user = User::firstOrCreate($data); |
||
| 263 | } catch (\Exception $e) { |
||
| 264 | $user = User::where('username', $collaborator->username) |
||
| 265 | ->where('provider', 'gitlab')->first(); |
||
| 266 | } |
||
| 267 | |||
| 268 | $userId[] = $user->id; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | $organization = Organization::where('username', $owner) |
||
| 273 | ->where('provider', 'gitlab')->first()->users(); |
||
| 274 | |||
| 275 | if (!$organization->userActive()->count()) { |
||
| 276 | $organization->attach($userId); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | public function createBranches($owner, $product_backlog_id, $repo, $providerId = null) |
||
| 299 | |||
| 300 | public function readIssues() |
||
| 321 | |||
| 322 | public function createOrUpdateIssue($obj) |
||
| 325 | |||
| 326 | public function createOrUpdateIssueComment($obj, $verb = 'POST') |
||
| 329 | |||
| 330 | public function deleteIssueComment($obj) |
||
| 333 | } |
||
| 334 |
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.