Teams   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 268
Duplicated Lines 19.4 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 52
loc 268
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A listTeams() 0 4 1
A getTeam() 0 4 1
A editTeam() 9 9 1
A deleteTeam() 10 10 2
A listTeamMembers() 0 4 1
A getTeamMembership() 0 5 1
A addTeamMembership() 0 6 1
A removeTeamMembership() 11 11 2
A listTeamRepos() 0 4 1
A checkTeamManagesRepository() 0 11 2
A addTeamRepository() 11 11 2
A removeTeamRepository() 11 11 2
A lisUserTeams() 0 4 1
A createTeam() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
namespace FlexyProject\GitHub\Receiver\Organizations;
3
4
use FlexyProject\GitHub\AbstractApi;
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * Teams API class gives you access to the available organization's teams.
9
 *
10
 * @package FlexyProject\GitHub\Receiver\Organizations
11
 */
12
class Teams extends AbstractOrganizations
13
{
14
15
    /**
16
     * List teams
17
     *
18
     * @link https://developer.github.com/v3/orgs/teams/#list-teams
19
     *
20
     * @param string $org
21
     *
22
     * @return array
23
     * @throws \Exception
24
     */
25
    public function listTeams(string $org): array
26
    {
27
        return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/teams', $org));
28
    }
29
30
    /**
31
     * Get team
32
     *
33
     * @link https://developer.github.com/v3/orgs/teams/#get-team
34
     *
35
     * @param int $id
36
     *
37
     * @return array
38
     * @throws \Exception
39
     */
40
    public function getTeam(int $id): array
41
    {
42
        return $this->getApi()->request($this->getApi()->sprintf('/teams/:id', (string)$id));
43
    }
44
45
    /**
46
     * Create team
47
     *
48
     * @link https://developer.github.com/v3/orgs/teams/#create-team
49
     *
50
     * @param string      $org
51
     * @param string      $name
52
     * @param null|string $description
53
     * @param array       $repoNames
54
     * @param string      $permission
55
     *
56
     * @return array
57
     * @throws \Exception
58
     */
59
    public function createTeam(string $org, string $name, string $description = null, array $repoNames = [],
60
                               string $permission = AbstractApi::PERMISSION_PULL): array
61
    {
62
        return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/teams', $org), Request::METHOD_POST, [
63
                'name'        => $name,
64
                'description' => $description,
65
                'repo_names'  => $repoNames,
66
                'permission'  => $permission
67
            ]);
68
    }
69
70
    /**
71
     * Edit team
72
     *
73
     * @link https://developer.github.com/v3/orgs/teams/#edit-team
74
     *
75
     * @param int         $id
76
     * @param string      $name
77
     * @param null|string $description
78
     * @param string      $permission
79
     *
80
     * @return array
81
     * @throws \Exception
82
     */
83 View Code Duplication
    public function editTeam(int $id, string $name, string $description = null,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
84
                             string $permission = AbstractApi::PERMISSION_PULL): array
85
    {
86
        return $this->getApi()->request($this->getApi()->sprintf('/teams/:id', (string)$id), Request::METHOD_PATCH, [
87
                'name'        => $name,
88
                'description' => $description,
89
                'permission'  => $permission
90
            ]);
91
    }
92
93
    /**
94
     * Delete team
95
     *
96
     * @link https://developer.github.com/v3/orgs/teams/#delete-team
97
     *
98
     * @param int $id
99
     *
100
     * @return bool
101
     * @throws \Exception
102
     */
103 View Code Duplication
    public function deleteTeam(int $id): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        $this->getApi()->request($this->getApi()->sprintf('/teams/:id', (string)$id), Request::METHOD_DELETE);
106
107
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
108
            return true;
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * List team members
116
     *
117
     * @link https://developer.github.com/v3/orgs/teams/#list-team-members
118
     *
119
     * @param int $id
120
     *
121
     * @return array
122
     * @throws \Exception
123
     */
124
    public function listTeamMembers(int $id): array
125
    {
126
        return $this->getApi()->request($this->getApi()->sprintf('/teams/:id/members', (string)$id));
127
    }
128
129
    /**
130
     * Get team membership
131
     *
132
     * @link https://developer.github.com/v3/orgs/teams/#get-team-membership
133
     *
134
     * @param int    $id
135
     * @param string $username
136
     *
137
     * @return array
138
     * @throws \Exception
139
     */
140
    public function getTeamMembership(int $id, string $username): array
141
    {
142
        return $this->getApi()->request($this->getApi()
143
                                             ->sprintf('/teams/:id/memberships/:username', (string)$id, $username));
144
    }
145
146
    /**
147
     * Add team membership
148
     *
149
     * @link https://developer.github.com/v3/orgs/teams/#add-team-membership
150
     *
151
     * @param int    $id
152
     * @param string $username
153
     *
154
     * @return array
155
     * @throws \Exception
156
     */
157
    public function addTeamMembership(int $id, string $username): array
158
    {
159
        return $this->getApi()->request($this->getApi()
160
                                             ->sprintf('/teams/:id/memberships/:username', (string)$id, $username),
161
            Request::METHOD_PUT);
162
    }
163
164
    /**
165
     * Remove team membership
166
     *
167
     * @link https://developer.github.com/v3/orgs/teams/#remove-team-membership
168
     *
169
     * @param int    $id
170
     * @param string $username
171
     *
172
     * @return bool
173
     * @throws \Exception
174
     */
175 View Code Duplication
    public function removeTeamMembership(int $id, string $username): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
176
    {
177
        $this->getApi()->request($this->getApi()->sprintf('/teams/:id/memberships/:username', (string)$id, $username),
178
            Request::METHOD_DELETE);
179
180
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
181
            return true;
182
        }
183
184
        return false;
185
    }
186
187
    /**
188
     * List team repos
189
     *
190
     * @link https://developer.github.com/v3/orgs/teams/#list-team-repos
191
     *
192
     * @param int $id
193
     *
194
     * @return array
195
     * @throws \Exception
196
     */
197
    public function listTeamRepos(int $id): array
198
    {
199
        return $this->getApi()->request($this->getApi()->sprintf('/teams/:id/repos', (string)$id));
200
    }
201
202
    /**
203
     * Check if a team manages a repository
204
     *
205
     * @link https://developer.github.com/v3/orgs/teams/#get-team-repo
206
     *
207
     * @param int $id
208
     *
209
     * @return bool
210
     * @throws \Exception
211
     */
212
    public function checkTeamManagesRepository(int $id): bool
213
    {
214
        $this->getApi()->request($this->getApi()->sprintf('/teams/:id/repos/:owner/:repo', (string)$id,
215
            $this->getOrganizations()->getOwner(), $this->getOrganizations()->getRepo()));
216
217
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
218
            return true;
219
        }
220
221
        return false;
222
    }
223
224
    /**
225
     * Add team repository
226
     *
227
     * @link https://developer.github.com/v3/orgs/teams/#add-team-repo
228
     *
229
     * @param int $id
230
     *
231
     * @return bool|array
232
     * @throws \Exception
233
     */
234 View Code Duplication
    public function addTeamRepository(int $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
235
    {
236
        $return = $this->getApi()->request($this->getApi()->sprintf('/teams/:id/repos/:org/:repo', (string)$id,
237
            $this->getOrganizations()->getOwner(), $this->getOrganizations()->getRepo()), Request::METHOD_PUT);
238
239
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
240
            return true;
241
        }
242
243
        return $return;
244
    }
245
246
    /**
247
     * Remove team repository
248
     *
249
     * @link https://developer.github.com/v3/orgs/teams/#remove-team-repo
250
     *
251
     * @param int $id
252
     *
253
     * @return bool
254
     * @throws \Exception
255
     */
256 View Code Duplication
    public function removeTeamRepository(int $id): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
257
    {
258
        $this->getApi()->request($this->getApi()->sprintf('/teams/:id/repos/:owner/:repo', (string)$id,
259
            $this->getOrganizations()->getOwner(), $this->getOrganizations()->getRepo()), Request::METHOD_DELETE);
260
261
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
262
            return true;
263
        }
264
265
        return false;
266
    }
267
268
    /**
269
     * List user teams
270
     *
271
     * @link https://developer.github.com/v3/orgs/teams/#list-user-teams
272
     * @return array
273
     * @throws \Exception
274
     */
275
    public function lisUserTeams(): array
276
    {
277
        return $this->getApi()->request($this->getApi()->sprintf('/user/teams'));
278
    }
279
}