Browse Source

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

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24870 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 years ago
parent
commit
d7ae7f65db

+ 26 - 7
library/Zend/Reflection/Method.php

@@ -145,21 +145,40 @@ class Zend_Reflection_Method extends ReflectionMethod
     {
         $lines = array_slice(
             file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES),
-            $this->getStartLine(),
-            ($this->getEndLine() - $this->getStartLine()),
+            $this->getStartLine()-1,
+            ($this->getEndLine() - $this->getStartLine()) + 1,
             true
         );
 
-        $firstLine = array_shift($lines);
+        // Strip off lines until we come to a closing bracket
+        do {
+            if (count($lines) == 0) break;
+            $firstLine = array_shift($lines);
+        } while (strpos($firstLine, ')') === false);
+
+        // If the opening brace isn't on the same line as method 
+        // signature, then we should pop off more lines until we find it
+        if (strpos($firstLine,'{') === false) {
+            do {
+                if (count($lines) == 0) break;
+                $firstLine = array_shift($lines);
+            } while (strpos($firstLine, '{') === false);
+        }
 
-        if (trim($firstLine) !== '{') {
-            array_unshift($lines, $firstLine);
+        // If there are more characters on the line after the opening brace,
+        // push them back onto the lines stack as they are part of the body
+        $restOfFirstLine = trim(substr($firstLine, strpos($firstLine, '{')+1));
+        if (!empty($restOfFirstLine)) {
+            array_unshift($lines, $restOfFirstLine);
         }
 
         $lastLine = array_pop($lines);
 
-        if (trim($lastLine) !== '}') {
-            array_push($lines, $lastLine);
+        // If there are more characters on the line before the closing brace,
+        // push them back onto the lines stack as they are part of the body
+        $restOfLastLine = trim(substr($lastLine, 0, strrpos($lastLine, '}')-1));
+        if (!empty($restOfLastLine)) {
+            array_push($lines, $restOfLastLine);
         }
 
         // just in case we had code on the bracket lines

+ 2 - 2
tests/Zend/Reflection/FileTest.php

@@ -60,7 +60,7 @@ class Zend_Reflection_FileTest extends PHPUnit_Framework_TestCase
         require_once $fileToRequire;
         $reflectionFile = new Zend_Reflection_File($fileToRequire);
         $this->assertEquals(get_class($reflectionFile), 'Zend_Reflection_File');
-        $this->assertEquals(count($reflectionFile->getClasses()), 8);
+        $this->assertEquals(count($reflectionFile->getClasses()), 9);
         $this->assertEquals(get_class($reflectionFile->getClass('Zend_Reflection_TestSampleClass2')), 'Zend_Reflection_Class');
     }
 
@@ -110,7 +110,7 @@ class Zend_Reflection_FileTest extends PHPUnit_Framework_TestCase
         require_once $fileToRequire;
         $reflectionFile = new Zend_Reflection_File($fileToRequire);
         $this->assertEquals(9, $reflectionFile->getStartLine());
-        $this->assertEquals(185, $reflectionFile->getEndLine());
+        $this->assertEquals(196, $reflectionFile->getEndLine());
     }
 
     public function testFileGetDocblockReturnsFileDocblock()

+ 20 - 0
tests/Zend/Reflection/MethodTest.php

@@ -121,5 +121,25 @@ class Zend_Reflection_MethodTest extends PHPUnit_Framework_TestCase
         $this->assertEquals("    {\n\n        return 'mixedValue';\n\n    }\n", $reflectionMethod->getContents(false));
     }
 
+    /**
+     * @group ZF-10870
+     */
+    public function testGetBodyReturnsCorrectBodyWhenMethodSignatureIsMultiline()
+    {
+        $body = '        // FUNKY SIGNATURE';
+        $reflectionMethod = new Zend_Reflection_Method('Zend_Reflection_TestSampleClass7', 'bigMethodSignature');
+        $this->assertEquals($body, $reflectionMethod->getBody());
+    }
+
+    /**
+     * @group ZF-10870
+     */
+    public function testGetBodyReturnsCorrectBodyWhenMethodSignatureAndBodyAreOnSameLine()
+    {
+        $body = 'return true;';
+        $reflectionMethod = new Zend_Reflection_Method('Zend_Reflection_TestSampleClass7', 'testInlineMethod');
+        $this->assertEquals($body, $reflectionMethod->getBody());
+    }
+
 }
 

+ 11 - 0
tests/Zend/Reflection/_files/TestSampleClass.php

@@ -182,3 +182,14 @@ class Zend_Reflection_Docblock_Param_WithNamespace
 
     }
 }
+
+class Zend_Reflection_TestSampleClass7
+{
+    public function bigMethodSignature($arg1, $arg2, $arg3, 
+        $arg4, $arg5, $arg6)
+    {
+        // FUNKY SIGNATURE
+    }
+
+    public function testInlineMethod() { return true; }
+}