shetabit/visitor

Visitor::userAgent() raises exception when User-Agent is not set

potofcoffee opened this issue · 1 comments

Visitor::userAgent() is declared with a strict return type of string. This raises an Exception when the underlying Request::headers->get('User-Agent') returns null because the User-Agent header is not set.

Unfortunately, this is the case with a number of bots and causes the log files to fill up with exception traces (and the bots to see an error message at best / debug info at worst).

The whole problem could be avoided by either (a) declaring the return type differently:

 public function userAgent() : ?string

Note: This requires at least PHP 7.1.

... or (b) by checking for null and returning an empty string:

return $this->request->userAgent() ?? '';

Actually, the same problem exists with other header() calls, but it regularly occurs with userAgent().

can you fix it and create a pull request? i will merge it.
thanks.