ZF-10870: Zend_Reflection_Method::getBody fails with multi-line method signature

Description

When you declare a function in multiple lines (because of the golden "80-characters-a-line"-rule for example):


function myFunction($arga, $argb, $argc,
    $argd, $arge
) {

  ...

}

"getBody" returns everything from line 2:


    $argd, $arge
) {

 ....

which is extremely poor when using Zend_CodeGenerator, which relies on this function. The attached patch resolves the issue with current 1.11.

Comments

Hmmm... don't know, how to attach something here, so here's the patch:


--- Method.php  2010-01-06 03:05:09.000000000 +0100
+++ /usr/share/php5/Zend/Reflection/Method.php  2010-12-27 16:03:48.643040872 +0100
@@ -152,7 +152,15 @@
 
         $firstLine = array_shift($lines);
 
+        while (!preg_match('/{/', $firstLine)) {
+
+            $firstLine = array_shift($lines);
+
+        }
+
         if (trim($firstLine) !== '{') {
+
+            $firstLine = preg_replace("/[^{]*{(.*)$/", "{\1", $firstLine);
             array_unshift($lines, $firstLine);
         }

The patch above is wrong (there's a special character outputted as well) This one corrects it and should also work on Windows systems:


--- Method.php  2010-01-06 03:05:09.000000000 +0100
+++ /usr/share/php5/Zend/Reflection/Method.php  2010-12-28 11:26:39.849801427 +0100
@@ -152,7 +152,15 @@
 
         $firstLine = array_shift($lines);
 
+        while (!preg_match('/{/', $firstLine)) {
+
+            $firstLine = array_shift($lines);
+
+        }
+
         if (trim($firstLine) !== '{') {
+
+            $firstLine = preg_replace("/^[^{]*{(.*$)/", '{$1', $firstLine);
             array_unshift($lines, $firstLine);
         }
 
@@ -163,6 +171,6 @@
         }
 
         // just in case we had code on the bracket lines
-        return rtrim(ltrim(implode("\n", $lines), '{'), '}');
+        return rtrim(ltrim(implode(PHP_EOL, $lines), '{'), '}');
     }
 }

Argh! Still wrong. This should do it:


--- Method.php  2010-01-06 03:05:09.000000000 +0100
+++ /usr/share/php5/Zend/Reflection/Method.php  2010-12-28 14:05:03.860674888 +0100
@@ -150,10 +150,42 @@
             true
         );
 
+        $bodyStart = 1;
+        $headerNotClosed = false;
+
+        foreach ($lines as $line) {
+
+            if (preg_match("/^[\s]*\) \{/", $line)) {
+                $headerNotClosed = true;
+                break;
+            }
+
+            $bodyStart++;
+
+        }
+
+        if ($headerNotClosed) {
+
+            for ($a = 0; $a < $bodyStart; $a++) {
+
+                array_shift($lines);
+
+            }
+
+        }
+
         $firstLine = array_shift($lines);
 
-        if (trim($firstLine) !== '{') {
-            array_unshift($lines, $firstLine);
+        if (preg_match('/{/', $firstLine)) {
+
+            $firstLine = trim(preg_replace('/^[^{]*{(.*$)/', '$1', $firstLine));
+
+            if (!empty($firstline)) {
+
+                array_unshift($lines, $firstLine);
+
+            }
+
         }
 
         $lastLine = array_pop($lines);
@@ -163,6 +195,6 @@
         }
 
         // just in case we had code on the bracket lines
-        return rtrim(ltrim(implode("\n", $lines), '{'), '}');
+        return rtrim(ltrim(implode(PHP_EOL, $lines), '{'), '}');
     }
 }

Dennis, have you signed a CLA? Patches can't be accepted without a signed CLA. Your JIRA profile doesn't have you tagged to the dev group.

[Edit: Code removed]

Complete patch is attached.

Added updated patch (v2) to also work with inline methods (ie: signature and body on same line)

Fixed in trunk (1.12.0): r24870