Completed
Pull Request — master (#8)
by Miro
02:49
created

fetchAllIssuesAndPullRequests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
namespace DevBoardLib\GithubApiFacade\Repo;
3
4
use DevBoardLib\GithubCore\Repo\GithubRepo;
5
use Github\Client;
6
use Github\ResultPager;
7
8
/**
9
 * Class PaginatedKnpLabsRepoFacade.
10
 */
11
class PaginatedKnpLabsRepoFacade implements RepoFacade
12
{
13
    private $client;
14
    private $githubRepo;
15
16
    private $paginator;
17
18
    /**
19
     * PaginatedKnpLabsRepoFacade constructor.
20
     *
21
     * @param $client
22
     * @param $githubRepo
23
     */
24
    public function __construct(Client $client, GithubRepo $githubRepo)
25
    {
26
        $this->client     = $client;
27
        $this->githubRepo = $githubRepo;
28
29
        $this->paginator = new ResultPager($this->client);
30
    }
31
32
    /**
33
     * Fetches GithubRepo details.
34
     *
35
     * @return array
36
     */
37
    public function fetchDetails()
38
    {
39
        return $this->getRepoApi()
0 ignored issues
show
Bug introduced by
The call to show() misses a required argument $id.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
The method show does only exist in Github\Api\Authorization...epo and Github\Api\User, but not in Github\Api\Deployment an...t and Github\Api\Search.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
40
            ->show(
41
                $this->githubRepo->getOwner(),
0 ignored issues
show
Unused Code introduced by
The call to CurrentUser::show() has too many arguments starting with $this->githubRepo->getOwner().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
                $this->githubRepo->getName()
0 ignored issues
show
Unused Code introduced by
The call to Gists::show() has too many arguments starting with $this->githubRepo->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Organization::show() has too many arguments starting with $this->githubRepo->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Teams::show() has too many arguments starting with $this->githubRepo->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Authorizations::show() has too many arguments starting with $this->githubRepo->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to User::show() has too many arguments starting with $this->githubRepo->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
            );
44
    }
45
46
    /**
47
     * Fetches GithubBranch details.
48
     *
49
     * @param $branchName
50
     *
51
     * @return array
52
     */
53
    public function fetchBranch($branchName)
54
    {
55
        return $this->getRepoApi()
0 ignored issues
show
Bug introduced by
The method branches does only exist in Github\Api\Repo, but not in Github\Api\Authorization...rch and Github\Api\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
56
            ->branches(
57
                $this->githubRepo->getOwner(),
58
                $this->githubRepo->getName(),
59
                $branchName
60
            );
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function fetchAllBranches()
67
    {
68
        $results = [];
69
70
        foreach ($this->fetchAllBranchNames() as $branchData) {
71
            $results[] = $this->fetchBranch($branchData['name']);
72
        }
73
74
        return $results;
75
    }
76
77
    /**
78
     * @return array
79
     */
80 View Code Duplication
    public function fetchAllBranchNames()
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...
81
    {
82
        $parameters = [
83
            $this->githubRepo->getOwner(),
84
            $this->githubRepo->getName(),
85
        ];
86
87
        return $this->paginator->fetchAll($this->getRepoApi(), 'branches', $parameters);
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function fetchAllTags()
94
    {
95
        $results = [];
96
97
        foreach ($this->fetchAllTagNames() as $tagData) {
98
            $tagData['commit'] = $this->fetchCommit($tagData['commit']['sha']);
99
            $results[]         = $tagData;
100
        }
101
102
        return $results;
103
    }
104
105
    /**
106
     * @return array
107
     */
108 View Code Duplication
    public function fetchAllTagNames()
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...
109
    {
110
        $parameters = [
111
            $this->githubRepo->getOwner(),
112
            $this->githubRepo->getName(),
113
        ];
114
115
        return $this->paginator->fetchAll($this->getRepoApi(), 'tags', $parameters);
116
    }
117
118
    /**
119
     * Fetches GithubCommit details.
120
     *
121
     * @param $commitSha
122
     *
123
     * @return array
124
     */
125
    public function fetchCommit($commitSha)
126
    {
127
        return $this->getRepoApi()->commits()
0 ignored issues
show
Bug introduced by
The call to commits() misses a required argument $id.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
The call to commits() misses some required arguments starting with $username.
Loading history...
Bug introduced by
The method commits does only exist in Github\Api\Gists and Git...est and Github\Api\Repo, but not in Github\Api\Authorization...rch and Github\Api\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
            ->show(
129
                $this->githubRepo->getOwner(),
130
                $this->githubRepo->getName(),
131
                $commitSha
132
            );
133
    }
134
135
    /**
136
     * Fetches GithubCommit status.
137
     *
138
     * @param $commitSha
139
     *
140
     * @return array
141
     */
142
    public function fetchCommitStatus($commitSha)
143
    {
144
        return $this->getRepoApi()->statuses()
0 ignored issues
show
Bug introduced by
The method statuses does only exist in Github\Api\Repo, but not in Github\Api\Authorization...rch and Github\Api\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
145
            ->combined(
146
                $this->githubRepo->getOwner(),
147
                $this->githubRepo->getName(),
148
                $commitSha
149
            );
150
    }
151
152
    /**
153
     * Fetches list of GithubCommit statuses.
154
     *
155
     * @param $commitSha
156
     *
157
     * @return array
158
     */
159
    public function fetchCommitStatuses($commitSha)
160
    {
161
        return $this->getRepoApi()->statuses()
0 ignored issues
show
Bug introduced by
The method statuses does only exist in Github\Api\Repo, but not in Github\Api\Authorization...rch and Github\Api\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
162
            ->show(
163
                $this->githubRepo->getOwner(),
164
                $this->githubRepo->getName(),
165
                $commitSha
166
            );
167
    }
168
169
    /**
170
     * @return array
171
     */
172 View Code Duplication
    public function fetchAllPullRequests()
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...
173
    {
174
        $parameters = [
175
            $this->githubRepo->getOwner(),
176
            $this->githubRepo->getName(),
177
            ['state' => 'all'],
178
        ];
179
180
        return $this->paginator->fetchAll($this->getPullRequestApi(), 'all', $parameters);
181
    }
182
183
    /**
184
     * @return array
185
     */
186 View Code Duplication
    public function fetchAllMilestones()
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...
187
    {
188
        $parameters = [
189
            $this->githubRepo->getOwner(),
190
            $this->githubRepo->getName(),
191
            ['state' => 'all'],
192
        ];
193
194
        return $this->paginator->fetchAll($this->getMilestonesApi(), 'all', $parameters);
195
    }
196
197
    /**
198
     * Returns all issues.
199
     *
200
     * IMPORTANT:
201
     * - GitHub API v3 returns both issues and pullRequests together so filtering needs to be done
202
     * - Pull requests have 'pull_request' key.
203
     *
204
     * @return array
205
     */
206
    public function fetchAllIssues()
207
    {
208
        $results = [];
209
210
        foreach ($this->fetchAllIssuesAndPullRequests() as $result) {
211
            if (!array_key_exists('pull_request', $result)) {
212
                $results[] = $result;
213
            }
214
        }
215
216
        return $results;
217
    }
218
219
    /**
220
     * @return array
221
     */
222 View Code Duplication
    public function fetchAllIssuesAndPullRequests()
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...
223
    {
224
        $parameters = [
225
            $this->githubRepo->getOwner(),
226
            $this->githubRepo->getName(),
227
            ['state' => 'all'],
228
        ];
229
230
        return $this->paginator->fetchAll($this->getIssueApi(), 'all', $parameters);
231
    }
232
233
    /**
234
     * @return \Github\Api\ApiInterface
235
     */
236
    private function getRepoApi()
237
    {
238
        return $this->client->api('repository');
239
    }
240
241
    /**
242
     * @return \Github\Api\ApiInterface
243
     */
244
    private function getIssueApi()
245
    {
246
        return $this->client->api('issues');
247
    }
248
249
    /**
250
     * @return \Github\Api\Issue\Milestones
251
     */
252
    private function getMilestonesApi()
253
    {
254
        return $this->getIssueApi()->milestones();
0 ignored issues
show
Bug introduced by
The call to milestones() misses some required arguments starting with $username.
Loading history...
Bug introduced by
The method milestones does only exist in Github\Api\Issue and Github\Api\Repo, but not in Github\Api\Authorization...rch and Github\Api\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
255
    }
256
257
    /**
258
     * @return \Github\Api\PullRequest
259
     */
260
    private function getPullRequestApi()
261
    {
262
        return $this->client->api('pull_requests');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->client->api('pull_requests'); (Github\Api\CurrentUser|G...pi\Meta|Github\Api\User) is incompatible with the return type documented by DevBoardLib\GithubApiFac...cade::getPullRequestApi of type Github\Api\PullRequest.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
263
    }
264
}
265