GithubClientProvider::getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Alnutile\Codereview;
5
6
use Carbon\Carbon;
7
use GuzzleHttp\Client;
8
9
class GithubClientProvider extends Application
10
{
11
12
13
    protected $github_token = "";
14
15
    protected $client;
16
17
    protected $base_url = "https://api.github.com/search/commits";
18
19
    protected $query_string = "";
20
21
22
    /**
23
     * GithubClientProvider constructor.
24
     * @param null|\Silly\Edition\Pimple\Application $app
25
     * @param Client $client
26
     * @codeCoverageIgnore
27
     */
28
    public function __construct($app, Client $client)
29
    {
30
        parent::__construct($app);
31
32
        $this->client = $client;
33
    }
34
35
36
    public function getLatestCommits($query = [])
37
    {
38
39
        $this->setQueryString($query);
40
41
        $url = sprintf(
42
            "%s?%s",
43
            $this->base_url,
44
            $this->getQueryString()
45
        );
46
47
        $results = $this->getClient()->get($url);
48
49
        return $this->returnResults($results);
50
    }
51
52
    /**
53
     * @return string
54
     * @codeCoverageIgnore
55
     */
56
    public function getGithubToken()
57
    {
58
        if (!$this->github_token) {
59
            $this->setGithubToken();
60
        }
61
        return $this->github_token;
62
    }
63
64
    /**
65
     * @param string $github_token
66
     */
67
    public function setGithubToken($github_token = null)
68
    {
69
        if (!$github_token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $github_token of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
70
            $github_token = $this->app->getConfigValueByKey('github_token');
0 ignored issues
show
Bug introduced by
The method getConfigValueByKey() does not seem to exist on object<Silly\Edition\Pimple\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
        }
72
        $this->github_token = $github_token;
73
    }
74
75
    /**
76
     * @return Client
77
     * @codeCoverageIgnore
78
     */
79
    public function getClient()
80
    {
81
        return $this->client;
82
    }
83
84
    /**
85
     * @param Client $client
86
     * @codeCoverageIgnore
87
     */
88
    public function setClient($client)
89
    {
90
        $this->client = $client;
91
    }
92
93
    /**
94
     * @param $results
95
     * @return array|mixed
96
     * @codeCoverageIgnore
97
     */
98
    private function returnResults($results)
99
    {
100
        if ($this->isGuzzleResponse($results)) {
101
            $results = json_decode($results->getBody(), true);
102
        }
103
104
        $results = $this->transformResults($results);
105
106
        return $results;
107
    }
108
109
    public function getQueryString()
110
    {
111
        return $this->query_string;
112
    }
113
114
115
116
    /**
117
     * @param string $query_string
0 ignored issues
show
Bug introduced by
There is no parameter named $query_string. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
118
     */
119
    public function setQueryString($query = [])
120
    {
121
        if (isset($query['committer'])) {
122
            $items[] = sprintf("committer:%s", $query['committer']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
123
        }
124
125
        $items[] = 'sort:committer-date-desc';
0 ignored issues
show
Bug introduced by
The variable $items does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
126
127
        if (isset($query['org'])) {
128
            $items[] = sprintf("org:%s", $query['org']);
129
        }
130
131
        $query = sprintf("q=%s", implode("+", $items));
132
133
        $query = sprintf('%s&access_token=%s', $query, $this->getGithubToken());
134
135
        $this->query_string = $query;
136
    }
137
138
    private function transformResults($results = [])
139
    {
140
        $items = [];
141
        foreach ($results['items'] as $key => $value) {
142
            $items[] = $this->getResult($value);
143
        }
144
145
        return $items;
146
    }
147
148
    /**
149
     * @param $value
150
     * @return array
151
     * @codeCoverageIgnore
152
     */
153
    private function getResult($value)
154
    {
155
        return [
156
            'repository' => $value['repository']['html_url'],
157
            'commit' => $value['html_url'],
158
            'date' => $this->getDate($value)
159
        ];
160
    }
161
162
    /**
163
     * @param $results
164
     * @return bool
165
     * @codeCoverageIgnore
166
     */
167
    private function isGuzzleResponse($results)
168
    {
169
        return is_object($results) && get_class($results) == 'GuzzleHttp\Psr7\Response';
170
    }
171
172
    /**
173
     * @param $value
174
     * @return string
175
     * @codeCoverageIgnore
176
     */
177
    private function getDate($value)
178
    {
179
        return Carbon::parse($value['commit']['committer']['date'])->format('Y/m/d H:i');
180
    }
181
}
182