Pārlūkot izejas kodu

ZF-6982 and ZF-7369 fix indenting with Zend_CodeGenerator (also affects Zend_Tool)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23561 44c647ce-9c0f-0410-b52a-842ac1e357ba
mjh_ca 15 gadi atpakaļ
vecāks
revīzija
a630b3dab3

+ 1 - 1
library/Zend/CodeGenerator/Php/File.php

@@ -97,7 +97,7 @@ class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
-     * fromReflectedFilePath() - use this if you intend on generating code generation objects based on the same file.
+     * fromReflectedFileName() - use this if you intend on generating code generation objects based on the same file.
      * This will keep previous changes to the file in tact during the same PHP process
      *
      * @param string $filePath

+ 3 - 1
library/Zend/CodeGenerator/Php/Method.php

@@ -220,10 +220,12 @@ class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstra
 
         $output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
 
-        if ($this->_body) {
+        if ($this->_body && $this->isSourceDirty()) {
             $output .= '        '
                     .  str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body))
                     .  self::LINE_FEED;
+        } elseif ($this->_body) {
+            $output .= $this->_body . self::LINE_FEED;
         }
 
         $output .= $indent . '}' . self::LINE_FEED;

+ 108 - 2
tests/Zend/CodeGenerator/Php/FileTest.php

@@ -114,12 +114,10 @@ EOS;
 
     public function testFromReflectionFile()
     {
-        ///$this->markTestSkipped('skipme');
         $file = dirname(__FILE__) . '/_files/TestSampleSingleClass.php';
 
         require_once $file;
         $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
-
         $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
 
         $expectedOutput = <<<EOS
@@ -170,6 +168,114 @@ EOS;
 
     }
 
+    /**
+     * @group ZF-7369
+     * @group ZF-6982
+     */
+    public function testFromReflectionFileKeepsIndents()
+    {
+        $file = dirname(__FILE__) . '/_files/TestClassWithCodeInMethod.php';
+
+        require_once $file;
+        $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
+
+        $expectedOutput = <<<EOS
+<?php
+/**
+ * File header here
+ *
+ * @author Ralph Schindler <ralph.schindler@zend.com>
+ */
+
+
+
+/**
+ * class docblock
+ *
+ * @package Zend_Reflection_TestClassWithCodeInMethod
+ */
+class Zend_Reflection_TestClassWithCodeInMethod
+{
+
+    /**
+     * Enter description here...
+     *
+     * @return bool
+     */
+    public function someMethod()
+    {
+        /* test test */
+        \$foo = 'bar';
+    }
+
+}
+
+
+EOS;
+
+        $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
+    }
+
+    /**
+     * @group ZF-7369
+     * @group ZF-6982
+     */
+    public function testFromReflectionFilePreservesIndentsWhenAdditionalMethodAdded()
+    {
+        $file = dirname(__FILE__) . '/_files/TestClassWithCodeInMethod.php';
+
+        require_once $file;
+        $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
+        $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
+        
+        $expectedOutput = <<<EOS
+<?php
+/**
+ * File header here
+ *
+ * @author Ralph Schindler <ralph.schindler@zend.com>
+ *
+ */
+
+
+
+
+/**
+ * class docblock
+ *
+ * @package Zend_Reflection_TestClassWithCodeInMethod
+ *
+ */
+class Zend_Reflection_TestClassWithCodeInMethod
+{
+
+    /**
+     * Enter description here...
+     *
+     * @return bool
+     *
+     */
+    public function someMethod()
+    {
+        /* test test */
+        \$foo = 'bar';
+    }
+
+    public function foobar()
+    {
+    }
+
+
+}
+
+
+
+
+EOS;
+
+        $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
+    }
+
     public function testFileLineEndingsAreAlwaysLineFeed()
     {
         $codeGenFile = new Zend_CodeGenerator_Php_File(array(

+ 30 - 0
tests/Zend/CodeGenerator/Php/_files/TestClassWithCodeInMethod.php

@@ -0,0 +1,30 @@
+<?php
+/**
+ * File header here
+ *
+ * @author Ralph Schindler <ralph.schindler@zend.com>
+ */
+
+
+
+/**
+ * class docblock
+ *
+ * @package Zend_Reflection_TestClassWithCodeInMethod
+ */
+class Zend_Reflection_TestClassWithCodeInMethod
+{
+
+    /**
+     * Enter description here...
+     *
+     * @return bool
+     */
+    public function someMethod()
+    {
+        /* test test */
+        $foo = 'bar';
+    }
+
+}
+