patternphpMajor
Shielding your CodeReview on GitHub
Viewed 0 times
shieldinggithubyourcodereview
Problem
In response to a feature-request on meta, I spent a few hours today creating the feature in php.
This application adds a review shield to your GitHub repository (or wherever else you want it).
It looks like the following:
To use it, use standard image/link markdown, like this:
Replace
You can also use the Code Review Shield Creator to create your shield.
I used shields.io to get the basic SVG XML and adapted that.
There are different ways the badge can be shown:
How the code works
Because of the daily Stack Exchange API limit of 10,000 requests, I am avoiding too many API requests by storing previous results in a database table, and only performing a new request if it has been more than an hour since the last API request for that particular question.
The code contains these functions:
This application adds a review shield to your GitHub repository (or wherever else you want it).
It looks like the following:
To use it, use standard image/link markdown, like this:
[](https://codereview.stackexchange.com/q/54737/31562)Replace
54737 with the question id you want to direct to, and replace 31562 with your user id (so that you can get the Announcer / Booster / Publicist badges).You can also use the Code Review Shield Creator to create your shield.
I used shields.io to get the basic SVG XML and adapted that.
There are different ways the badge can be shown:
- Unanswered questions shows the question score and a red background
- Answered questions shows the number of answers and an orange background
- Questions with accepted answer shows the view count and a green background
How the code works
Because of the daily Stack Exchange API limit of 10,000 requests, I am avoiding too many API requests by storing previous results in a database table, and only performing a new request if it has been more than an hour since the last API request for that particular question.
The code contains these functions:
buildURL($apiCall, $site, $filter, $apiKey): Creates the URL for the Stack Exchange API call (for future use, I check if the apiCall parameter contains a '?' or not).
apiCall($apiCall, $site, $filter): Performs a HTTP request to the Stack Exchange API usingcurl, returns JSON data as a pure string
fetchQuestion($qid, $db): Uses the JSON data retrieved byapiCallas an associative array, extracts the interesting data from it, updates the database, and callsuseData.
useData($data): Takes an associative array and creates SVG XML for it
dbOrAPI($qid, $db): Main point of entry.$qidis a Code Review question id and$dbis a PDO object. Checks the
Solution
On a personal note, your code is really clean, the idea is brilliant, and I really hope to see it implemented soon.
Your code is well implemented and structured, but there's syntax points that could be improved.
-
I see a lot of basic
See the following examples for usage:
I do believe some versions of PHP support
-
There's a few points that are either inconsistent or don't adhere to standards:
Should be
Switching between implementing the variable directly in the string
-
Using
-
You have two blank lines above your
-
in
-
You could consider keeping the SVG in another file and replacing in your variables, but I'm not 100% on its standing as a code standard / best practice.
-
In
-
You retrieve and store quite a few variables for each post that aren't currently used in the final image:
Although I can see future updates using these, and capturing them now is great, but, you could look at modifying that.
On the topic of future implementations, a score counter on the button would be pretty cool too.
-
You could consider implementing a
Your code is well implemented and structured, but there's syntax points that could be improved.
-
I see a lot of basic
if-else statements, if you're into ternaries, using them really slims down these statements, but it's at the cost of readability.See the following examples for usage:
($time errorInfo()));I do believe some versions of PHP support
(a == a ?: doStuff()); syntax also, however, I could be mistaken.-
There's a few points that are either inconsistent or don't adhere to standards:
$apiCall = $apiCall + "?dummy";Should be
$apiCall .= "?dummy";, and you shouldn't use + when concatenating strings, it's best to use . instead.Switching between implementing the variable directly in the string
'words$varmorewords' or adding it like:. $var . , I would recommend the latter as it's more reader-friendly, and because the former can have issues, it's best to wrap in curly braces: 'words{$var}morewords' in place of the former.-
Using
curl instead of get_file_contents is great, I see a lot of people make that mistake, and I've even too.-
You have two blank lines above your
return $json statement in fetchQuestion(), they don't need to be there.-
in
useData(), you create the variable $is_answered and then never use it, and I'd suggest replacing its value with $data['accepted_answer_id'] so that your if loop looks cleaner.-
You could consider keeping the SVG in another file and replacing in your variables, but I'm not 100% on its standing as a code standard / best practice.
-
In
useData(), rather than doing a double check (isset: (returns true for '') and != 0), you can just compare to > 0.-
You retrieve and store quite a few variables for each post that aren't currently used in the final image:
"is_answered" : Not used, "view_count": Used, "favorite_count": Not used, "answer_count": Not used, "score": Not used, "accepted_answer_id": Not used.Although I can see future updates using these, and capturing them now is great, but, you could look at modifying that.
On the topic of future implementations, a score counter on the button would be pretty cool too.
-
You could consider implementing a
namespace and class like structure into your project so it can be used externally, easier.Code Snippets
($time < time() - 3600 ? fetchQuestion($qid, $db) : useData($row));
($result ? useData($question) : print_r($stmt->errorInfo()));$apiCall = $apiCall + "?dummy";Context
StackExchange Code Review Q#95459, answer score: 48
Revisions (0)
No revisions yet.