Fix a bunch of problems and bugs with code found by php-analyzer.