Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improve code generators consistency #3378

Merged
merged 8 commits into from

2 participants

@neeckeloo

I added formArray() method in some generators for consistency.

@neeckeloo

I think it would be judicious to create a static method \Zend\Code\Generator\AbstractGenerator::fromArray() a bit like \Zend\Stdlib\AbstractOptions::setFromArray() to reduce code and for consistency of all generators. There still remains the possibility of extending this method if necessary.

@weierophinney Do you think I can refactor the code from this idea ?

@weierophinney

@neeckeloo The only issue with abstracting it is that not all of them have the same properties/methods that they operate on -- it looked like each one had a slight variation of one sort or another. As such, I think having each define their specific functionality is good here.

@weierophinney weierophinney merged commit a4c5e41 into zendframework:develop
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/3378' into develop
Close #3378
a16fa11
@weierophinney weierophinney referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2013
  1. @neeckeloo

    Remove unused property

    neeckeloo authored
  2. @neeckeloo

    Fix indentation

    neeckeloo authored
  3. @neeckeloo
  4. @neeckeloo

    Add fromArray methods

    neeckeloo authored
Commits on Jan 8, 2013
  1. @neeckeloo

    Fix unit tests

    neeckeloo authored
  2. @neeckeloo

    Fix doc block

    neeckeloo authored
  3. @neeckeloo

    Add other fromArray method

    neeckeloo authored
  4. @neeckeloo

    Minor changes

    neeckeloo authored
This page is out of date. Refresh to see the latest.
View
63 library/Zend/Code/Generator/AbstractMemberGenerator.php
@@ -92,36 +92,6 @@ public function removeFlag($flag)
}
/**
- * @param DocBlockGenerator|string $docBlock
- * @throws Exception\InvalidArgumentException
- * @return AbstractMemberGenerator
- */
- public function setDocBlock($docBlock)
- {
- if (is_string($docBlock)) {
- $docBlock = new DocBlockGenerator($docBlock);
- } elseif (!$docBlock instanceof DocBlockGenerator) {
- throw new Exception\InvalidArgumentException(sprintf(
- '%s is expecting either a string, array or an instance of %s\DocBlockGenerator',
- __METHOD__,
- __NAMESPACE__
- ));
- }
-
- $this->docBlock = $docBlock;
-
- return $this;
- }
-
- /**
- * @return DocBlockGenerator
- */
- public function getDocBlock()
- {
- return $this->docBlock;
- }
-
- /**
* @param bool $isAbstract
* @return AbstractMemberGenerator
*/
@@ -135,7 +105,7 @@ public function setAbstract($isAbstract)
*/
public function isAbstract()
{
- return ($this->flags & self::FLAG_ABSTRACT);
+ return (bool) ($this->flags & self::FLAG_ABSTRACT);
}
/**
@@ -152,7 +122,7 @@ public function setFinal($isFinal)
*/
public function isFinal()
{
- return ($this->flags & self::FLAG_FINAL);
+ return (bool) ($this->flags & self::FLAG_FINAL);
}
/**
@@ -229,4 +199,33 @@ public function getName()
return $this->name;
}
+ /**
+ * @param DocBlockGenerator|string $docBlock
+ * @throws Exception\InvalidArgumentException
+ * @return AbstractMemberGenerator
+ */
+ public function setDocBlock($docBlock)
+ {
+ if (is_string($docBlock)) {
+ $docBlock = new DocBlockGenerator($docBlock);
+ } elseif (!$docBlock instanceof DocBlockGenerator) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ '%s is expecting either a string, array or an instance of %s\DocBlockGenerator',
+ __METHOD__,
+ __NAMESPACE__
+ ));
+ }
+
+ $this->docBlock = $docBlock;
+
+ return $this;
+ }
+
+ /**
+ * @return DocBlockGenerator
+ */
+ public function getDocBlock()
+ {
+ return $this->docBlock;
+ }
}
View
7 library/Zend/Code/Generator/FileGenerator.php
@@ -185,15 +185,14 @@ public static function fromArray(array $values)
{
$fileGenerator = new static;
foreach ($values as $name => $value) {
- switch ($name) {
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
case 'filename':
- $fileGenerator->filename = $value;
+ $fileGenerator->setFilename($value);
continue;
case 'class':
$fileGenerator->setClass(($value instanceof ClassGenerator) ? : ClassGenerator::fromArray($value));
continue;
- case 'requiredFiles':
- case 'required_files':
+ case 'requiredfiles':
$fileGenerator->setRequiredFiles($value);
continue;
default:
View
65 library/Zend/Code/Generator/MethodGenerator.php
@@ -24,11 +24,6 @@ class MethodGenerator extends AbstractMemberGenerator
protected $docBlock = null;
/**
- * @var bool
- */
- protected $isFinal = false;
-
- /**
* @var ParameterGenerator[]
*/
protected $parameters = array();
@@ -77,6 +72,66 @@ public static function fromReflection(MethodReflection $reflectionMethod)
}
/**
+ * Generate from array
+ *
+ * @configkey name string [required] Class Name
+ * @configkey docblock string The docblock information
+ * @configkey flags int Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
+ * @configkey parameters string Class which this class is extending
+ * @configkey body string
+ * @configkey abstract bool
+ * @configkey final bool
+ * @configkey static bool
+ * @configkey visibility string
+ *
+ * @throws Exception\InvalidArgumentException
+ * @param array $array
+ * @return MethodGenerator
+ */
+ public static function fromArray(array $array)
+ {
+ if (!isset($array['name'])) {
+ throw new Exception\InvalidArgumentException(
+ 'Method generator requires that a name is provided for this object'
+ );
+ }
+
+ $method = new static($array['name']);
+ foreach ($array as $name => $value) {
+ // normalize key
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
+ case 'docblock':
+ $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
+ $method->setDocBlock($docBlock);
+ break;
+ case 'flags':
+ $method->setFlags($value);
+ break;
+ case 'parameters':
+ $method->setParameters($value);
+ break;
+ case 'body':
+ $method->setBody($value);
+ break;
+ case 'abstract':
+ $method->setAbstract($value);
+ break;
+ case 'final':
+ $method->setFinal($value);
+ break;
+ case 'static':
+ $method->setStatic($value);
+ break;
+ case 'visibility':
+ $method->setVisibility($value);
+ break;
+ }
+ }
+
+ return $method;
+ }
+
+ /**
* @param string $name
* @param array $parameters
* @param int|array $flags
View
55 library/Zend/Code/Generator/ParameterGenerator.php
@@ -78,6 +78,61 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
}
/**
+ * Generate from array
+ *
+ * @configkey name string [required] Class Name
+ * @configkey type string
+ * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
+ * @configkey passedbyreference bool
+ * @configkey position int
+ * @configkey sourcedirty bool
+ * @configkey indentation string
+ * @configkey sourcecontent string
+ *
+ * @throws Exception\InvalidArgumentException
+ * @param array $array
+ * @return ParameterGenerator
+ */
+ public static function fromArray(array $array)
+ {
+ if (!isset($array['name'])) {
+ throw new Exception\InvalidArgumentException(
+ 'Paramerer generator requires that a name is provided for this object'
+ );
+ }
+
+ $param = new static($array['name']);
+ foreach ($array as $name => $value) {
+ // normalize key
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
+ case 'type':
+ $param->setType($value);
+ break;
+ case 'defaultvalue':
+ $param->setDefaultValue($value);
+ break;
+ case 'passedbyreference':
+ $param->setPassedByReference($value);
+ break;
+ case 'position':
+ $param->setPosition($value);
+ break;
+ case 'sourcedirty':
+ $param->setSourceDirty($value);
+ break;
+ case 'indentation':
+ $param->setIndentation($value);
+ break;
+ case 'sourcecontent':
+ $param->setSourceContent($value);
+ break;
+ }
+ }
+
+ return $param;
+ }
+
+ /**
* @param string $name
* @param string $type
* @param mixed $defaultValue
View
61 library/Zend/Code/Generator/PropertyGenerator.php
@@ -66,6 +66,65 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
}
/**
+ * Generate from array
+ *
+ * @configkey name string [required] Class Name
+ * @configkey const bool
+ * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
+ * @configkey flags int
+ * @configkey abstract bool
+ * @configkey final bool
+ * @configkey static bool
+ * @configkey visibility string
+ *
+ * @throws Exception\InvalidArgumentException
+ * @param array $array
+ * @return PropertyGenerator
+ */
+ public static function fromArray(array $array)
+ {
+ if (!isset($array['name'])) {
+ throw new Exception\InvalidArgumentException(
+ 'Property generator requires that a name is provided for this object'
+ );
+ }
+
+ $property = new static($array['name']);
+ foreach ($array as $name => $value) {
+ // normalize key
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
+ case 'const':
+ $property->setConst($value);
+ break;
+ case 'defaultvalue':
+ $property->setDefaultValue($value);
+ break;
+ case 'docblock':
+ $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
+ $property->setDocBlock($docBlock);
+ break;
+ case 'flags':
+ $property->setFlags($value);
+ break;
+ case 'abstract':
+ $property->setAbstract($value);
+ break;
+ case 'final':
+ $property->setFinal($value);
+ break;
+ case 'static':
+ $property->setStatic($value);
+ break;
+ case 'visibility':
+ $property->setVisibility($value);
+ break;
+ }
+ }
+
+ return $property;
+ }
+
+ /**
* @param string $name
* @param PropertyValueGenerator|string|array $defaultValue
* @param int|array $flags
@@ -104,7 +163,7 @@ public function setConst($const)
*/
public function isConst()
{
- return ($this->flags & self::FLAG_CONSTANT);
+ return (bool) ($this->flags & self::FLAG_CONSTANT);
}
/**
View
9 tests/ZendTest/Code/Generator/ClassGeneratorTest.php
@@ -186,8 +186,7 @@ public function testHasProperty()
public function testToString()
{
- $classGenerator = ClassGenerator::fromArray(
- array(
+ $classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'flags' => ClassGenerator::FLAG_ABSTRACT,
'extendedClass' => 'ExtendedClassName',
@@ -371,8 +370,7 @@ public function testAddUses()
public function testCreateFromArrayWithDocBlockFromArray()
{
- $classGenerator = ClassGenerator::fromArray(
- array(
+ $classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'docblock' => array(
'shortdescription' => 'foo',
@@ -385,8 +383,7 @@ public function testCreateFromArrayWithDocBlockFromArray()
public function testCreateFromArrayWithDocBlockInstance()
{
- $classGenerator = ClassGenerator::fromArray(
- array(
+ $classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'docblock' => new DocBlockGenerator('foo'),
));
View
25 tests/ZendTest/Code/Generator/MethodGeneratorTest.php
@@ -23,7 +23,7 @@
* @group Zend_Code_Generator
* @group Zend_Code_Generator_Php
*/
-class PhpMethodTest extends \PHPUnit_Framework_TestCase
+class MethodGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testMethodConstructor()
{
@@ -179,4 +179,27 @@ public function testDefaultValueGenerationDoesNotIncludeTrailingSemicolon()
$generated = $method->generate();
$this->assertRegexp('/array \$options = array\(\)\)/', $generated, $generated);
}
+
+ public function testCreateFromArray()
+ {
+ $methodGenerator = MethodGenerator::fromArray(array(
+ 'name' => 'SampleMethod',
+ 'body' => 'foo',
+ 'docblock' => array(
+ 'shortdescription' => 'foo',
+ ),
+ 'abstract' => true,
+ 'final' => true,
+ 'static' => true,
+ 'visibility' => MethodGenerator::VISIBILITY_PROTECTED,
+ ));
+
+ $this->assertEquals('SampleMethod', $methodGenerator->getName());
+ $this->assertEquals('foo', $methodGenerator->getBody());
+ $this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $methodGenerator->getDocBlock());
+ $this->assertTrue($methodGenerator->isAbstract());
+ $this->assertTrue($methodGenerator->isFinal());
+ $this->assertTrue($methodGenerator->isStatic());
+ $this->assertEquals(MethodGenerator::VISIBILITY_PROTECTED, $methodGenerator->getVisibility());
+ }
}
View
23 tests/ZendTest/Code/Generator/ParameterGeneratorTest.php
@@ -165,4 +165,27 @@ protected function getFirstReflectionParameter($method)
return array_shift($params);
}
+
+ public function testCreateFromArray()
+ {
+ $parameterGenerator = ParameterGenerator::fromArray(array(
+ 'name' => 'SampleParameter',
+ 'type' => 'int',
+ 'defaultvalue' => 'foo',
+ 'passedbyreference' => false,
+ 'position' => 1,
+ 'sourcedirty' => false,
+ 'sourcecontent' => 'foo',
+ 'indentation' => '-',
+ ));
+
+ $this->assertEquals('SampleParameter', $parameterGenerator->getName());
+ $this->assertEquals('int', $parameterGenerator->getType());
+ $this->assertInstanceOf('Zend\Code\Generator\ValueGenerator', $parameterGenerator->getDefaultValue());
+ $this->assertFalse($parameterGenerator->getPassedByReference());
+ $this->assertEquals(1, $parameterGenerator->getPosition());
+ $this->assertFalse($parameterGenerator->isSourceDirty());
+ $this->assertEquals('foo', $parameterGenerator->getSourceContent());
+ $this->assertEquals('-', $parameterGenerator->getIndentation());
+ }
}
View
25 tests/ZendTest/Code/Generator/PropertyGeneratorTest.php
@@ -219,4 +219,29 @@ public function testOtherTypesThrowExceptionOnGenerate()
$codeGenProperty->generate();
}
+ public function testCreateFromArray()
+ {
+ $propertyGenerator = PropertyGenerator::fromArray(array(
+ 'name' => 'SampleProperty',
+ 'const' => true,
+ 'defaultvalue' => 'foo',
+ 'docblock' => array(
+ 'shortdescription' => 'foo',
+ ),
+ 'abstract' => true,
+ 'final' => true,
+ 'static' => true,
+ 'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
+ ));
+
+ $this->assertEquals('SampleProperty', $propertyGenerator->getName());
+ $this->assertTrue($propertyGenerator->isConst());
+ $this->assertInstanceOf('Zend\Code\Generator\ValueGenerator', $propertyGenerator->getDefaultValue());
+ $this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $propertyGenerator->getDocBlock());
+ $this->assertTrue($propertyGenerator->isAbstract());
+ $this->assertTrue($propertyGenerator->isFinal());
+ $this->assertTrue($propertyGenerator->isStatic());
+ $this->assertEquals(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility());
+ }
+
}
View
4 tests/ZendTest/Code/Generator/TestAsset/TestClassWithManyProperties.php
@@ -27,7 +27,7 @@ class TestClassWithManyProperties
'bar',
'baz',
//PHP_EOL
- )
- );
+ )
+ );
}
Something went wrong with that request. Please try again.