Skip to content
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

Merged
merged 13 commits into from
Oct 4, 2021
Merged

Warning - headers getting sent twice #162 #163

merged 13 commits into from
Oct 4, 2021

Conversation

komen205
Copy link
Contributor

@komen205 komen205 commented Oct 3, 2021

Description

Fixes #162

Type of change

  • Bug fix (added a non-breaking change which fixes an issue)

How Has This Been Tested?

  • Tested locally with a valid username
  • Tested locally with an invalid username
  • Ran tests with composer test
  • Added or updated test cases to test new features

Checklist:

  • I have checked to make sure no other pull requests are open for this issue
  • The code is properly formatted and is consistent with the existing code style
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

Screenshots

@DenverCoder1
Copy link
Owner

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)

@komen205
Copy link
Contributor Author

komen205 commented Oct 3, 2021

What do you think about this solution?

@DenverCoder1
Copy link
Owner

DenverCoder1 commented Oct 3, 2021

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));

@DenverCoder1
Copy link
Owner

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 generateErrorCard($output) as SVG if error, just $output if no error.

Send json_encode(array("error" => $output)) as JSON if error, just $output.

Maybe I'll think about it a bit more later, feel free to share your thoughts on it.

@DenverCoder1
Copy link
Owner

It may be worth considering a separate function for echoing stats than for echoing an error message since they are different variable types.

@komen205
Copy link
Contributor Author

komen205 commented Oct 3, 2021

What do you think about this solution? @DenverCoder1 Looks really neat to me!

Copy link
Owner

@DenverCoder1 DenverCoder1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. $requestedType = $_REQUEST['type'] ?? 'svg'; is unnecessary on line 16 since it is now inside the function.

  2. JSON output does not work. It should display the json encoding of $stats if successful, otherwise { "error": message }.

src/index.php Outdated
Comment on lines 50 to 51
function renderOutput(string $output): void
{
Copy link
Owner

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?

@komen205
Copy link
Contributor Author

komen205 commented Oct 3, 2021

Does this work for you? We can move json to another function if needed, but it's pretty clear rn. @DenverCoder1

@komen205 komen205 requested a review from DenverCoder1 October 3, 2021 22:53
src/index.php Outdated
}
renderOutput(generateCard($stats));
Copy link
Owner

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)

Copy link
Owner

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
Comment on lines 16 to 31
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;
}
Copy link
Owner

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.

Suggested change
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
Copy link
Owner

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;

Copy link
Contributor Author

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

@komen205 komen205 requested a review from DenverCoder1 October 4, 2021 08:41
src/index.php Outdated
echoAsSvg($card);

exit;
renderOutput(generateErrorCard($message));
Copy link
Owner

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

Copy link
Contributor Author

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?

Copy link
Owner

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.

@komen205 komen205 requested a review from DenverCoder1 October 4, 2021 09:28
src/index.php Outdated
echoAsSvg($card);

exit;
renderOutput(generateErrorCard($message));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
renderOutput(generateCard($stats));
renderOutput($stats);

src/index.php Outdated
echoAsSvg($card);

exit;
renderOutput(generateErrorCard($error->getMessage()));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
renderOutput(generateErrorCard($error->getMessage()));
renderOutput($error->getMessage());

@komen205 komen205 requested a review from DenverCoder1 October 4, 2021 09:38
Copy link
Owner

@DenverCoder1 DenverCoder1 left a 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
Comment on lines 19 to 22
// set content type to JSON
header('Content-Type: application/json');
// echo JSON error message
echo json_encode($data);
Copy link
Owner

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);
}

@komen205
Copy link
Contributor Author

komen205 commented Oct 4, 2021

Looks good now. I learned some things from this PR 👍

@komen205 komen205 requested a review from DenverCoder1 October 4, 2021 09:54
Copy link
Owner

@DenverCoder1 DenverCoder1 left a 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)

komen205 and others added 2 commits October 4, 2021 11:15
Co-authored-by: Jonah Lawrence <[email protected]>
Co-authored-by: Jonah Lawrence <[email protected]>
@komen205 komen205 requested a review from DenverCoder1 October 4, 2021 10:15
Copy link
Owner

@DenverCoder1 DenverCoder1 left a 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.

@DenverCoder1 DenverCoder1 merged commit a74e2d8 into DenverCoder1:main Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning - headers getting sent twice
2 participants