Parcourir la source

ZF-7923 - Single quotes in property default values not handled correctly.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18393 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei il y a 16 ans
Parent
commit
202fa2e8f4

+ 18 - 9
library/Zend/CodeGenerator/Php/Property/DefaultValue.php

@@ -78,9 +78,11 @@ class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Ph
      */
     protected function _init()
     {
-        $reflect = new ReflectionClass(get_class($this));
-        self::$_constants = $reflect->getConstants();
-        unset($reflect);
+        if(count(self::$_constants) == 0) {
+            $reflect = new ReflectionClass(get_class($this));
+            self::$_constants = $reflect->getConstants();
+            unset($reflect);
+        }
     }
 
     /**
@@ -221,8 +223,6 @@ class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Ph
             default:
                 return self::TYPE_OTHER;
         }
-
-        return self::TYPE_OTHER;
     }
 
     /**
@@ -263,15 +263,21 @@ class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Ph
         $output = '';
 
         switch ($type) {
+            case self::TYPE_BOOLEAN:
+            case self::TYPE_BOOL:
+                $output .= ( $value ? 'true' : 'false' );
+                break;
             case self::TYPE_STRING:
-                $output .= "'" . $value . "'";
+                $output .= "'" . addcslashes($value, "'") . "'";
+                break;
+            case self::TYPE_NULL:
+                $output .= 'null';
                 break;
             case self::TYPE_NUMBER:
             case self::TYPE_INTEGER:
             case self::TYPE_INT:
             case self::TYPE_FLOAT:
             case self::TYPE_DOUBLE:
-            case self::TYPE_NULL:
             case self::TYPE_CONSTANT:
                 $output .= $value;
                 break;
@@ -292,7 +298,7 @@ class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Ph
                         $outputParts[] = $partV;
                         $noKeyIndex++;
                     } else {
-                        $outputParts[] = (is_int($n) ? $n : "'" . $n . "'") . ' => ' . $partV;
+                        $outputParts[] = (is_int($n) ? $n : "'" . addcslashes($n, "'") . "'") . ' => ' . $partV;
                     }
 
                 }
@@ -304,7 +310,10 @@ class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Ph
                 break;
             case self::TYPE_OTHER:
             default:
-                throw new Exception('I dont know this type');
+                require_once "Zend/CodeGenerator/Php/Exception.php";
+                throw new Zend_CodeGenerator_Php_Exception(
+                    "Type '".get_class($value)."' is unknown or cannot be used as property default value."
+                );
         }
 
         $output .= ';';

+ 72 - 3
tests/Zend/CodeGenerator/Php/PropertyTest.php

@@ -60,20 +60,26 @@ class Zend_CodeGenerator_Php_PropertyTest extends PHPUnit_Framework_TestCase
         $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value'));
         $this->assertEquals('    public $someVal = \'some string value\';', $codeGenProperty->generate());
     }
-    
+
     public function testPropertyMultilineValue()
     {
         $targetValue = array(
             5, 
             'one' => 1,
             'two' => '2',
+            'null' => null,
+            'true' => true,
+            "bar's" => "bar's",
             );
         
         $expectedSource = <<<EOS
     public \$myFoo = array(
         5,
         'one' => 1,
-        'two' => '2'
+        'two' => '2',
+        'null' => null,
+        'true' => true,
+        'bar\'s' => 'bar\'s'
         );
 EOS;
 
@@ -160,5 +166,68 @@ EOS;
 EOS;
         $this->assertEquals($expected, $codeGenProperty->generate());
     }
-    
+
+    public function testOtherTypesThrowExceptionOnGenerate()
+    {
+        $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
+            'name' => 'someVal',
+            'defaultValue' => new stdClass(),
+        ));
+
+        $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
+
+        $codeGenProperty->generate();
+    }
+
+    static public function dataSetTypeSetValueGenerate()
+    {
+        return array(
+            array('string', 'foo', "'foo';"),
+            array('int', 1, "1;"),
+            array('integer', 1, "1;"),
+            array('bool', true, "true;"),
+            array('bool', false, "false;"),
+            array('boolean', true, "true;"),
+            array('number', 1, '1;'),
+            array('float', 1.23, '1.23;'),
+            array('double', 1.23, '1.23;'),
+            array('constant', 'FOO', 'FOO;'),
+            array('null', null, 'null;'),
+        );
+    }
+
+    /**
+     * @dataProvider dataSetTypeSetValueGenerate
+     * @param string $type
+     * @param mixed $value
+     * @param string $code
+     */
+    public function testSetTypeSetValueGenerate($type, $value, $code)
+    {
+        $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
+        $defaultValue->setType($type);
+        $defaultValue->setValue($value);
+
+        $this->assertEquals($type, $defaultValue->getType());
+        $this->assertEquals($code, $defaultValue->generate());
+    }
+
+    /**
+     * @dataProvider dataSetTypeSetValueGenerate
+     * @param string $type
+     * @param mixed $value
+     * @param string $code
+     */
+    public function testSetBogusTypeSetValueGenerateUseAutoDetection($type, $value, $code)
+    {
+        if($type == 'constant') {
+            return; // constant can only be detected explicitly
+        }
+
+        $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
+        $defaultValue->setType("bogus");
+        $defaultValue->setValue($value);
+
+        $this->assertEquals($code, $defaultValue->generate());
+    }
 }