ReviewComments   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 126
Duplicated Lines 8.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 11
loc 126
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A listCommentsPullRequest() 0 5 1
A getSingleComment() 0 5 1
A createComment() 0 10 1
A editComment() 0 8 1
A deleteComment() 0 11 2
A listCommentsRepository() 11 11 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\PullRequests;
3
4
use DateTime;
5
use FlexyProject\GitHub\AbstractApi;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * Pull Request API class Review Comments are comments on a portion of the unified diff.
10
 *
11
 * @link    https://developer.github.com/v3/pulls/comments/
12
 * @package FlexyProject\GitHub\Receiver\PullRequests
13
 */
14
class ReviewComments extends AbstractPullRequests
15
{
16
17
    /**
18
     * List comments on a pull request
19
     *
20
     * @link https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
21
     *
22
     * @param int $number
23
     *
24
     * @return array
25
     * @throws \Exception
26
     */
27
    public function listCommentsPullRequest(int $number): array
28
    {
29
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number/comments',
30
            $this->getPullRequests()->getOwner(), $this->getPullRequests()->getRepo(), $number));
31
    }
32
33
    /**
34
     * List comments in a repository
35
     *
36
     * @link https://developer.github.com/v3/pulls/comments/#list-comments-in-a-repository
37
     *
38
     * @param string $sort
39
     * @param string $direction
40
     * @param string $since
41
     *
42
     * @return array
43
     * @throws \Exception
44
     */
45 View Code Duplication
    public function listCommentsRepository(string $sort = AbstractApi::SORT_CREATED,
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...
46
                                           string $direction = AbstractApi::DIRECTION_DESC,
47
                                           string $since = 'now'): array
48
    {
49
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/comments?:args',
50
            $this->getPullRequests()->getOwner(), $this->getPullRequests()->getRepo(), http_build_query([
51
                'sort'      => $sort,
52
                'direction' => $direction,
53
                'since'     => (new DateTime($since))->format(DateTime::ATOM)
54
            ])));
55
    }
56
57
    /**
58
     * Get a single comment
59
     *
60
     * @link https://developer.github.com/v3/pulls/comments/#get-a-single-comment
61
     *
62
     * @param int $number
63
     *
64
     * @return array
65
     * @throws \Exception
66
     */
67
    public function getSingleComment(int $number): array
68
    {
69
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/comments/:number',
70
            $this->getPullRequests()->getOwner(), $this->getPullRequests()->getRepo(), $number));
71
    }
72
73
    /**
74
     * Create a comment
75
     *
76
     * @link https://developer.github.com/v3/pulls/comments/#create-a-comment
77
     *
78
     * @param int    $number
79
     * @param string $body
80
     * @param string $commitId
81
     * @param string $path
82
     * @param int    $position
83
     *
84
     * @return array
85
     * @throws \Exception
86
     */
87
    public function createComment(int $number, string $body, string $commitId, string $path, int $position): array
88
    {
89
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number/comments',
90
            $this->getPullRequests()->getOwner(), $this->getPullRequests()->getRepo(), $number), Request::METHOD_POST, [
91
                'body'      => $body,
92
                'commit_id' => $commitId,
93
                'path'      => $path,
94
                'position'  => $position
95
            ]);
96
    }
97
98
    /**
99
     * Edit a comment
100
     *
101
     * @link https://developer.github.com/v3/pulls/comments/#edit-a-comment
102
     *
103
     * @param int    $number
104
     * @param string $body
105
     *
106
     * @return array
107
     * @throws \Exception
108
     */
109
    public function editComment(int $number, string $body): array
110
    {
111
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/comments/:number',
112
            $this->getPullRequests()->getOwner(), $this->getPullRequests()->getRepo(), $number), Request::METHOD_PATCH,
113
            [
114
                'body' => $body
115
            ]);
116
    }
117
118
    /**
119
     * Delete a comment
120
     *
121
     * @link https://developer.github.com/v3/pulls/comments/#delete-a-comment
122
     *
123
     * @param int $number
124
     *
125
     * @return bool
126
     * @throws \Exception
127
     */
128
    public function deleteComment(int $number): bool
129
    {
130
        $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/comments/:number',
131
            $this->getPullRequests()->getOwner(), $this->getPullRequests()->getRepo(), $number));
132
133
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
134
            return true;
135
        }
136
137
        return false;
138
    }
139
}