Jelajahi Sumber

ZF-7205
- Property and Method handling of Docblocks refactored

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16630 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph 16 tahun lalu
induk
melakukan
b373944bee

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

@@ -207,7 +207,7 @@ class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
     protected function _docCommentize($content)
     {
         $indent = $this->getIndentation();
-        $output = '/**' . self::LINE_FEED;
+        $output = $indent . '/**' . self::LINE_FEED;
         $content = wordwrap($content, 80, "\n");
         $lines = explode("\n", $content);
         foreach ($lines as $line) {

+ 11 - 0
library/Zend/CodeGenerator/Php/Member/Abstract.php

@@ -26,6 +26,11 @@
 require_once 'Zend/CodeGenerator/Php/Abstract.php';
 
 /**
+ * @see Zend_CodeGenerator_Php_Abstract
+ */
+require_once 'Zend/CodeGenerator/Php/Docblock.php';
+
+/**
  * @category   Zend
  * @package    Zend_CodeGenerator
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
@@ -33,6 +38,7 @@ require_once 'Zend/CodeGenerator/Php/Abstract.php';
  */
 abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator_Php_Abstract
 {
+    
     /**#@+
      * @param const string
      */
@@ -42,6 +48,11 @@ abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator
     /**#@-*/
     
     /**
+     * @var Zend_CodeGenerator_Php_Docblock
+     */
+    protected $_docblock   = null;
+    
+    /**
      * @var bool
      */
     protected $_isAbstract = false;

+ 10 - 7
library/Zend/CodeGenerator/Php/Method.php

@@ -188,14 +188,17 @@ class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstra
      */
     public function generate()
     {
-        $output = '    ';
+        $output = '';
         
-        if (null !== ($docblock = $this->getDocblock())) {
-            $docblock->setIndentation('    ');
+        $indent = $this->getIndentation();
+        
+        if (($docblock = $this->getDocblock()) !== null) {
+            $docblock->setIndentation($indent);
             $output .= $docblock->generate();
-            $output .= '    ';
         }
         
+        $output .= $indent;
+        
         if ($this->isAbstract()) {
             $output .= 'abstract ';
         } else {
@@ -215,15 +218,15 @@ class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstra
             $output .= implode(', ', $parameterOuput);
         }
         
-        $output .= ')' . self::LINE_FEED . '    {' . self::LINE_FEED;
+        $output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
 
         if ($this->_body) {
             $output .= '        ' 
-                    .  str_replace(self::LINE_FEED, self::LINE_FEED . '        ', trim($this->_body)) 
+                    .  str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body)) 
                     .  self::LINE_FEED;
         }
         
-        $output .= '    }' . self::LINE_FEED;
+        $output .= $indent . '}' . self::LINE_FEED;
         
         return $output;
     }

+ 11 - 3
library/Zend/CodeGenerator/Php/Property.php

@@ -150,22 +150,30 @@ class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abst
     {
         $name         = $this->getName();
         $defaultValue = $this->getDefaultValue();
+        
+        $output = '';
+        
+        if (($docblock = $this->getDocblock()) !== null) {
+            $docblock->setIndentation('    ');
+            $output .= $docblock->generate();
+        }
+        
         if ($this->isConst()) {
             if ($defaultValue != null && !$defaultValue->isValidConstantType()) {
                 require_once 'Zend/CodeGenerator/Php/Exception.php';
                 throw new Zend_CodeGenerator_Php_Exception('The property ' . $this->_name . ' is said to be '
                     . 'constant but does not have a valid constant value.');
             }
-            $string = $this->_indentation . 'const ' . $name . ' = ' 
+            $output .= $this->_indentation . 'const ' . $name . ' = ' 
                 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
         } else {
-            $string = $this->_indentation
+            $output .= $this->_indentation
                 . $this->getVisibility() 
                 . (($this->isStatic()) ? ' static' : '') 
                 . ' $' . $name . ' = '
                 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
         }
-        return $string; 
+        return $output; 
     }
     
 }

+ 31 - 7
tests/Zend/CodeGenerator/Php/MethodTest.php

@@ -60,13 +60,13 @@ class Zend_CodeGenerator_Php_MethodTest extends PHPUnit_Framework_TestCase
         $this->_method = null;
     }
     
-    public function testConstructor()
+    public function testMethodConstructor()
     {
         $codeGenMethod = new Zend_CodeGenerator_Php_Method();
         $this->isInstanceOf($codeGenMethod, 'Zend_CodeGenerator_Php_Method');
     }
     
-    public function testParameterAccessors()
+    public function testMethodParameterAccessors()
     {
         $codeGen = new Zend_CodeGenerator_Php_Method();
         $codeGen->setParameters(array(
@@ -77,7 +77,7 @@ class Zend_CodeGenerator_Php_MethodTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($param instanceof Zend_CodeGenerator_Php_Parameter, 'Failed because $param was not instance of Zend_CodeGenerator_Php_Property');
     }
     
-    public function testBodyGetterAndSetter()
+    public function testMethodBodyGetterAndSetter()
     {
         $this->_method->setBody('Foo');
         $this->assertEquals('Foo', $this->_method->getBody());
@@ -92,7 +92,7 @@ class Zend_CodeGenerator_Php_MethodTest extends PHPUnit_Framework_TestCase
     }
     
     
-    public function testFromReflection()
+    public function testMethodFromReflection()
     {
         $ref = new Zend_Reflection_Method('Zend_Reflection_TestSampleSingleClass', 'someMethod');
         
@@ -116,7 +116,7 @@ EOS;
     /**
      * @group ZF-6444
      */
-    public function testStaticModifierIsEmitted()
+    public function testMethodWithStaticModifierIsEmitted()
     {
         $codeGen = new Zend_CodeGenerator_Php_Method();
         $codeGen->setName('foo');
@@ -138,7 +138,7 @@ EOS;
     /**
      * @group ZF-6444
      */
-    public function testFinalModifierIsEmitted()
+    public function testMethodWithFinalModifierIsEmitted()
     {
         $codeGen = new Zend_CodeGenerator_Php_Method();
         $codeGen->setName('foo');
@@ -159,7 +159,7 @@ EOS;
     /**
      * @group ZF-6444
      */
-    public function testFinalModifierIsNotEmittedWhenMethodIsAbstract()
+    public function testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract()
     {
         $codeGen = new Zend_CodeGenerator_Php_Method();
         $codeGen->setName('foo');
@@ -178,4 +178,28 @@ EOS;
         $this->assertEquals($expected, $codeGen->generate());
     }
     
+    /**
+     * @group ZF-7205
+     */
+    public function testMethodCanHaveDocblock()
+    {
+        $codeGenProperty = new Zend_CodeGenerator_Php_Method(array(
+            'name' => 'someFoo',
+            'static' => true,
+            'visibility' => 'protected',
+            'docblock' => '@var string $someVal This is some val'
+            ));
+            
+        $expected = <<<EOS
+    /**
+     * @var string \$someVal This is some val
+     */
+    protected static function someFoo()
+    {
+    }
+
+EOS;
+        $this->assertEquals($expected, $codeGenProperty->generate());
+    }
+    
 }

+ 22 - 0
tests/Zend/CodeGenerator/Php/PropertyTest.php

@@ -137,4 +137,26 @@ EOS;
         $this->assertEquals('    protected static $someVal = \'some string value\';', $codeGenProperty->generate());
     }
     
+    /**
+     * @group ZF-7205
+     */
+    public function testPropertyCanHaveDocblock()
+    {
+        $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
+            'name' => 'someVal',
+            'static' => true,
+            'visibility' => 'protected',
+            'defaultValue' => 'some string value',
+            'docblock' => '@var string $someVal This is some val'
+            ));
+            
+        $expected = <<<EOS
+    /**
+     * @var string \$someVal This is some val
+     */
+    protected static \$someVal = 'some string value';
+EOS;
+        $this->assertEquals($expected, $codeGenProperty->generate());
+    }
+    
 }