THRIFT-4463: Generated PHP code to match PSR-2
Client: php

* PHP ident with 4 space
* remove redundancy blank lines
* open bracket of function, class at new line
* remove hard-coded indent, use indent()
* add necessary visibility declaration for class field
* open bracket of flow control put end of line, not new line
* remove inline control structures
* add blank around equal symbol

This closes #1473
diff --git a/compiler/cpp/src/thrift/generate/t_generator.cc b/compiler/cpp/src/thrift/generate/t_generator.cc
index 0c1f49d..7549d5d 100644
--- a/compiler/cpp/src/thrift/generate/t_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_generator.cc
@@ -101,21 +101,22 @@
                                              const string& line_prefix,
                                              const string& contents,
                                              const string& comment_end) {
-  if (comment_start != "")
+  if (!comment_start.empty())
     indent(out) << comment_start;
   stringstream docs(contents, ios_base::in);
   while (!(docs.eof() || docs.fail())) {
     char line[1024];
     docs.getline(line, 1024);
 
-    // Just prnt a newline when the line & prefix are empty.
-    if (strlen(line) == 0 && line_prefix == "" && !docs.eof()) {
-      out << std::endl;
-    } else if (strlen(line) > 0 || !docs.eof()) { // skip the empty last line
+    if (strlen(line) > 0) {
       indent(out) << line_prefix << line << std::endl;
+    } else if (line_prefix.empty()){
+      out << std::endl;
+    } else if(!docs.eof()) {
+      indent(out) << line_prefix << std::endl;
     }
   }
-  if (comment_end != "")
+  if (!comment_end.empty())
     indent(out) << comment_end;
 }
 
diff --git a/compiler/cpp/src/thrift/generate/t_php_generator.cc b/compiler/cpp/src/thrift/generate/t_php_generator.cc
index 11771c2..5166d03 100644
--- a/compiler/cpp/src/thrift/generate/t_php_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_php_generator.cc
@@ -93,6 +93,10 @@
     escape_['$'] = "\\$";
   }
 
+  virtual std::string indent_str() const {
+    return "    ";
+  }
+
   static bool is_valid_namespace(const std::string& sub_namespace);
 
   /**
@@ -442,7 +446,7 @@
                 "use stdClass;\n";
   }
 
-  return includes + "\n";
+  return includes;
 }
 
 /**
@@ -451,7 +455,6 @@
 void t_php_generator::close_generator() {
   if (!psr4_) {
     // Close types file
-    f_types_ << endl;
     f_types_.close();
   }
 }
@@ -471,7 +474,8 @@
 void t_php_generator::generate_service_header(t_service* tservice, std::ofstream& file) {
   file << "<?php" << endl;
   if (!php_namespace_suffix(tservice->get_program()).empty()) {
-    file << "namespace " << php_namespace_suffix(tservice->get_program()) << ";" << endl;
+    file << "namespace " << php_namespace_suffix(tservice->get_program()) << ";" << endl
+         << endl;
   }
   file << autogen_comment() << php_includes();
 
@@ -484,7 +488,8 @@
 void t_php_generator::generate_program_header(std::ofstream& file) {
   file << "<?php" << endl;
   if (!php_namespace_suffix(get_program()).empty()) {
-    file << "namespace " << php_namespace_suffix(get_program()) << ";" << endl << endl;
+    file << "namespace " << php_namespace_suffix(get_program()) << ";" << endl
+         << endl;
   }
   file << autogen_comment() << php_includes();
 
@@ -512,20 +517,25 @@
   // code but you can't do things like an 'extract' on it, which is a bit of
   // a downer.
   generate_php_doc(f_enum, tenum);
-  f_enum << "final class " << tenum->get_name() << " {" << endl;
+  f_enum << "final class " << tenum->get_name() << endl
+         << "{" << endl;
   indent_up();
 
   for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
     int value = (*c_iter)->get_value();
     generate_php_doc(f_enum, *c_iter);
-    indent(f_enum) << "const " << (*c_iter)->get_name() << " = " << value << ";" << endl;
+    indent(f_enum) << "const " << (*c_iter)->get_name() << " = " << value << ";" << endl
+                   << endl;
   }
 
   indent(f_enum) << "static public $__names = array(" << endl;
+
+  indent_up();
   for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
     int value = (*c_iter)->get_value();
-    indent(f_enum) << "  " << value << " => '" << (*c_iter)->get_name() << "'," << endl;
+    indent(f_enum) << value << " => '" << (*c_iter)->get_name() << "'," << endl;
   }
+  indent_down();
   indent(f_enum) << ");" << endl;
 
   indent_down();
@@ -553,7 +563,8 @@
       f_consts.open(f_consts_name.c_str());
       generate_program_header(f_consts);
     }
-    f_consts << "final class Constant extends \\Thrift\\Type\\TConstant {" << endl;
+    f_consts << "final class Constant extends \\Thrift\\Type\\TConstant"<< endl
+             << "{" << endl;
 
     indent_up();
 
@@ -570,7 +581,8 @@
 
       f_consts << endl;
 
-      indent(f_consts) << "static protected function init_" << name << "() {" << endl;
+      f_consts << indent() << "protected static function init_" << name << "()" <<endl
+               << indent() << "{" << endl;
       indent_up();
 
       indent(f_consts) << "return ";
@@ -583,7 +595,7 @@
     }
 
     indent_down();
-    f_consts << "}" << endl << endl;
+    f_consts << "}" << endl;
     if (psr4_) {
       f_consts.close();
     }
@@ -771,7 +783,7 @@
  * type information to generalize serialization routines.
  */
 void t_php_generator::generate_php_struct_spec(ofstream& out, t_struct* tstruct) {
-  indent(out) << "static $_TSPEC = array(" << endl;
+  indent(out) << "static public $_TSPEC = array(" << endl;
   indent_up();
 
   const vector<t_field*>& members = tstruct->get_members();
@@ -783,12 +795,12 @@
     out << indent() << "'var' => '" << (*m_iter)->get_name() << "'," << endl;
     out << indent() << "'isRequired' => " << ((*m_iter)->get_req() == t_field::T_REQUIRED ? "true" : "false") << "," << endl;
     generate_php_type_spec(out, t);
-    indent(out) << ")," << endl;
     indent_down();
+    indent(out) << ")," << endl;
   }
 
   indent_down();
-  indent(out) << "  );" << endl << endl;
+  indent(out) << ");" << endl << endl;
 }
 
 /**
@@ -817,10 +829,11 @@
   if (json_serializable_) {
     out << " implements JsonSerializable";
   }
-  out << " {" << endl;
+  out << endl
+      << "{" << endl;
   indent_up();
 
-  out << indent() << "static $isValidate = " << (validate_ ? "true" : "false") << ";" << endl << endl;
+  out << indent() << "static public $isValidate = " << (validate_ ? "true" : "false") << ";" << endl << endl;
 
   generate_php_struct_spec(out, tstruct);
 
@@ -837,8 +850,9 @@
   out << endl;
 
   // Generate constructor from array
-  string param = (members.size() > 0) ? "$vals=null" : "";
-  out << indent() << "public function __construct(" << param << ") {" << endl;
+  string param = (members.size() > 0) ? "$vals = null" : "";
+  out << indent() << "public function __construct(" << param << ")"<< endl
+      << indent() << "{" << endl;
   indent_up();
 
   if (members.size() > 0) {
@@ -855,9 +869,14 @@
       out << indent() << "parent::__construct(self::$_TSPEC, $vals);" << endl;
     } else {
       for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
-        out << indent() << "if (isset($vals['" << (*m_iter)->get_name() << "'])) {" << endl
-            << indent() << "  $this->" << (*m_iter)->get_name() << " = $vals['"
-            << (*m_iter)->get_name() << "'];" << endl << indent() << "}" << endl;
+        out << indent() << "if (isset($vals['" << (*m_iter)->get_name() << "'])) {" << endl;
+
+        indent_up();
+        out << indent() << "$this->" << (*m_iter)->get_name() << " = $vals['"
+            << (*m_iter)->get_name() << "'];" << endl;
+
+        indent_down();
+        out << indent() << "}" << endl;
       }
     }
     indent_down();
@@ -866,23 +885,34 @@
   scope_down(out);
   out << endl;
 
-  out << indent() << "public function getName() {" << endl << indent() << "  return '"
-      << tstruct->get_name() << "';" << endl << indent() << "}" << endl << endl;
+  out << indent() << "public function getName()" << endl
+      << indent() << "{" << endl;
 
+  indent_up();
+  out << indent() << "return '" << tstruct->get_name() << "';" << endl;
+
+  indent_down();
+  out << indent() << "}" << endl << endl;
+
+  out << endl;
   generate_php_struct_reader(out, tstruct, is_result);
+  out << endl;
   generate_php_struct_writer(out, tstruct, is_result);
   if (needs_php_read_validator(tstruct, is_result)) {
+    out << endl;
     generate_php_struct_read_validator(out, tstruct);
   }
   if (needs_php_write_validator(tstruct, is_result)) {
+    out << endl;
     generate_php_struct_write_validator(out, tstruct);
   }
   if (json_serializable_) {
+    out << endl;
     generate_php_struct_json_serialize(out, tstruct, is_result);
   }
 
   indent_down();
-  out << indent() << "}" << endl << endl;
+  out << indent() << "}" << endl;
 }
 
 /**
@@ -919,9 +949,9 @@
   }
 
   // Loop over reading in fields
-  indent(out) << "while (true)" << endl;
+  indent(out) << "while (true) {" << endl;
 
-  scope_up(out);
+  indent_up();
 
   // Read beginning field marker
   if (binary_inline_) {
@@ -943,9 +973,9 @@
   }
 
   // Switch statement on the field we are reading
-  indent(out) << "switch ($fid)" << endl;
+  indent(out) << "switch ($fid) {" << endl;
 
-  scope_up(out);
+  indent_up();
 
   // Generate deserialization code for known cases
   for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -956,25 +986,31 @@
     generate_deserialize_field(out, *f_iter, "this->");
     indent_down();
     out << indent() << "} else {" << endl;
+
+    indent_up();
     if (binary_inline_) {
-      indent(out) << "  $xfer += "
-                  << "TProtocol::skipBinary($input, $ftype);" << endl;
+      indent(out) << "$xfer += TProtocol::skipBinary($input, $ftype);" << endl;
     } else {
-      indent(out) << "  $xfer += $input->skip($ftype);" << endl;
+      indent(out) << "$xfer += $input->skip($ftype);" << endl;
     }
+
+    indent_down();
     out << indent() << "}" << endl << indent() << "break;" << endl;
     indent_down();
   }
 
   // In the default case we skip the field
   indent(out) << "default:" << endl;
+
+  indent_up();
   if (binary_inline_) {
-    indent(out) << "  $xfer += "
+    indent(out) << "$xfer += "
                 << "TProtocol::skipBinary($input, $ftype);" << endl;
   } else {
-    indent(out) << "  $xfer += $input->skip($ftype);" << endl;
+    indent(out) << "$xfer += $input->skip($ftype);" << endl;
   }
-  indent(out) << "  break;" << endl;
+  indent(out) << "break;" << endl;
+  indent_down();
 
   scope_down(out);
 
@@ -996,7 +1032,7 @@
   indent(out) << "return $xfer;" << endl;
 
   indent_down();
-  out << indent() << "}" << endl << endl;
+  out << indent() << "}" << endl;
 }
 
 /**
@@ -1008,10 +1044,11 @@
   vector<t_field*>::const_iterator f_iter;
 
   if (binary_inline_) {
-    indent(out) << "public function write(&$output) {" << endl;
+    indent(out) << "public function write(&$output)" << endl;
   } else {
-    indent(out) << "public function write($output) {" << endl;
+    indent(out) << "public function write($output)" << endl;
   }
+  indent(out) << "{" << endl;
   indent_up();
 
   if (needs_php_write_validator(tstruct, is_result)) {
@@ -1086,7 +1123,7 @@
   out << indent() << "return $xfer;" << endl;
 
   indent_down();
-  out << indent() << "}" << endl << endl;
+  out << indent() << "}" << endl;
 }
 
 void t_php_generator::generate_php_struct_read_validator(ofstream& out, t_struct* tstruct) {
@@ -1124,7 +1161,7 @@
   }
 
   indent_down();
-  indent(out) << "}" << endl << endl;
+  indent(out) << "}" << endl;
 }
 
 void t_php_generator::generate_php_struct_json_serialize(ofstream& out,
@@ -1173,7 +1210,7 @@
   indent(out) << "return $json;" << endl;
   indent_down();
 
-  indent(out) << "}" << endl << endl;
+  indent(out) << "}" << endl;
 }
 
 int t_php_generator::get_php_num_required_fields(const vector<t_field*>& fields, bool write_mode) {
@@ -1257,23 +1294,30 @@
   }
 
   // Generate the header portion
-  f_service_processor << "class " << service_name_ << "Processor" << extends_processor << " {" << endl;
+  f_service_processor << "class " << service_name_ << "Processor" << extends_processor << endl
+                      << "{" << endl;
   indent_up();
 
   if (extends.empty()) {
     f_service_processor << indent() << "protected $handler_ = null;" << endl;
   }
 
-  f_service_processor << indent() << "public function __construct($handler) {" << endl;
+  f_service_processor << indent() << "public function __construct($handler)"<< endl
+                      << indent() << "{" << endl;
+
+  indent_up();
   if (extends.empty()) {
-    f_service_processor << indent() << "  $this->handler_ = $handler;" << endl;
+    f_service_processor << indent() << "$this->handler_ = $handler;" << endl;
   } else {
-    f_service_processor << indent() << "  parent::__construct($handler);" << endl;
+    f_service_processor << indent() << "parent::__construct($handler);" << endl;
   }
+
+  indent_down();
   f_service_processor << indent() << "}" << endl << endl;
 
   // Generate the server implementation
-  indent(f_service_processor) << "public function process($input, $output) {" << endl;
+  f_service_processor << indent() << "public function process($input, $output)" << endl
+                      << indent() << "{" << endl;
   indent_up();
 
   f_service_processor << indent() << "$rseqid = 0;" << endl << indent() << "$fname = null;" << endl
@@ -1291,11 +1335,12 @@
   }
 
   // HOT: check for method implementation
-  f_service_processor << indent() << "$methodname = 'process_'.$fname;" << endl << indent()
-                      << "if (!method_exists($this, $methodname)) {" << endl;
+  f_service_processor << indent() << "$methodname = 'process_'.$fname;" << endl
+                      << indent() << "if (!method_exists($this, $methodname)) {" << endl;
+
+  indent_up();
   if (binary_inline_) {
-    f_service_processor << indent() << "  throw new \\Exception('Function '.$fname.' not implemented.');"
-                        << endl;
+    f_service_processor << indent() << "throw new \\Exception('Function '.$fname.' not implemented.');" << endl;
   } else {
     f_service_processor << indent() << "  $input->skip("
                         << "TType::STRUCT);" << endl << indent() << "  $input->readMessageEnd();" << endl
@@ -1308,9 +1353,12 @@
                         << endl << indent() << "  $output->getTransport()->flush();" << endl << indent()
                         << "  return;" << endl;
   }
-  f_service_processor << indent() << "}" << endl << indent()
-                      << "$this->$methodname($rseqid, $input, $output);" << endl << indent()
-                      << "return true;" << endl;
+
+  indent_down();
+  f_service_processor << indent() << "}" << endl
+                      << indent() << "$this->$methodname($rseqid, $input, $output);" << endl
+                      << indent() << "return true;" << endl;
+
   indent_down();
   f_service_processor << indent() << "}" << endl << endl;
 
@@ -1334,8 +1382,8 @@
  */
 void t_php_generator::generate_process_function(std::ofstream& out, t_service* tservice, t_function* tfunction) {
   // Open function
-  indent(out) << "protected function process_" << tfunction->get_name()
-                     << "($seqid, $input, $output) {" << endl;
+  out << indent() << "protected function process_" << tfunction->get_name() << "($seqid, $input, $output)" << endl
+      << indent() << "{" << endl;
   indent_up();
 
   string argsname = php_namespace(tservice->get_program()) + service_name_ + "_"
@@ -1346,21 +1394,32 @@
   out << indent() << "$bin_accel = ($input instanceof "
              << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary_after_message_begin');"
              << endl;
-  out << indent() << "if ($bin_accel)" << endl;
-  scope_up(out);
+  out << indent() << "if ($bin_accel) {" << endl;
+  indent_up();
 
-  out << indent() << "$args = thrift_protocol_read_binary_after_message_begin($input, '" << argsname
-             << "', $input->isStrictRead());" << endl;
+  out << indent() << "$args = thrift_protocol_read_binary_after_message_begin(" <<endl;
 
-  scope_down(out);
-  out << indent() << "else" << endl;
-  scope_up(out);
-  out << indent() << "$args = new " << argsname << "();" << endl << indent()
-             << "$args->read($input);" << endl;
+  indent_up();
+  out << indent() << "$input,"<<endl
+      << indent() << "'" << argsname << "'," << endl
+      << indent() << "$input->isStrictRead()" <<endl;
+
+  indent_down();
+  out << indent() <<");" << endl;
+
+  indent_down();
+  out << indent() << "} else {" << endl;
+
+  indent_up();
+  out << indent() << "$args = new " << argsname << "();" << endl
+      << indent() << "$args->read($input);" << endl;
+
+  indent_down();
+  out << indent() << "}" << endl;
+
   if (!binary_inline_) {
     out << indent() << "$input->readMessageEnd();" << endl;
   }
-  scope_down(out);
 
   t_struct* xs = tfunction->get_xceptions();
   const std::vector<t_field*>& xceptions = xs->get_members();
@@ -1428,16 +1487,25 @@
              << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary');"
              << endl;
 
-  out << indent() << "if ($bin_accel)" << endl;
-  scope_up(out);
+  out << indent() << "if ($bin_accel) {" << endl;
+  indent_up();
 
-  out << indent() << "thrift_protocol_write_binary($output, '" << tfunction->get_name()
-             << "', "
-             << "TMessageType::REPLY, $result, $seqid, $output->isStrictWrite());" << endl;
+  out << indent() << "thrift_protocol_write_binary(" << endl;
 
-  scope_down(out);
-  out << indent() << "else" << endl;
-  scope_up(out);
+  indent_up();
+  out << indent() << "$output,"<<endl
+      << indent() << "'" << tfunction->get_name()<< "'," <<endl
+      << indent() << "TMessageType::REPLY,"<< endl
+      << indent() << "$result," << endl
+      << indent() << "$seqid," << endl
+      << indent() << "$output->isStrictWrite()"<<endl;
+
+  indent_down();
+  out << indent() << ");" << endl;
+
+  indent_down();
+  out << indent() << "} else {" << endl;
+  indent_up();
 
   // Serialize the request header
   if (binary_inline_) {
@@ -1551,8 +1619,9 @@
                  + tservice->get_extends()->get_name() + "If";
   }
   generate_php_doc(f_service_interface, tservice);
-  f_service_interface << "interface " << php_namespace_declaration(tservice) << "If" << extends_if << " {"
-             << endl;
+  f_service_interface << "interface " << php_namespace_declaration(tservice) << "If" << extends_if << endl
+                      << "{" << endl;
+
   indent_up();
   vector<t_function*> functions = tservice->get_functions();
   vector<t_function*>::iterator f_iter;
@@ -1561,10 +1630,9 @@
     indent(f_service_interface) << "public function " << function_signature(*f_iter) << ";" << endl;
   }
   indent_down();
-  f_service_interface << "}" << endl << endl;
+  f_service_interface << "}" << endl;
 
   // Close service interface file
-  f_service_interface << endl;
   if (psr4_) {
     f_service_interface.close();
   }
@@ -1589,7 +1657,8 @@
     extends_if = " extends " + php_namespace(tservice->get_extends()->get_program())
                  + tservice->get_extends()->get_name() + "Rest";
   }
-  f_service_rest << "class " << service_name_ << "Rest" << extends_if << " {" << endl;
+  f_service_rest << "class " << service_name_ << "Rest" << extends_if << endl
+                 << "{" << endl;
   indent_up();
 
   if (extends.empty()) {
@@ -1670,8 +1739,8 @@
   }
 
   f_service_client << "class " << php_namespace_declaration(tservice) << "Client" << extends_client
-             << " implements " << php_namespace(tservice->get_program()) << service_name_ << "If {"
-             << endl;
+             << " implements " << php_namespace(tservice->get_program()) << service_name_ << "If" << endl
+             <<"{"<< endl;
   indent_up();
 
   // Private members
@@ -1682,13 +1751,18 @@
   }
 
   // Constructor function
-  f_service_client << indent() << "public function __construct($input, $output=null) {" << endl;
+  f_service_client << indent() << "public function __construct($input, $output = null)" << endl
+                   << indent() << "{" << endl;
+
+  indent_up();
   if (!extends.empty()) {
-    f_service_client << indent() << "  parent::__construct($input, $output);" << endl;
+    f_service_client << indent() << "parent::__construct($input, $output);" << endl;
   } else {
-    f_service_client << indent() << "  $this->input_ = $input;" << endl << indent()
-               << "  $this->output_ = $output ? $output : $input;" << endl;
+    f_service_client << indent() << "$this->input_ = $input;" << endl
+                     << indent() << "$this->output_ = $output ? $output : $input;" << endl;
   }
+
+  indent_down();
   f_service_client << indent() << "}" << endl << endl;
 
   // Generate client method implementations
@@ -1700,6 +1774,8 @@
     vector<t_field*>::const_iterator fld_iter;
     string funname = (*f_iter)->get_name();
 
+    f_service_client << endl;
+
     // Open function
     indent(f_service_client) << "public function " << function_signature(*f_iter) << endl;
     scope_up(f_service_client);
@@ -1743,18 +1819,27 @@
                << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary');"
                << endl;
 
-    f_service_client << indent() << "if ($bin_accel)" << endl;
-    scope_up(f_service_client);
+    f_service_client << indent() << "if ($bin_accel) {" << endl;
+    indent_up();
 
     string messageType = (*f_iter)->is_oneway() ? "TMessageType::ONEWAY" : "TMessageType::CALL";
 
-    f_service_client << indent() << "thrift_protocol_write_binary($this->output_, '"
-               << (*f_iter)->get_name() << "', " << messageType
-               << ", $args, $this->seqid_, $this->output_->isStrictWrite());" << endl;
+    f_service_client << indent() << "thrift_protocol_write_binary(" << endl;
 
-    scope_down(f_service_client);
-    f_service_client << indent() << "else" << endl;
-    scope_up(f_service_client);
+    indent_up();
+    f_service_client << indent() << "$this->output_," << endl
+               << indent() << "'" << (*f_iter)->get_name() << "'," << endl
+               << indent() << messageType << "," << endl
+               << indent() << "$args," << endl
+               << indent() << "$this->seqid_," << endl
+               << indent() << "$this->output_->isStrictWrite()" << endl;
+
+    indent_down();
+    f_service_client << indent() << ");" << endl;
+
+    indent_down();
+    f_service_client << indent() << "} else {" << endl;
+    indent_up();
 
     // Serialize the request header
     if (binary_inline_) {
@@ -1799,14 +1884,26 @@
                        << "TBinaryProtocolAccelerated)"
                        << " && function_exists('thrift_protocol_read_binary');" << endl;
 
-      f_service_client << indent()
-                       << "if ($bin_accel) $result = thrift_protocol_read_binary($this->input_, '"
-                       << resultname << "', $this->input_->isStrictRead());" << endl;
-      f_service_client << indent() << "else" << endl;
-      scope_up(f_service_client);
+      f_service_client << indent() << "if ($bin_accel) {" << endl;
 
-      f_service_client << indent() << "$rseqid = 0;" << endl << indent() << "$fname = null;" << endl
-                 << indent() << "$mtype = 0;" << endl << endl;
+      indent_up();
+      f_service_client << indent() << "$result = thrift_protocol_read_binary(" << endl;
+
+      indent_up();
+      f_service_client << indent() << "$this->input_," << endl
+                       << indent() << "'" << resultname << "'," << endl
+                       << indent() << "$this->input_->isStrictRead()" << endl;
+
+      indent_down();
+      f_service_client << indent() << ");" << endl;
+
+      indent_down();
+      f_service_client << indent() << "} else {" << endl;
+
+      indent_up();
+      f_service_client << indent() << "$rseqid = 0;" << endl
+                       << indent() << "$fname = null;" << endl
+                       << indent() << "$mtype = 0;" << endl << endl;
 
       if (binary_inline_) {
         t_field ffname(g_type_string, "fname");
@@ -1820,16 +1917,20 @@
         generate_deserialize_field(f_service_client, &ffname, "", true);
         generate_deserialize_field(f_service_client, &fseqid, "", true);
       } else {
-        f_service_client << indent() << "$this->input_->readMessageBegin($fname, $mtype, $rseqid);"
-                         << endl << indent() << "if ($mtype == "
-                         << "TMessageType::EXCEPTION) {" << endl << indent() << "  $x = new "
-                         << "TApplicationException();" << endl << indent() << "  $x->read($this->input_);"
-                         << endl << indent() << "  $this->input_->readMessageEnd();" << endl << indent()
-                         << "  throw $x;" << endl << indent() << "}" << endl;
+        f_service_client << indent() << "$this->input_->readMessageBegin($fname, $mtype, $rseqid);" << endl
+                         << indent() << "if ($mtype == TMessageType::EXCEPTION) {" << endl;
+
+        indent_up();
+        f_service_client << indent() << "$x = new TApplicationException();" << endl
+                         << indent() << "$x->read($this->input_);" << endl
+                         << indent() << "$this->input_->readMessageEnd();" << endl
+                         << indent() << "throw $x;" << endl;
+        indent_down();
+        f_service_client << indent() << "}" << endl;
       }
 
-      f_service_client << indent() << "$result = new " << resultname << "();" << endl << indent()
-                       << "$result->read($this->input_);" << endl;
+      f_service_client << indent() << "$result = new " << resultname << "();" << endl
+                       << indent() << "$result->read($this->input_);" << endl;
 
       if (!binary_inline_) {
         f_service_client << indent() << "$this->input_->readMessageEnd();" << endl;
@@ -1839,17 +1940,26 @@
 
       // Careful, only return result if not a void function
       if (!(*f_iter)->get_returntype()->is_void()) {
-        f_service_client << indent() << "if ($result->success !== null) {" << endl << indent()
-                   << "  return $result->success;" << endl << indent() << "}" << endl;
+        f_service_client << indent() << "if ($result->success !== null) {" << endl;
+
+        indent_up();
+        f_service_client << indent() << "return $result->success;" << endl;
+
+        indent_down();
+        f_service_client << indent() << "}" << endl;
       }
 
       t_struct* xs = (*f_iter)->get_xceptions();
       const std::vector<t_field*>& xceptions = xs->get_members();
       vector<t_field*>::const_iterator x_iter;
       for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
-        f_service_client << indent() << "if ($result->" << (*x_iter)->get_name() << " !== null) {" << endl
-                         << indent() << "  throw $result->" << (*x_iter)->get_name() << ";" << endl
-                         << indent() << "}" << endl;
+        f_service_client << indent() << "if ($result->" << (*x_iter)->get_name() << " !== null) {" << endl;
+
+        indent_up();
+        f_service_client << indent() << "throw $result->" << (*x_iter)->get_name() << ";" << endl;
+
+        indent_down();
+        f_service_client << indent() << "}" << endl;
       }
 
       // Careful, only return _result if not a void function
@@ -1862,15 +1972,13 @@
 
       // Close function
       scope_down(f_service_client);
-      f_service_client << endl;
     }
   }
 
   indent_down();
-  f_service_client << "}" << endl << endl;
+  f_service_client << "}" << endl;
 
   // Close service client file
-  f_service_client << endl;
   if (psr4_) {
     f_service_client.close();
   }
@@ -2065,9 +2173,9 @@
 
   // For loop iterates over elements
   string i = tmp("_i");
-  indent(out) << "for ($" << i << " = 0; $" << i << " < $" << size << "; ++$" << i << ")" << endl;
+  indent(out) << "for ($" << i << " = 0; $" << i << " < $" << size << "; ++$" << i << ") {" << endl;
 
-  scope_up(out);
+  indent_up();
 
   if (ttype->is_map()) {
     generate_deserialize_map_element(out, (t_map*)ttype, prefix);
@@ -2258,8 +2366,6 @@
  * Writes out a container
  */
 void t_php_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
-  scope_up(out);
-
   if (ttype->is_map()) {
     if (binary_inline_) {
       out << indent() << "$output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_key_type())
@@ -2295,21 +2401,19 @@
     }
   }
 
-  scope_up(out);
-
   if (ttype->is_map()) {
     string kiter = tmp("kiter");
     string viter = tmp("viter");
     indent(out) << "foreach ($" << prefix << " as "
-                << "$" << kiter << " => $" << viter << ")" << endl;
-    scope_up(out);
+                << "$" << kiter << " => $" << viter << ") {" << endl;
+    indent_up();
     generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
     scope_down(out);
   } else if (ttype->is_set()) {
     string iter = tmp("iter");
     string iter_val = tmp("iter");
-    indent(out) << "foreach ($" << prefix << " as $" << iter << " => $" << iter_val << ")" << endl;
-    scope_up(out);
+    indent(out) << "foreach ($" << prefix << " as $" << iter << " => $" << iter_val << ") {" << endl;
+    indent_up();
 
     t_type* elem_type = ((t_set*)ttype)->get_elem_type();
     if(php_is_scalar(elem_type)) {
@@ -2320,14 +2424,12 @@
     scope_down(out);
   } else if (ttype->is_list()) {
     string iter = tmp("iter");
-    indent(out) << "foreach ($" << prefix << " as $" << iter << ")" << endl;
-    scope_up(out);
+    indent(out) << "foreach ($" << prefix << " as $" << iter << ") {" << endl;
+    indent_up();
     generate_serialize_list_element(out, (t_list*)ttype, iter);
     scope_down(out);
   }
 
-  scope_down(out);
-
   if (!binary_inline_) {
     if (ttype->is_map()) {
       indent(out) << "$output->writeMapEnd();" << endl;
@@ -2337,8 +2439,6 @@
       indent(out) << "$output->writeListEnd();" << endl;
     }
   }
-
-  scope_down(out);
 }
 
 /**