Fix various PHP code bugs found using static analysis (#1489086)