alnutile /
codereview
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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) { |
||
| 70 | $github_token = $this->app->getConfigValueByKey('github_token'); |
||
| 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 |
||
| 118 | */ |
||
| 119 | public function setQueryString($query = []) |
||
| 120 | { |
||
| 121 | if (isset($query['committer'])) { |
||
| 122 | $items[] = sprintf("committer:%s", $query['committer']); |
||
|
0 ignored issues
–
show
|
|||
| 123 | } |
||
| 124 | |||
| 125 | $items[] = 'sort:committer-date-desc'; |
||
| 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 |
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.