-
-
Notifications
You must be signed in to change notification settings - Fork 842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning - headers getting sent twice #162 #163
Warning - headers getting sent twice #162 #163
Conversation
Doesn't work for error cards, generateErrorCard needs to be called instead of GenerateCard for some and it takes a message (string) instead of array (stats) |
What do you think about this solution? |
Both generateCard and generateErrorCard return a string might as well pass that in as an argument. I'm thinking something like: function renderOutput(string $output)
{
$requestedType = $_REQUEST['type'] ?? 'svg';
// set headers and echo response based on type
...
} And call it like renderOutput(generateErrorCard($error->message));
or
renderOutput(generateCard($stats)); |
JSON is another type to incorporate. All JSON outputs should use this function as well. This may need to be redesigned to be written efficiently. function renderOutput(string $output, bool $error)
{
$type = $_REQUEST['type'] ?? 'svg';
// set headers and echo response based on type
} And call it like renderOutput($error->message, true);
or
renderOutput(generateCard($stats), false);
or
renderOutput(json_encode($stats), false); Send Send Maybe I'll think about it a bit more later, feel free to share your thoughts on it. |
It may be worth considering a separate function for echoing stats than for echoing an error message since they are different variable types. |
What do you think about this solution? @DenverCoder1 Looks really neat to me! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
$requestedType = $_REQUEST['type'] ?? 'svg';
is unnecessary on line 16 since it is now inside the function. -
JSON output does not work. It should display the json encoding of
$stats
if successful, otherwise{ "error": message }
.
src/index.php
Outdated
function renderOutput(string $output): void | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this function be moved to the card.php file?
Does this work for you? We can move json to another function if needed, but it's pretty clear rn. @DenverCoder1 |
src/index.php
Outdated
} | ||
renderOutput(generateCard($stats)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderOutput(generateCard($stats));
This should probably go after the last line of the try-block (right after $stats
is assigned)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per the third comment (below), the generateCard/generateErrorCard can happen withing renderOutput, so this would end up being just
renderOutput($stats);
And for error outputs, you just pass the message.
src/card.php
Outdated
if ($requestedType === "json" && $error === false) { | ||
// set content type to JSON | ||
header('Content-Type: application/json'); | ||
// echo JSON data for streak stats | ||
echo json_encode($output); | ||
// exit | ||
exit; | ||
} | ||
|
||
if ($requestedType === "json" && $error === true) { | ||
// set content type to JSON | ||
header('Content-Type: application/json'); | ||
// echo JSON error message | ||
echo json_encode(array("error" => $output)); | ||
exit; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to repeat these lines, we can do it with nesting.
This could also be extracted to an echoAsJson
method for consistency.
if ($requestedType === "json" && $error === false) { | |
// set content type to JSON | |
header('Content-Type: application/json'); | |
// echo JSON data for streak stats | |
echo json_encode($output); | |
// exit | |
exit; | |
} | |
if ($requestedType === "json" && $error === true) { | |
// set content type to JSON | |
header('Content-Type: application/json'); | |
// echo JSON error message | |
echo json_encode(array("error" => $output)); | |
exit; | |
} | |
if ($requestedType === "json") { | |
$data = $error ? array("error" => $output) : $output; | |
// set content type to JSON | |
header('Content-Type: application/json'); | |
// echo JSON data for streak stats | |
echo json_encode($data); | |
// exit | |
exit; | |
} |
src/card.php
Outdated
* @param string If a message is a error | ||
* @return void | ||
*/ | ||
function renderOutput(string $output, bool $error = false): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing $error, we could do (similar to your first idea), pass an array or string as the first parameter.
Union types are supported in PHP 8, so we can specify the parameter as array|string $output
and then to check if it is an error or stats, we can use gettype, for example, gettype($output) === "string"
The card can then be generated like:
$card = gettype($output) === "string" ? generateErrorCard($output) : generateCard($output);
And the JSON data like:
$array = gettype($output) === "string" ? array("error" => $output) : $output;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, I'm not sure if that will improve readability specially for new comers. But we can go forward with that idea. @DenverCoder1
src/index.php
Outdated
echoAsSvg($card); | ||
|
||
exit; | ||
renderOutput(generateErrorCard($message)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSON won't work unless generateErrorCard/generateCard happens within the render function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DenverCoder1 Strange... Why is this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSON does not output use the SVG card to output. It just displays the plain message/stats.
src/index.php
Outdated
echoAsSvg($card); | ||
|
||
exit; | ||
renderOutput(generateErrorCard($message)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderOutput(generateErrorCard($message)); | |
renderOutput($message); |
src/index.php
Outdated
@@ -49,27 +41,9 @@ | |||
$contributionGraphs = getContributionGraphs($_REQUEST["user"]); | |||
$contributions = getContributionDates($contributionGraphs); | |||
$stats = getContributionStats($contributions); | |||
renderOutput(generateCard($stats)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderOutput(generateCard($stats)); | |
renderOutput($stats); |
src/index.php
Outdated
echoAsSvg($card); | ||
|
||
exit; | ||
renderOutput(generateErrorCard($error->getMessage())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderOutput(generateErrorCard($error->getMessage())); | |
renderOutput($error->getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! It's working now. Just a couple minor change suggestions and it will be ready to merge.
src/card.php
Outdated
// set content type to JSON | ||
header('Content-Type: application/json'); | ||
// echo JSON error message | ||
echo json_encode($data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be extracted to a function for consistency with the other echo types.
/**
* Displays an array as JSON
*
* @param array $array The array to output
*/
function echoAsJson(array $data): void
{
// set content type to JSON
header("Content-Type: application/json");
// echo JSON data for streak stats
echo json_encode($data);
}
Looks good now. I learned some things from this PR 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple type/comment changes. (You can commit suggestions through the GitHub website btw)
Co-authored-by: Jonah Lawrence <[email protected]>
Co-authored-by: Jonah Lawrence <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now. I learned some things from this PR +1
Thanks for contributing! 🎉
Code review is a great way to learn, so I'm glad it helped.
Description
Fixes #162
Type of change
How Has This Been Tested?
composer test
Checklist:
Screenshots