Completed
Push — master ( 990239...4eaa1e )
by Miro
02:27
created

PaginatedKnpLabsRepoFacade::fetchAllBranchNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 5
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
    /** @return array */
64
    public function fetchAllBranches()
65
    {
66
        $results = [];
67
68
        foreach ($this->fetchAllBranchNames() as $branchData) {
69
            $results[] = $this->fetchBranch($branchData['name']);
70
        }
71
72
        return $results;
73
    }
74
75
    /** @return array */
76 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...
77
    {
78
        $parameters = [
79
            $this->githubRepo->getOwner(),
80
            $this->githubRepo->getName(),
81
        ];
82
83
        return $this->paginator->fetchAll($this->getRepoApi(), 'branches', $parameters);
84
    }
85
86
    /** @return array */
87
    public function fetchAllTags()
88
    {
89
        $results = [];
90
91
        foreach ($this->fetchAllTagNames() as $tagData) {
92
            $tagData['commit'] = $this->fetchCommit($tagData['commit']['sha']);
93
            $results[]         = $tagData;
94
        }
95
96
        return $results;
97
    }
98
99
    /** @return array */
100 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...
101
    {
102
        $parameters = [
103
            $this->githubRepo->getOwner(),
104
            $this->githubRepo->getName(),
105
        ];
106
107
        return $this->paginator->fetchAll($this->getRepoApi(), 'tags', $parameters);
108
    }
109
110
    /**
111
     * Fetches GithubCommit details.
112
     *
113
     * @param $commitSha
114
     *
115
     * @return array
116
     */
117
    public function fetchCommit($commitSha)
118
    {
119
        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...
120
            ->show(
121
                $this->githubRepo->getOwner(),
122
                $this->githubRepo->getName(),
123
                $commitSha
124
            );
125
    }
126
127
    /**
128
     * Fetches GithubCommit status.
129
     *
130
     * @param $commitSha
131
     *
132
     * @return array
133
     */
134
    public function fetchCommitStatus($commitSha)
135
    {
136
        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...
137
            ->combined(
138
                $this->githubRepo->getOwner(),
139
                $this->githubRepo->getName(),
140
                $commitSha
141
            );
142
    }
143
144
    /**
145
     * Fetches list of GithubCommit statuses.
146
     *
147
     * @param $commitSha
148
     *
149
     * @return array
150
     */
151
    public function fetchCommitStatuses($commitSha)
152
    {
153
        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...
154
            ->show(
155
                $this->githubRepo->getOwner(),
156
                $this->githubRepo->getName(),
157
                $commitSha
158
            );
159
    }
160
161
    /** @return array */
162 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...
163
    {
164
        $parameters = [
165
            $this->githubRepo->getOwner(),
166
            $this->githubRepo->getName(),
167
            ['state' => 'all'],
168
        ];
169
170
        return $this->paginator->fetchAll($this->getPullRequestApi(), 'all', $parameters);
171
    }
172
173
    /** @return array */
174 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...
175
    {
176
        $parameters = [
177
            $this->githubRepo->getOwner(),
178
            $this->githubRepo->getName(),
179
            ['state' => 'all'],
180
        ];
181
182
        return $this->paginator->fetchAll($this->getMilestonesApi(), 'all', $parameters);
183
    }
184
185
    /**
186
     * Returns all issues.
187
     *
188
     * IMPORTANT:
189
     * - GitHub API v3 returns both issues and pullRequests together so filtering needs to be done
190
     * - Pull requests have 'pull_request' key.
191
     *
192
     * @return array
193
     */
194
    public function fetchAllIssues()
195
    {
196
        $results = [];
197
198
        foreach ($this->fetchAllIssuesAndPullRequests() as $result) {
199
            if (!array_key_exists('pull_request', $result)) {
200
                $results[] = $result;
201
            }
202
        }
203
204
        return $results;
205
    }
206
207
    /** @return array */
208 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...
209
    {
210
        $parameters = [
211
            $this->githubRepo->getOwner(),
212
            $this->githubRepo->getName(),
213
            ['state' => 'all'],
214
        ];
215
216
        return $this->paginator->fetchAll($this->getIssueApi(), 'all', $parameters);
217
    }
218
219
    /**
220
     * @return \Github\Api\ApiInterface
221
     */
222
    private function getRepoApi()
223
    {
224
        return $this->client->api('repository');
225
    }
226
227
    /**
228
     * @return \Github\Api\ApiInterface
229
     */
230
    private function getIssueApi()
231
    {
232
        return $this->client->api('issues');
233
    }
234
235
    /**
236
     * @return \Github\Api\Issue\Milestones
237
     */
238
    private function getMilestonesApi()
239
    {
240
        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...
241
    }
242
243
    /**
244
     * @return \Github\Api\PullRequest
245
     */
246
    private function getPullRequestApi()
247
    {
248
        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...
249
    }
250
}
251