Issues

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

Issue Type: Bug Created: 2010-12-27T07:08:36.000+0000 Last Updated: 2012-06-02T02:16:54.000+0000 Status: Resolved Fix version(s): Reporter: Dennis Ploeger (ploeger) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Reflection

  • FixForZF1.12
  • zf-caretaker-adamlundrigan

Related issues: - ZF-9018

Attachments: - ZF-10870.patch

Description

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

<pre class="highlight">
function myFunction($arga, $argb, $argc,
    $argd, $arge
) {

  ...

}

"getBody" returns everything from line 2:

<pre class="highlight">
    $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

Posted by Dennis Ploeger (ploeger) on 2010-12-27T07:11:49.000+0000

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

<pre class="highlight">
--- 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);
         }

Posted by Dennis Ploeger (ploeger) on 2010-12-28T02:28:11.000+0000

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

<pre class="highlight">
--- 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), '{'), '}');
     }
 }

Posted by Dennis Ploeger (ploeger) on 2010-12-28T05:07:15.000+0000

Argh! Still wrong. This should do it:

<pre class="highlight">
--- 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), '{'), '}');
     }
 }

Posted by Marc Hodgins (mjh_ca) on 2010-12-29T01:44:14.000+0000

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.

Posted by Adam Lundrigan (adamlundrigan) on 2012-06-01T18:30:47.000+0000

[Edit: Code removed]

Complete patch is attached.

Posted by Adam Lundrigan (adamlundrigan) on 2012-06-01T18:50:41.000+0000

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

Posted by Adam Lundrigan (adamlundrigan) on 2012-06-02T02:16:54.000+0000

Fixed in trunk (1.12.0): r24870

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts