Jelajahi Sumber

ZF-6444
- Fixed the emission of keywords static and final in both methods and properties of classes

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

ralph 16 tahun lalu
induk
melakukan
6abe351447

+ 11 - 0
library/Zend/CodeGenerator/Php/Class.php

@@ -327,6 +327,17 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     }
     
     /**
+     * hasProperty()
+     *
+     * @param string $propertyName
+     * @return bool
+     */
+    public function hasProperty($propertyName)
+    {
+        return isset($this->_properties[$propertyName]);
+    }
+    
+    /**
      * setMethods()
      *
      * @param array $methods

+ 28 - 1
library/Zend/CodeGenerator/Php/Member/Abstract.php

@@ -49,6 +49,11 @@ abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator
     /**
      * @var bool
      */
+    protected $_isFinal    = false;
+    
+    /**
+     * @var bool
+     */
     protected $_isStatic   = false;
     
     /**
@@ -117,6 +122,28 @@ abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator
     }
     
     /**
+     * setFinal()
+     *
+     * @param bool $isFinal
+     * @return Zend_CodeGenerator_Php_Member_Abstract
+     */
+    public function setFinal($isFinal)
+    {
+        $this->_isFinal = ($isFinal) ? true : false;
+        return $this;
+    }
+    
+    /**
+     * isFinal()
+     *
+     * @return bool
+     */
+    public function isFinal()
+    {
+        return $this->_isFinal;
+    }
+    
+    /**
      * setStatic()
      *
      * @param bool $isStatic
@@ -136,7 +163,7 @@ abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator
     public function isStatic()
     {
         return $this->_isStatic;
-    }    
+    }
     
     /**
      * setVisitibility()

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

@@ -198,9 +198,13 @@ class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstra
         
         if ($this->isAbstract()) {
             $output .= 'abstract ';
+        } else {
+            $output .= (($this->isFinal()) ? 'final ' : '');
         }
                 
-        $output .= $this->getVisibility() . ' function ' . $this->getName() . '(';
+        $output .= $this->getVisibility()
+            . (($this->isStatic()) ? ' static' : '')
+            . ' function ' . $this->getName() . '(';
 
         $parameters = $this->getParameters();
         if (!empty($parameters)) {

+ 4 - 2
library/Zend/CodeGenerator/Php/Property.php

@@ -159,8 +159,10 @@ class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abst
             $string = $this->_indentation . 'const ' . $name . ' = ' 
                 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
         } else {
-            $string = $this->_indentation . $this->getVisibility() 
-                . (($this->isStatic()) ? ' static' : '') . ' $' . $name . ' = '
+            $string = $this->_indentation
+                . $this->getVisibility() 
+                . (($this->isStatic()) ? ' static' : '') 
+                . ' $' . $name . ' = '
                 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
         }
         return $string; 

+ 68 - 0
tests/Zend/CodeGenerator/Php/MethodTest.php

@@ -72,6 +72,9 @@ class Zend_CodeGenerator_Php_MethodTest extends PHPUnit_Framework_TestCase
         $codeGen->setParameters(array(
             array('name' => 'one')
             ));
+        $params = $codeGen->getParameters();
+        $param = array_shift($params);
+        $this->assertTrue($param instanceof Zend_CodeGenerator_Php_Parameter, 'Failed because $param was not instance of Zend_CodeGenerator_Php_Property');
     }
     
     public function testBodyGetterAndSetter()
@@ -110,4 +113,69 @@ EOS;
         $this->assertEquals($target, (string) $codeGenMethod);
     }
     
+    /**
+     * @group ZF-6444
+     */
+    public function testStaticModifierIsEmitted()
+    {
+        $codeGen = new Zend_CodeGenerator_Php_Method();
+        $codeGen->setName('foo');
+        $codeGen->setParameters(array(
+            array('name' => 'one')
+            ));
+        $codeGen->setStatic(true);
+        
+        $expected = <<<EOS
+    public static function foo(\$one)
+    {
+    }
+
+EOS;
+            
+        $this->assertEquals($expected, $codeGen->generate());
+    }
+    
+    /**
+     * @group ZF-6444
+     */
+    public function testFinalModifierIsEmitted()
+    {
+        $codeGen = new Zend_CodeGenerator_Php_Method();
+        $codeGen->setName('foo');
+        $codeGen->setParameters(array(
+            array('name' => 'one')
+            ));
+        $codeGen->setFinal(true);
+        
+        $expected = <<<EOS
+    final public function foo(\$one)
+    {
+    }
+
+EOS;
+        $this->assertEquals($expected, $codeGen->generate());
+    }
+    
+    /**
+     * @group ZF-6444
+     */
+    public function testFinalModifierIsNotEmittedWhenMethodIsAbstract()
+    {
+        $codeGen = new Zend_CodeGenerator_Php_Method();
+        $codeGen->setName('foo');
+        $codeGen->setParameters(array(
+            array('name' => 'one')
+            ));
+        $codeGen->setFinal(true);
+        $codeGen->setAbstract(true);
+        
+        $expected = <<<EOS
+    abstract public function foo(\$one)
+    {
+    }
+
+EOS;
+        $this->assertEquals($expected, $codeGen->generate());
+    }
+    
 }

+ 13 - 1
tests/Zend/CodeGenerator/Php/PropertyTest.php

@@ -97,7 +97,6 @@ EOS;
     
     /**
      * @group ZF-6444
-     *
      */
     public function testPropertyWillLoadFromReflection()
     {
@@ -124,5 +123,18 @@ EOS;
         $this->assertEquals('private', $cgProp->getVisibility());
     }
 
+    /**
+     * @group ZF-6444
+     */
+    public function testPropertyWillEmitStaticModifier()
+    {
+        $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
+            'name' => 'someVal',
+            'static' => true,
+            'visibility' => 'protected',
+            'defaultValue' => 'some string value'
+            ));
+        $this->assertEquals('    protected static $someVal = \'some string value\';', $codeGenProperty->generate());
+    }
     
 }