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
if-elsestatements, 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 < time() - 3600 ? fetchQuestion($qid, $db) : useData($row));
($result ? useData($question) : print_r($stmt->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
curlinstead ofget_file_contentsis great, I see a lot of people make that mistake, and I've even too.You have two blank lines above your
return $jsonstatement infetchQuestion(), they don't need to be there.in
useData(), you create the variable$is_answeredand then never use it, and I'd suggest replacing it'sits value with$data['accepted_answer_id']so that yourifloop looks cleaner.You could consider keeping the SVG in another file and replacing in your variables, but I'm not 100% on it'sits 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
namespaceand class like structure into your project so it can be used externally, easier.