Skip to content

Commit a4aa8c2

Browse files
committed
phpstan fix
Signed-off-by: bidi <bidi@apidemia.com>
1 parent da6d4aa commit a4aa8c2

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/App/src/Service/GitHubClient.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,34 @@
4141
* package generator needs and nothing more. Requests degrade to unauthenticated when no token is
4242
* configured, which keeps the generator usable on a machine without credentials.
4343
*
44-
* @phpstan-type ResponseData array{status: int, body: string, links: array<string, string>}
44+
* @phpstan-type ResponseData array{status: int, body: string, links: array<string, non-empty-string>}
4545
*/
4646
class GitHubClient implements GitHubClientInterface
4747
{
48-
private const string API_ROOT = 'https://api.github.com';
49-
private const string API_VERSION = '2022-11-28';
48+
private const string API_ROOT = 'https://api.github.com';
49+
private const string API_VERSION = '2022-11-28';
50+
private const string DEFAULT_USER_AGENT = 'dotkernel.com';
51+
52+
/**
53+
* cURL rejects an empty user agent, and GitHub rejects requests without one, so an empty
54+
* configured value falls back to the default rather than failing every request.
55+
*
56+
* @var non-empty-string
57+
*/
58+
private readonly string $userAgent;
5059

5160
public function __construct(
5261
private readonly string $token,
53-
private readonly string $userAgent,
62+
string $userAgent,
5463
private readonly int $timeout,
5564
private readonly int $connectTimeout,
5665
) {
66+
$this->userAgent = $userAgent === '' ? self::DEFAULT_USER_AGENT : $userAgent;
5767
}
5868

69+
/**
70+
* @param non-empty-string $path
71+
*/
5972
public function get(string $path, string $accept = self::ACCEPT_JSON): ?string
6073
{
6174
$response = $this->request($this->absoluteUrl($path), $accept);
@@ -70,6 +83,7 @@ public function get(string $path, string $accept = self::ACCEPT_JSON): ?string
7083
}
7184

7285
/**
86+
* @param non-empty-string $path
7387
* @return list<array<string, mixed>>
7488
*/
7589
public function getAllPages(string $path): array
@@ -99,6 +113,7 @@ public function getAllPages(string $path): array
99113
}
100114

101115
/**
116+
* @param non-empty-string $url
102117
* @return ResponseData
103118
*/
104119
private function request(string $url, string $accept): array
@@ -147,16 +162,18 @@ private function request(string $url, string $accept): array
147162
/**
148163
* Parses `<https://...>; rel="next", <https://...>; rel="last"` into a rel => url map.
149164
*
150-
* @return array<string, string>
165+
* @return array<string, non-empty-string>
151166
*/
152167
private function parseLinkHeader(string $value): array
153168
{
154169
$links = [];
155170

156171
foreach (explode(',', $value) as $part) {
157-
if (preg_match('/<([^>]+)>\s*;\s*rel="([^"]+)"/', trim($part), $matches) === 1) {
158-
$links[$matches[2]] = $matches[1];
172+
if (preg_match('/<([^>]+)>\s*;\s*rel="([^"]+)"/', trim($part), $matches) !== 1) {
173+
continue;
159174
}
175+
176+
$links[$matches[2]] = $matches[1];
160177
}
161178

162179
return $links;
@@ -179,6 +196,10 @@ private function headers(string $accept): array
179196
return $headers;
180197
}
181198

199+
/**
200+
* @param non-empty-string $path
201+
* @return non-empty-string
202+
*/
182203
private function absoluteUrl(string $path): string
183204
{
184205
if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {

src/App/src/Service/GitHubClientInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface GitHubClientInterface
1414
/**
1515
* Performs a single authenticated GET request.
1616
*
17-
* @param string $path Path relative to the API root, or an absolute URL.
17+
* @param non-empty-string $path Path relative to the API root, or an absolute URL.
1818
* @return string|null The raw response body, or null when the resource does not exist.
1919
* @throws RuntimeException On transport failure or an unexpected response status.
2020
*/
@@ -23,6 +23,7 @@ public function get(string $path, string $accept = self::ACCEPT_JSON): ?string;
2323
/**
2424
* Performs a GET request and follows every `rel="next"` link, merging the decoded pages.
2525
*
26+
* @param non-empty-string $path
2627
* @return list<array<string, mixed>>
2728
* @throws RuntimeException On transport failure or an unexpected response status.
2829
*/

0 commit comments

Comments
 (0)