Scrutinizer fixes

Fixes to problems detected with Scrutinizer.

Based on top of https://github.com/Elgg/Elgg/pull/6255, merge that one first.