Skip to content

Commit 09d18cd

Browse files
authored
docs: Refactoring and min PHP version bumped to 8 (#170)
1 parent a74e2d8 commit 09d18cd

File tree

8 files changed

+68
-89
lines changed

8 files changed

+68
-89
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Make sure your request is meaningful and you have tested the app locally before
99

1010
#### Requirements
1111

12-
* [PHP 7.4+](https://www.apachefriends.org/index.html)
12+
* [PHP 8.0+](https://www.apachefriends.org/index.html)
1313
* [Composer](https://getcomposer.org)
1414
* [Imagick](https://www.php.net/imagick)
1515

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Make sure your request is meaningful and you have tested the app locally before
186186

187187
#### Requirements
188188

189-
- [PHP 7.4+](https://www.apachefriends.org/index.html)
189+
- [PHP 8.0+](https://www.apachefriends.org/index.html)
190190
- [Composer](https://getcomposer.org)
191191
- [Imagick](https://www.php.net/imagick)
192192

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
]
1919
},
2020
"require": {
21-
"php": "^7.4|^8.0",
21+
"php": "^8.0",
2222
"ext-imagick": "*",
2323
"vlucas/phpdotenv": "^5.3"
2424
},

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/card.php

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,6 @@
22

33
declare(strict_types=1);
44

5-
/**
6-
* Set headers and echo response based on type
7-
*
8-
* @param string|array $output
9-
* @return void
10-
*/
11-
function renderOutput(string|array $output): void
12-
{
13-
$requestedType = $_REQUEST['type'] ?? 'svg';
14-
15-
$card = gettype($output) === "string" ? generateErrorCard($output) : generateCard($output);
16-
17-
if ($requestedType === "json") {
18-
$data = gettype($output) === "string" ? array("error" => $output) : $output;
19-
echoAsJson($data);
20-
exit;
21-
}
22-
23-
if ($requestedType === "png") {
24-
echoAsPng($card);
25-
exit;
26-
}
27-
28-
echoAsSvg($card);
29-
exit;
30-
}
31-
32-
/**
33-
* Displays an array as JSON
34-
*
35-
* @param array $data The data array to output
36-
*/
37-
function echoAsJson(array $data): void
38-
{
39-
// set content type to JSON
40-
header('Content-Type: application/json');
41-
// echo JSON data
42-
echo json_encode($data);
43-
}
44-
45-
465
/**
476
* Convert date from Y-M-D to more human-readable format
487
*
@@ -334,27 +293,15 @@ function generateErrorCard(string $message, array $params = null): string
334293
}
335294

336295
/**
337-
* Displays a card as an SVG image
296+
* Converts an SVG card to a PNG image
338297
*
339-
* @param string $svg The SVG for the card to display
340-
*/
341-
function echoAsSvg(string $svg): void
342-
{
343-
// set content type to SVG image
344-
header("Content-Type: image/svg+xml");
345-
346-
// echo SVG data for streak stats
347-
echo $svg;
348-
}
349-
350-
/**
351-
* Displays a card as a PNG image
352-
*
353-
* @param string $svg The SVG for the card to display
298+
* @param string $svg The SVG for the card as a string
299+
*
300+
* @return string The generated PNG data
354301
*
355302
* @throws ImagickException
356303
*/
357-
function echoAsPng(string $svg): void
304+
function convertSvgToPng(string $svg): string
358305
{
359306
// trim off all whitespaces to make it a valid SVG string
360307
$svg = trim($svg);
@@ -374,11 +321,42 @@ function echoAsPng(string $svg): void
374321
$imagick->readImageBlob('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg);
375322
$imagick->setFormat('png');
376323

377-
// echo PNG data
378-
header('Content-Type: image/png');
379-
echo $imagick->getImageBlob();
324+
// get PNG data
325+
$png = $imagick->getImageBlob();
380326

381327
// clean up memory
382328
$imagick->clear();
383329
$imagick->destroy();
330+
331+
return $png;
332+
}
333+
334+
/**
335+
* Set headers and echo response based on type
336+
*
337+
* @param string|array $output The stats (array) or error message (string) to display
338+
*/
339+
function renderOutput(string|array $output): void
340+
{
341+
$requestedType = $_REQUEST['type'] ?? 'svg';
342+
343+
// output JSON data
344+
if ($requestedType === "json") {
345+
// set content type to JSON
346+
header('Content-Type: application/json');
347+
// generate array from output
348+
$data = gettype($output) === "string" ? array("error" => $output) : $output;
349+
// output as JSON
350+
echo json_encode($data);
351+
}
352+
// output SVG or PNG card
353+
else {
354+
// set content type to SVG or PNG
355+
header("Content-Type: image/" . ($requestedType === "png" ? "png" : "svg+xml"));
356+
// render SVG card
357+
$svg = gettype($output) === "string" ? generateErrorCard($output) : generateCard($output);
358+
// output PNG if PNG is requested, otherwise output SVG
359+
echo $requestedType === "png" ? convertSvgToPng($svg) : $svg;
360+
}
361+
exit;
384362
}

src/index.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
// get streak stats for user given in query string
4141
$contributionGraphs = getContributionGraphs($_REQUEST["user"]);
4242
$contributions = getContributionDates($contributionGraphs);
43+
// if no contributions, display error
44+
if (count($contributions) === 0) {
45+
throw new AssertionError("No contributions found.");
46+
}
4347
$stats = getContributionStats($contributions);
4448
renderOutput($stats);
45-
} catch (InvalidArgumentException $error) {
49+
} catch (InvalidArgumentException|AssertionError $error) {
4650
renderOutput($error->getMessage());
4751
}
48-
49-

src/stats.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,14 @@ function getGitHubApiResponse(string $url): string
110110
curl_setopt($ch, CURLOPT_VERBOSE, false);
111111
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
112112
$response = curl_exec($ch);
113-
114-
//Handles curl errors
115-
if($response === false) {
116-
if(str_contains(curl_error($ch), 'unable to get local issuer certificate')) {
117-
throw new InvalidArgumentException("You don't have valid SSL Certificate installed or XAMPP.");
113+
// handle curl errors
114+
if ($response === false) {
115+
if (str_contains(curl_error($ch), 'unable to get local issuer certificate')) {
116+
throw new InvalidArgumentException("You don't have a valid SSL Certificate installed or XAMPP.");
118117
}
119-
throw new InvalidArgumentException("Something is wrong with getGitHubApiResponse().");
118+
throw new InvalidArgumentException("An error occurred when getting a response from GitHub.");
120119
}
121-
120+
// close curl handle and return response
122121
curl_close($ch);
123122
return $response;
124123
}
@@ -163,8 +162,8 @@ function getYearJoined(string $user): int
163162
*/
164163
function getContributionStats(array $contributions): array
165164
{
166-
$today = array_key_last($contributions) ?? date("Y-m-d");
167-
$first = array_key_first($contributions) ?? date("Y-m-d");
165+
$today = array_key_last($contributions);
166+
$first = array_key_first($contributions);
168167
$stats = [
169168
"totalContributions" => 0,
170169
"firstContribution" => "",

src/themes.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -783,17 +783,17 @@
783783
"dates" => "#000000",
784784
],
785785
"Javascript-dark" => [
786-
"background" => "#000000FF",
787-
"border" => "#F7DF1EFF",
788-
"stroke" => "#F7DF1EFF",
789-
"ring" => "#F7DF1EFF",
790-
"currStreakNum" => "#FFFFFFFF",
791-
"fire" => "#F7DF1EFF",
792-
"sideNums" => "#FFFFFFFF",
793-
"currStreakLabel" => "#F7DF1EFF",
794-
"sideLabels" => "#F7DF1EFF",
795-
"dates" => "#F7DF1EFF",
796-
],
786+
"background" => "#000000FF",
787+
"border" => "#F7DF1EFF",
788+
"stroke" => "#F7DF1EFF",
789+
"ring" => "#F7DF1EFF",
790+
"currStreakNum" => "#FFFFFFFF",
791+
"fire" => "#F7DF1EFF",
792+
"sideNums" => "#FFFFFFFF",
793+
"currStreakLabel" => "#F7DF1EFF",
794+
"sideLabels" => "#F7DF1EFF",
795+
"dates" => "#F7DF1EFF",
796+
],
797797
"noctis-minimus" => [
798798
"background" => "#1b2932",
799799
"border" => "#F0F0F0FF",

0 commit comments

Comments
 (0)