Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'feature/3378' into develop

Close #3378
  • Loading branch information...
commit ccb663851d121650ff2a41461b6773c6ba389097 2 parents 2a8ca79 + 319a681
Matthew Weier O'Phinney authored January 14, 2013
63  library/Zend/Code/Generator/AbstractMemberGenerator.php
@@ -92,36 +92,6 @@ public function removeFlag($flag)
92 92
     }
93 93
 
94 94
     /**
95  
-     * @param  DocBlockGenerator|string $docBlock
96  
-     * @throws Exception\InvalidArgumentException
97  
-     * @return AbstractMemberGenerator
98  
-     */
99  
-    public function setDocBlock($docBlock)
100  
-    {
101  
-        if (is_string($docBlock)) {
102  
-            $docBlock = new DocBlockGenerator($docBlock);
103  
-        } elseif (!$docBlock instanceof DocBlockGenerator) {
104  
-            throw new Exception\InvalidArgumentException(sprintf(
105  
-                '%s is expecting either a string, array or an instance of %s\DocBlockGenerator',
106  
-                __METHOD__,
107  
-                __NAMESPACE__
108  
-            ));
109  
-        }
110  
-
111  
-        $this->docBlock = $docBlock;
112  
-
113  
-        return $this;
114  
-    }
115  
-
116  
-    /**
117  
-     * @return DocBlockGenerator
118  
-     */
119  
-    public function getDocBlock()
120  
-    {
121  
-        return $this->docBlock;
122  
-    }
123  
-
124  
-    /**
125 95
      * @param  bool $isAbstract
126 96
      * @return AbstractMemberGenerator
127 97
      */
@@ -135,7 +105,7 @@ public function setAbstract($isAbstract)
135 105
      */
136 106
     public function isAbstract()
137 107
     {
138  
-        return ($this->flags & self::FLAG_ABSTRACT);
  108
+        return (bool) ($this->flags & self::FLAG_ABSTRACT);
139 109
     }
140 110
 
141 111
     /**
@@ -152,7 +122,7 @@ public function setFinal($isFinal)
152 122
      */
153 123
     public function isFinal()
154 124
     {
155  
-        return ($this->flags & self::FLAG_FINAL);
  125
+        return (bool) ($this->flags & self::FLAG_FINAL);
156 126
     }
157 127
 
158 128
     /**
@@ -229,4 +199,33 @@ public function getName()
229 199
         return $this->name;
230 200
     }
231 201
 
  202
+    /**
  203
+     * @param  DocBlockGenerator|string $docBlock
  204
+     * @throws Exception\InvalidArgumentException
  205
+     * @return AbstractMemberGenerator
  206
+     */
  207
+    public function setDocBlock($docBlock)
  208
+    {
  209
+        if (is_string($docBlock)) {
  210
+            $docBlock = new DocBlockGenerator($docBlock);
  211
+        } elseif (!$docBlock instanceof DocBlockGenerator) {
  212
+            throw new Exception\InvalidArgumentException(sprintf(
  213
+                '%s is expecting either a string, array or an instance of %s\DocBlockGenerator',
  214
+                __METHOD__,
  215
+                __NAMESPACE__
  216
+            ));
  217
+        }
  218
+
  219
+        $this->docBlock = $docBlock;
  220
+
  221
+        return $this;
  222
+    }
  223
+
  224
+    /**
  225
+     * @return DocBlockGenerator
  226
+     */
  227
+    public function getDocBlock()
  228
+    {
  229
+        return $this->docBlock;
  230
+    }
232 231
 }
7  library/Zend/Code/Generator/FileGenerator.php
@@ -185,15 +185,14 @@ public static function fromArray(array $values)
185 185
     {
186 186
         $fileGenerator = new static;
187 187
         foreach ($values as $name => $value) {
188  
-            switch ($name) {
  188
+            switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
189 189
                 case 'filename':
190  
-                    $fileGenerator->filename = $value;
  190
+                    $fileGenerator->setFilename($value);
191 191
                     continue;
192 192
                 case 'class':
193 193
                     $fileGenerator->setClass(($value instanceof ClassGenerator) ? : ClassGenerator::fromArray($value));
194 194
                     continue;
195  
-                case 'requiredFiles':
196  
-                case 'required_files':
  195
+                case 'requiredfiles':
197 196
                     $fileGenerator->setRequiredFiles($value);
198 197
                     continue;
199 198
                 default:
65  library/Zend/Code/Generator/MethodGenerator.php
@@ -24,11 +24,6 @@ class MethodGenerator extends AbstractMemberGenerator
24 24
     protected $docBlock = null;
25 25
 
26 26
     /**
27  
-     * @var bool
28  
-     */
29  
-    protected $isFinal = false;
30  
-
31  
-    /**
32 27
      * @var ParameterGenerator[]
33 28
      */
34 29
     protected $parameters = array();
@@ -77,6 +72,66 @@ public static function fromReflection(MethodReflection $reflectionMethod)
77 72
     }
78 73
 
79 74
     /**
  75
+     * Generate from array
  76
+     *
  77
+     * @configkey name           string        [required] Class Name
  78
+     * @configkey docblock       string        The docblock information
  79
+     * @configkey flags          int           Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
  80
+     * @configkey parameters     string        Class which this class is extending
  81
+     * @configkey body           string
  82
+     * @configkey abstract       bool
  83
+     * @configkey final          bool
  84
+     * @configkey static         bool
  85
+     * @configkey visibility     string
  86
+     *
  87
+     * @throws Exception\InvalidArgumentException
  88
+     * @param  array $array
  89
+     * @return MethodGenerator
  90
+     */
  91
+    public static function fromArray(array $array)
  92
+    {
  93
+        if (!isset($array['name'])) {
  94
+            throw new Exception\InvalidArgumentException(
  95
+                'Method generator requires that a name is provided for this object'
  96
+            );
  97
+        }
  98
+
  99
+        $method = new static($array['name']);
  100
+        foreach ($array as $name => $value) {
  101
+            // normalize key
  102
+            switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
  103
+                case 'docblock':
  104
+                    $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
  105
+                    $method->setDocBlock($docBlock);
  106
+                    break;
  107
+                case 'flags':
  108
+                    $method->setFlags($value);
  109
+                    break;
  110
+                case 'parameters':
  111
+                    $method->setParameters($value);
  112
+                    break;
  113
+                case 'body':
  114
+                    $method->setBody($value);
  115
+                    break;
  116
+                case 'abstract':
  117
+                    $method->setAbstract($value);
  118
+                    break;
  119
+                case 'final':
  120
+                    $method->setFinal($value);
  121
+                    break;
  122
+                case 'static':
  123
+                    $method->setStatic($value);
  124
+                    break;
  125
+                case 'visibility':
  126
+                    $method->setVisibility($value);
  127
+                    break;
  128
+            }
  129
+        }
  130
+
  131
+        return $method;
  132
+    }
  133
+
  134
+    /**
80 135
      * @param  string $name
81 136
      * @param  array $parameters
82 137
      * @param  int|array $flags
55  library/Zend/Code/Generator/ParameterGenerator.php
@@ -78,6 +78,61 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
78 78
     }
79 79
 
80 80
     /**
  81
+     * Generate from array
  82
+     *
  83
+     * @configkey name              string                                          [required] Class Name
  84
+     * @configkey type              string
  85
+     * @configkey defaultvalue      null|bool|string|int|float|array|ValueGenerator
  86
+     * @configkey passedbyreference bool
  87
+     * @configkey position          int
  88
+     * @configkey sourcedirty       bool
  89
+     * @configkey indentation       string
  90
+     * @configkey sourcecontent     string
  91
+     *
  92
+     * @throws Exception\InvalidArgumentException
  93
+     * @param  array $array
  94
+     * @return ParameterGenerator
  95
+     */
  96
+    public static function fromArray(array $array)
  97
+    {
  98
+        if (!isset($array['name'])) {
  99
+            throw new Exception\InvalidArgumentException(
  100
+                'Paramerer generator requires that a name is provided for this object'
  101
+            );
  102
+        }
  103
+
  104
+        $param = new static($array['name']);
  105
+        foreach ($array as $name => $value) {
  106
+            // normalize key
  107
+            switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
  108
+                case 'type':
  109
+                    $param->setType($value);
  110
+                    break;
  111
+                case 'defaultvalue':
  112
+                    $param->setDefaultValue($value);
  113
+                    break;
  114
+                case 'passedbyreference':
  115
+                    $param->setPassedByReference($value);
  116
+                    break;
  117
+                case 'position':
  118
+                    $param->setPosition($value);
  119
+                    break;
  120
+                case 'sourcedirty':
  121
+                    $param->setSourceDirty($value);
  122
+                    break;
  123
+                case 'indentation':
  124
+                    $param->setIndentation($value);
  125
+                    break;
  126
+                case 'sourcecontent':
  127
+                    $param->setSourceContent($value);
  128
+                    break;
  129
+            }
  130
+        }
  131
+
  132
+        return $param;
  133
+    }
  134
+
  135
+    /**
81 136
      * @param  string $name
82 137
      * @param  string $type
83 138
      * @param  mixed $defaultValue
61  library/Zend/Code/Generator/PropertyGenerator.php
@@ -66,6 +66,65 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
66 66
     }
67 67
 
68 68
     /**
  69
+     * Generate from array
  70
+     *
  71
+     * @configkey name         string                                          [required] Class Name
  72
+     * @configkey const        bool
  73
+     * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
  74
+     * @configkey flags        int
  75
+     * @configkey abstract     bool
  76
+     * @configkey final        bool
  77
+     * @configkey static       bool
  78
+     * @configkey visibility   string
  79
+     *
  80
+     * @throws Exception\InvalidArgumentException
  81
+     * @param  array $array
  82
+     * @return PropertyGenerator
  83
+     */
  84
+    public static function fromArray(array $array)
  85
+    {
  86
+        if (!isset($array['name'])) {
  87
+            throw new Exception\InvalidArgumentException(
  88
+                'Property generator requires that a name is provided for this object'
  89
+            );
  90
+        }
  91
+
  92
+        $property = new static($array['name']);
  93
+        foreach ($array as $name => $value) {
  94
+            // normalize key
  95
+            switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
  96
+                case 'const':
  97
+                    $property->setConst($value);
  98
+                    break;
  99
+                case 'defaultvalue':
  100
+                    $property->setDefaultValue($value);
  101
+                    break;
  102
+                case 'docblock':
  103
+                    $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
  104
+                    $property->setDocBlock($docBlock);
  105
+                    break;
  106
+                case 'flags':
  107
+                    $property->setFlags($value);
  108
+                    break;
  109
+                case 'abstract':
  110
+                    $property->setAbstract($value);
  111
+                    break;
  112
+                case 'final':
  113
+                    $property->setFinal($value);
  114
+                    break;
  115
+                case 'static':
  116
+                    $property->setStatic($value);
  117
+                    break;
  118
+                case 'visibility':
  119
+                    $property->setVisibility($value);
  120
+                    break;
  121
+            }
  122
+        }
  123
+
  124
+        return $property;
  125
+    }
  126
+
  127
+    /**
69 128
      * @param  string $name
70 129
      * @param PropertyValueGenerator|string|array $defaultValue
71 130
      * @param  int|array $flags
@@ -104,7 +163,7 @@ public function setConst($const)
104 163
      */
105 164
     public function isConst()
106 165
     {
107  
-        return ($this->flags & self::FLAG_CONSTANT);
  166
+        return (bool) ($this->flags & self::FLAG_CONSTANT);
108 167
     }
109 168
 
110 169
     /**
9  tests/ZendTest/Code/Generator/ClassGeneratorTest.php
@@ -186,8 +186,7 @@ public function testHasProperty()
186 186
 
187 187
     public function testToString()
188 188
     {
189  
-        $classGenerator = ClassGenerator::fromArray(
190  
-            array(
  189
+        $classGenerator = ClassGenerator::fromArray(array(
191 190
             'name' => 'SampleClass',
192 191
             'flags' => ClassGenerator::FLAG_ABSTRACT,
193 192
             'extendedClass' => 'ExtendedClassName',
@@ -371,8 +370,7 @@ public function testAddUses()
371 370
 
372 371
     public function testCreateFromArrayWithDocBlockFromArray()
373 372
     {
374  
-        $classGenerator = ClassGenerator::fromArray(
375  
-            array(
  373
+        $classGenerator = ClassGenerator::fromArray(array(
376 374
             'name' => 'SampleClass',
377 375
             'docblock' => array(
378 376
                 'shortdescription' => 'foo',
@@ -385,8 +383,7 @@ public function testCreateFromArrayWithDocBlockFromArray()
385 383
 
386 384
     public function testCreateFromArrayWithDocBlockInstance()
387 385
     {
388  
-        $classGenerator = ClassGenerator::fromArray(
389  
-            array(
  386
+        $classGenerator = ClassGenerator::fromArray(array(
390 387
             'name' => 'SampleClass',
391 388
             'docblock' => new DocBlockGenerator('foo'),
392 389
         ));
25  tests/ZendTest/Code/Generator/MethodGeneratorTest.php
@@ -23,7 +23,7 @@
23 23
  * @group Zend_Code_Generator
24 24
  * @group Zend_Code_Generator_Php
25 25
  */
26  
-class PhpMethodTest extends \PHPUnit_Framework_TestCase
  26
+class MethodGeneratorTest extends \PHPUnit_Framework_TestCase
27 27
 {
28 28
     public function testMethodConstructor()
29 29
     {
@@ -179,4 +179,27 @@ public function testDefaultValueGenerationDoesNotIncludeTrailingSemicolon()
179 179
         $generated = $method->generate();
180 180
         $this->assertRegexp('/array \$options = array\(\)\)/', $generated, $generated);
181 181
     }
  182
+
  183
+    public function testCreateFromArray()
  184
+    {
  185
+        $methodGenerator = MethodGenerator::fromArray(array(
  186
+            'name'       => 'SampleMethod',
  187
+            'body'       => 'foo',
  188
+            'docblock'   => array(
  189
+                'shortdescription' => 'foo',
  190
+            ),
  191
+            'abstract'   => true,
  192
+            'final'      => true,
  193
+            'static'     => true,
  194
+            'visibility' => MethodGenerator::VISIBILITY_PROTECTED,
  195
+        ));
  196
+
  197
+        $this->assertEquals('SampleMethod', $methodGenerator->getName());
  198
+        $this->assertEquals('foo', $methodGenerator->getBody());
  199
+        $this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $methodGenerator->getDocBlock());
  200
+        $this->assertTrue($methodGenerator->isAbstract());
  201
+        $this->assertTrue($methodGenerator->isFinal());
  202
+        $this->assertTrue($methodGenerator->isStatic());
  203
+        $this->assertEquals(MethodGenerator::VISIBILITY_PROTECTED, $methodGenerator->getVisibility());
  204
+    }
182 205
 }
23  tests/ZendTest/Code/Generator/ParameterGeneratorTest.php
@@ -165,4 +165,27 @@ protected function getFirstReflectionParameter($method)
165 165
 
166 166
         return array_shift($params);
167 167
     }
  168
+
  169
+    public function testCreateFromArray()
  170
+    {
  171
+        $parameterGenerator = ParameterGenerator::fromArray(array(
  172
+            'name'              => 'SampleParameter',
  173
+            'type'              => 'int',
  174
+            'defaultvalue'      => 'foo',
  175
+            'passedbyreference' => false,
  176
+            'position'          => 1,
  177
+            'sourcedirty'       => false,
  178
+            'sourcecontent'     => 'foo',
  179
+            'indentation'       => '-',
  180
+        ));
  181
+
  182
+        $this->assertEquals('SampleParameter', $parameterGenerator->getName());
  183
+        $this->assertEquals('int', $parameterGenerator->getType());
  184
+        $this->assertInstanceOf('Zend\Code\Generator\ValueGenerator', $parameterGenerator->getDefaultValue());
  185
+        $this->assertFalse($parameterGenerator->getPassedByReference());
  186
+        $this->assertEquals(1, $parameterGenerator->getPosition());
  187
+        $this->assertFalse($parameterGenerator->isSourceDirty());
  188
+        $this->assertEquals('foo', $parameterGenerator->getSourceContent());
  189
+        $this->assertEquals('-', $parameterGenerator->getIndentation());
  190
+    }
168 191
 }
25  tests/ZendTest/Code/Generator/PropertyGeneratorTest.php
@@ -219,4 +219,29 @@ public function testOtherTypesThrowExceptionOnGenerate()
219 219
         $codeGenProperty->generate();
220 220
     }
221 221
 
  222
+    public function testCreateFromArray()
  223
+    {
  224
+        $propertyGenerator = PropertyGenerator::fromArray(array(
  225
+            'name'         => 'SampleProperty',
  226
+            'const'        => true,
  227
+            'defaultvalue' => 'foo',
  228
+            'docblock'     => array(
  229
+                'shortdescription' => 'foo',
  230
+            ),
  231
+            'abstract'     => true,
  232
+            'final'        => true,
  233
+            'static'       => true,
  234
+            'visibility'   => PropertyGenerator::VISIBILITY_PROTECTED,
  235
+        ));
  236
+
  237
+        $this->assertEquals('SampleProperty', $propertyGenerator->getName());
  238
+        $this->assertTrue($propertyGenerator->isConst());
  239
+        $this->assertInstanceOf('Zend\Code\Generator\ValueGenerator', $propertyGenerator->getDefaultValue());
  240
+        $this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $propertyGenerator->getDocBlock());
  241
+        $this->assertTrue($propertyGenerator->isAbstract());
  242
+        $this->assertTrue($propertyGenerator->isFinal());
  243
+        $this->assertTrue($propertyGenerator->isStatic());
  244
+        $this->assertEquals(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility());
  245
+    }
  246
+
222 247
 }
4  tests/ZendTest/Code/Generator/TestAsset/TestClassWithManyProperties.php
@@ -27,7 +27,7 @@ class TestClassWithManyProperties
27 27
             'bar',
28 28
             'baz',
29 29
             //PHP_EOL
30  
-            )
31  
-        );
  30
+        )
  31
+    );
32 32
 
33 33
 }

0 notes on commit ccb6638

Please sign in to comment.
Something went wrong with that request. Please try again.