| Conditions | 7 |
| Paths | 7 |
| Total Lines | 58 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 101 | public function organization($obj) |
||
| 102 | { |
||
| 103 | |||
| 104 | if (!isset($obj->owner) && !isset($obj->namespace)) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!isset($obj->owner) && isset($obj->namespace)) { |
||
| 109 | // To avoid to make unnecessary calls to the api to get the groups info saving the fetched groups into a private variable |
||
| 110 | if (!isset($this->gitlabGroups[$obj->namespace->id])) { |
||
| 111 | $group = current(collect(Helper::request(env('GITLAB_INSTANCE_URI').'api/v3/groups/'.$obj->namespace->id.'?access_token='.Auth::user()->token))); |
||
| 112 | |||
| 113 | $this->gitlabGroups[$obj->namespace->id] = $group; |
||
| 114 | } |
||
| 115 | |||
| 116 | $group = $this->gitlabGroups[$obj->namespace->id]; |
||
| 117 | |||
| 118 | $obj->owner = new \stdClass; |
||
| 119 | $obj->owner->id = $group['id']; |
||
| 120 | $obj->owner->username = $group['path']; |
||
| 121 | $obj->owner->web_url = $group['web_url']; |
||
| 122 | $obj->owner->avatar_url = $group['avatar_url']; |
||
| 123 | } |
||
| 124 | |||
| 125 | $data = [ |
||
| 126 | 'provider_id' => $obj->owner->id, |
||
| 127 | 'username' => $obj->owner->username, |
||
| 128 | 'url' => $obj->owner->web_url, |
||
| 129 | 'repos_url' => null, |
||
| 130 | 'events_url' => null, |
||
| 131 | 'hooks_url' => null, |
||
| 132 | 'issues_url' => null, |
||
| 133 | 'members_url' => null, |
||
| 134 | 'public_members_url' => null, |
||
| 135 | 'avatar_url' => $obj->owner->avatar_url, |
||
| 136 | 'description' => null, |
||
| 137 | 'title' => $obj->owner->username, |
||
| 138 | 'blog' => null, |
||
| 139 | 'location' => null, |
||
| 140 | 'email' => null, |
||
| 141 | 'public_repos' => null, |
||
| 142 | 'html_url' => null, |
||
| 143 | 'total_private_repos' => null, |
||
| 144 | 'since' => @Carbon::parse($obj->namespace->created_at)->toDateTimeString(), |
||
| 145 | 'disk_usage' => null, |
||
| 146 | ]; |
||
| 147 | |||
| 148 | try { |
||
| 149 | $organization = Organization::create($data); |
||
| 150 | } catch (\Illuminate\Database\QueryException $e) { |
||
| 151 | $organization = Organization::where('username', $data['username']) |
||
| 152 | ->where('provider', 'gitlab')->first(); |
||
| 153 | } |
||
| 154 | |||
| 155 | $organization->users()->sync([Auth::id()]); |
||
| 156 | |||
| 157 | return $organization; |
||
| 158 | } |
||
| 159 | |||
| 203 |
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.