Milestones   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 111
Duplicated Lines 20.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 23
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A listMilestones() 0 10 1
A getMilestone() 0 5 1
A createMilestone() 12 12 1
A updateMilestone() 11 11 1
A deleteMilestone() 0 11 2

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\Issues;
3
4
use DateTime;
5
use Symfony\Component\HttpFoundation\Request;
6
use FlexyProject\GitHub\AbstractApi;
7
8
/**
9
 * The Trees API class provides access to Issues's milestones.
10
 *
11
 * @link    https://developer.github.com/v3/issues/milestones/
12
 * @package FlexyProject\GitHub\Receiver\Issues
13
 */
14
class Milestones extends AbstractIssues
15
{
16
17
    /**
18
     * List milestones for a repository
19
     *
20
     * @link https://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
21
     *
22
     * @param string $state
23
     * @param string $sort
24
     * @param string $direction
25
     *
26
     * @return array
27
     */
28
    public function listMilestones(string $state = AbstractApi::STATE_OPEN, string $sort = AbstractApi::SORT_DUE_DATE,
29
                                   string $direction = AbstractApi::DIRECTION_ASC): array
30
    {
31
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/milestones?:args',
32
            $this->getIssues()->getOwner(), $this->getIssues()->getRepo(), http_build_query([
33
                'state'     => $state,
34
                'sort'      => $sort,
35
                'direction' => $direction
36
            ])));
37
    }
38
39
    /**
40
     * Get a single milestone
41
     *
42
     * @link https://developer.github.com/v3/issues/milestones/#get-a-single-milestone
43
     *
44
     * @param int $number
45
     *
46
     * @return array
47
     */
48
    public function getMilestone(int $number): array
49
    {
50
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/milestones/:number',
51
            $this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number));
52
    }
53
54
    /**
55
     * Create a milestone
56
     *
57
     * @link https://developer.github.com/v3/issues/milestones/#create-a-milestone
58
     *
59
     * @param string $title
60
     * @param string $state
61
     * @param string $description
62
     * @param string $dueOn
63
     *
64
     * @return array
65
     */
66 View Code Duplication
    public function createMilestone(string $title, string $state = AbstractApi::STATE_OPEN, string $description = '',
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...
67
                                    string $dueOn = ''): array
68
    {
69
        return $this->getApi()->request($this->getApi()
70
                                             ->sprintf('/repos/:owner/:repo/milestones', $this->getIssues()->getOwner(),
71
                                                 $this->getIssues()->getRepo()), Request::METHOD_POST, [
72
                'title'       => $title,
73
                'state'       => $state,
74
                'description' => $description,
75
                'due_on'      => (new DateTime($dueOn))->format(DateTime::ATOM)
76
            ]);
77
    }
78
79
    /**
80
     * Update a milestone
81
     *
82
     * @link https://developer.github.com/v3/issues/milestones/#update-a-milestone
83
     *
84
     * @param int    $number
85
     * @param string $title
86
     * @param string $state
87
     * @param string $description
88
     * @param string $dueOn
89
     *
90
     * @return array
91
     */
92 View Code Duplication
    public function updateMilestone(int $number, string $title = '', string $state = AbstractApi::STATE_OPEN,
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...
93
                                    string $description = '', string $dueOn = ''): array
94
    {
95
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/milestones/:number',
96
            $this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number), Request::METHOD_PATCH, [
97
                'title'       => $title,
98
                'state'       => $state,
99
                'description' => $description,
100
                'due_on'      => (new DateTime($dueOn))->format(DateTime::ATOM)
101
            ]);
102
    }
103
104
    /**
105
     * Delete a milestone
106
     *
107
     * @link https://developer.github.com/v3/issues/milestones/#delete-a-milestone
108
     *
109
     * @param int $number
110
     *
111
     * @return bool
112
     */
113
    public function deleteMilestone(int $number): bool
114
    {
115
        $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/milestones/:number',
116
            $this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number), Request::METHOD_DELETE);
117
118
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
119
            return true;
120
        }
121
122
        return false;
123
    }
124
}