Skip to content

Commit c24b1af

Browse files
authored
refactor: Update types and docstrings (DenverCoder1#410)
1 parent eb2a4c3 commit c24b1af

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

src/card.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ function formatDate(string $dateString, string|null $format, string $locale): st
5656
/**
5757
* Check theme and color customization parameters to generate a theme mapping
5858
*
59-
* @param array<string, string> $params Request parameters
60-
* @return array<string, string> The chosen theme or default
59+
* @param array<string,string> $params Request parameters
60+
* @return array<string,string> The chosen theme or default
6161
*/
6262
function getRequestedTheme(array $params): array
6363
{
6464
/**
65-
* @var array<string, array<string, string>> $THEMES
65+
* @var array<string,array<string,string>> $THEMES
6666
* List of theme names mapped to labeled colors
6767
*/
6868
$THEMES = include "themes.php";
@@ -125,7 +125,7 @@ function getRequestedTheme(array $params): array
125125
* than the word width.
126126
* @return string The given string wrapped at the specified length
127127
*/
128-
function utf8WordWrap($string, $width = 75, $break = "\n", $cut_long_words = false)
128+
function utf8WordWrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false): string
129129
{
130130
// match anything 1 to $width chars long followed by whitespace or EOS
131131
$string = preg_replace("/(.{1,$width})(?:\s|$)/uS", "$1$break", $string);
@@ -145,7 +145,7 @@ function utf8WordWrap($string, $width = 75, $break = "\n", $cut_long_words = fal
145145
* @param string $string The input string
146146
* @return int The length of the string
147147
*/
148-
function utf8Strlen($string)
148+
function utf8Strlen(string $string): int
149149
{
150150
return preg_match_all("/./us", $string, $matches);
151151
}
@@ -156,7 +156,6 @@ function utf8Strlen($string)
156156
* @param string $text Text to split
157157
* @param int $maxChars Maximum number of characters per line
158158
* @param int $line1Offset Offset for the first line
159-
*
160159
* @return string Original text if one line, or split text with <tspan> elements
161160
*/
162161
function splitLines(string $text, int $maxChars, int $line1Offset): string
@@ -184,7 +183,6 @@ function splitLines(string $text, int $maxChars, int $line1Offset): string
184183
* Normalize a locale code
185184
*
186185
* @param string $localeCode Locale code
187-
*
188186
* @return string Normalized locale code
189187
*/
190188
function normalizeLocaleCode(string $localeCode): string
@@ -210,10 +208,9 @@ function normalizeLocaleCode(string $localeCode): string
210208
* Get the translations for a locale code after normalizing it
211209
*
212210
* @param string $localeCode Locale code
213-
*
214211
* @return array Translations for the locale code
215212
*/
216-
function getTranslations(string $localeCode)
213+
function getTranslations(string $localeCode): array
217214
{
218215
// normalize locale code
219216
$localeCode = normalizeLocaleCode($localeCode);
@@ -239,9 +236,8 @@ function getTranslations(string $localeCode)
239236
/**
240237
* Generate SVG output for a stats array
241238
*
242-
* @param array<string, mixed> $stats Streak stats
243-
* @param array<string, string>|NULL $params Request parameters
244-
*
239+
* @param array<string,mixed> $stats Streak stats
240+
* @param array<string,string>|NULL $params Request parameters
245241
* @return string The generated SVG Streak Stats card
246242
*
247243
* @throws InvalidArgumentException If a locale does not exist
@@ -424,8 +420,7 @@ function generateCard(array $stats, array $params = null): string
424420
* Generate SVG displaying an error message
425421
*
426422
* @param string $message The error message to display
427-
* @param array<string, string>|NULL $params Request parameters
428-
*
423+
* @param array<string,string>|NULL $params Request parameters
429424
* @return string The generated SVG error card
430425
*/
431426
function generateErrorCard(string $message, array $params = null): string
@@ -485,7 +480,6 @@ function generateErrorCard(string $message, array $params = null): string
485480
* Converts an SVG card to a PNG image
486481
*
487482
* @param string $svg The SVG for the card as a string
488-
*
489483
* @return string The generated PNG data
490484
*/
491485
function convertSvgToPng(string $svg): string
@@ -516,7 +510,7 @@ function convertSvgToPng(string $svg): string
516510
if (empty($png)) {
517511
// `2>&1`: redirect stderr to stdout
518512
$error = shell_exec("$cmd 2>&1"); // skipcq: PHP-A1009
519-
throw new Exception("Failed to convert SVG to PNG: {$error}", 500);
513+
throw new InvalidArgumentException("Failed to convert SVG to PNG: {$error}", 500);
520514
}
521515

522516
// return the generated png
@@ -527,7 +521,6 @@ function convertSvgToPng(string $svg): string
527521
* Return headers and response based on type
528522
*
529523
* @param string|array $output The stats (array) or error message (string) to display
530-
*
531524
* @return array The Content-Type header and the response body, and status code in case of an error
532525
*/
533526
function generateOutput(string|array $output): array
@@ -573,6 +566,7 @@ function generateOutput(string|array $output): array
573566
*
574567
* @param string|array $output The Content-Type header and the response body
575568
* @param int $responseCode The HTTP response code to send
569+
* @return void The function exits after sending the response
576570
*/
577571
function renderOutput(string|array $output, int $responseCode = 200): void
578572
{

src/stats.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
*
88
* @param string $user GitHub username to get graphs for
99
* @param int $year Year to get graph for
10-
*
1110
* @return string GraphQL query
1211
*/
13-
function buildContributionGraphQuery(string $user, int $year)
12+
function buildContributionGraphQuery(string $user, int $year): string
1413
{
1514
$start = "$year-01-01T00:00:00Z";
1615
$end = "$year-12-31T23:59:59Z";
@@ -36,8 +35,7 @@ function buildContributionGraphQuery(string $user, int $year)
3635
*
3736
* @param string $user GitHub username to get graphs for
3837
* @param array<int> $years Years to get graphs for
39-
*
40-
* @return array<stdClass> List of GraphQL response objects
38+
* @return array<int,stdClass> List of GraphQL response objects with years as keys
4139
*/
4240
function executeContributionGraphRequests(string $user, array $years): array
4341
{
@@ -115,7 +113,6 @@ function executeContributionGraphRequests(string $user, array $years): array
115113
* Get all HTTP request responses for user's contributions
116114
*
117115
* @param string $user GitHub username to get graphs for
118-
*
119116
* @return array<stdClass> List of contribution graph response objects
120117
*/
121118
function getContributionGraphs(string $user): array
@@ -138,7 +135,7 @@ function getContributionGraphs(string $user): array
138135
*
139136
* @return array<string> List of tokens
140137
*/
141-
function getGitHubTokens()
138+
function getGitHubTokens(): array
142139
{
143140
// result is already calculated
144141
if (isset($GLOBALS["ALL_TOKENS"])) {
@@ -161,21 +158,28 @@ function getGitHubTokens()
161158
* Get a token from the token pool
162159
*
163160
* @return string GitHub token
161+
*
162+
* @throws AssertionError if no tokens are available
164163
*/
165-
function getGitHubToken()
164+
function getGitHubToken(): string
166165
{
167166
$all_tokens = getGitHubTokens();
167+
// if there is no available token, throw an error (this should never happen)
168+
if (empty($all_tokens)) {
169+
throw new AssertionError("There is no GitHub token available.", 500);
170+
}
168171
return $all_tokens[array_rand($all_tokens)];
169172
}
170173

171174
/**
172175
* Remove a token from the token pool
173176
*
174177
* @param string $token Token to remove
178+
* @return void
175179
*
176180
* @throws AssertionError if no tokens are available after removing the token
177181
*/
178-
function removeGitHubToken(string $token)
182+
function removeGitHubToken(string $token): void
179183
{
180184
$index = array_search($token, $GLOBALS["ALL_TOKENS"]);
181185
if ($index !== false) {
@@ -194,10 +198,9 @@ function removeGitHubToken(string $token)
194198
*
195199
* @param string $query GraphQL query
196200
* @param string $token GitHub token to use for the request
197-
*
198201
* @return CurlHandle The curl handle for the request
199202
*/
200-
function getGraphQLCurlHandle(string $query, string $token)
203+
function getGraphQLCurlHandle(string $query, string $token): CurlHandle
201204
{
202205
$headers = [
203206
"Authorization: bearer $token",
@@ -221,9 +224,8 @@ function getGraphQLCurlHandle(string $query, string $token)
221224
/**
222225
* Get an array of all dates with the number of contributions
223226
*
224-
* @param array<stdClass> $contributionCalendars List of GraphQL response objects
225-
*
226-
* @return array<string, int> Y-M-D dates mapped to the number of contributions
227+
* @param array<int,stdClass> $contributionCalendars List of GraphQL response objects by year
228+
* @return array<string,int> Y-M-D dates mapped to the number of contributions
227229
*/
228230
function getContributionDates(array $contributionGraphs): array
229231
{
@@ -253,8 +255,8 @@ function getContributionDates(array $contributionGraphs): array
253255
/**
254256
* Get a stats array with the contribution count, daily streak, and dates
255257
*
256-
* @param array<string, int> $contributions Y-M-D contribution dates with contribution counts
257-
* @return array<string, mixed> Streak stats
258+
* @param array<string,int> $contributions Y-M-D contribution dates with contribution counts
259+
* @return array<string,mixed> Streak stats
258260
*/
259261
function getContributionStats(array $contributions): array
260262
{
@@ -331,8 +333,8 @@ function getPreviousSunday(string $date): string
331333
/**
332334
* Get a stats array with the contribution count, weekly streak, and dates
333335
*
334-
* @param array<string, int> $contributions Y-M-D contribution dates with contribution counts
335-
* @return array<string, mixed> Streak stats
336+
* @param array<string,int> $contributions Y-M-D contribution dates with contribution counts
337+
* @return array<string,mixed> Streak stats
336338
*/
337339
function getWeeklyContributionStats(array $contributions): array
338340
{

0 commit comments

Comments
 (0)