Completed
Push — master ( 1f5aaf...c63d45 )
by Miro
04:10 queued 02:00
created

PaginatedKnpLabsRepoFacade::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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
     * @return array|mixed
34
     */
35 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...
36
    {
37
        $parameters = [
38
            $this->githubRepo->getOwner(),
39
            $this->githubRepo->getName(),
40
            ['state' => 'all'],
41
        ];
42
43
        return $this->paginator->fetchAll($this->getMilestonesApi(), 'all', $parameters);
44
    }
45
46
    /**
47
     * Returns all issues.
48
     *
49
     * IMPORTANT:
50
     * - GitHub API v3 returns both issues and pullRequests together so filtering needs to be done
51
     * - Pull requests have 'pull_request' key.
52
     *
53
     * @return array
54
     */
55
    public function fetchAllIssues()
56
    {
57
        $results = [];
58
59
        foreach ($this->fetchAllIssuesAndPullRequests() as $result) {
60
            if (!array_key_exists('pull_request', $result)) {
61
                $results[] = $result;
62
            }
63
        }
64
65
        return $results;
66
    }
67
68
    /**
69
     * @return array|mixed
70
     */
71 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...
72
    {
73
        $parameters = [
74
            $this->githubRepo->getOwner(),
75
            $this->githubRepo->getName(),
76
            ['state' => 'all'],
77
        ];
78
79
        return $this->paginator->fetchAll($this->getIssueApi(), 'all', $parameters);
80
    }
81
82
    /**
83
     * @return \Github\Api\ApiInterface
84
     */
85
    private function getIssueApi()
86
    {
87
        return $this->client->api('issues');
88
    }
89
90
    private function getMilestonesApi()
91
    {
92
        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...
93
    }
94
}
95