diff --git a/doc/book/generator/reference.md b/doc/book/generator/reference.md index b96b089e..2fc15d57 100644 --- a/doc/book/generator/reference.md +++ b/doc/book/generator/reference.md @@ -102,6 +102,7 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen ) public function addConstants(Array $properties) public function addConstant($property) + public function removeConstant($constantName) public function getConstants() public function getConstant($propertyName) public function setDocblock(Zend\Code\Generator\DocBlockGenerator $docblock) @@ -116,6 +117,7 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen public function getImplementedInterfaces() public function addProperties(Array $properties) public function addProperty($property) + public function removeProperty($propertyName) public function getProperties() public function getProperty($propertyName) public function addMethods(Array $methods) @@ -126,6 +128,7 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen $body = null, $docBlock = null ) + public function removeMethod($methodName) public function getMethods() public function getMethod($methodName) public function hasMethod($methodName) diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index c84e1b4c..653bf21d 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -534,6 +534,20 @@ public function addConstant($name, $value) ); } + /** + * @param $constantName + * + * @return $this + */ + public function removeConstant($constantName) + { + if ($this->hasConstant($constantName)) { + unset($this->constants[$constantName]); + } + + return $this; + } + /** * @param PropertyGenerator[]|array[] $constants * @@ -603,6 +617,19 @@ public function addProperty($name, $defaultValue = null, $flags = PropertyGenera return $this->addPropertyFromGenerator(new PropertyGenerator($name, $defaultValue, $flags)); } + /** + * @param string $propertyName + * @return ClassGenerator + */ + public function removeProperty($propertyName) + { + if ($this->hasProperty($propertyName)) { + unset($this->properties[$propertyName]); + } + + return $this; + } + /** * Add property from PropertyGenerator * diff --git a/test/Generator/ClassGeneratorTest.php b/test/Generator/ClassGeneratorTest.php index c79f4dcb..7ae2168c 100644 --- a/test/Generator/ClassGeneratorTest.php +++ b/test/Generator/ClassGeneratorTest.php @@ -85,6 +85,10 @@ public function testPropertyAccessors() // add a new property $classGenerator->addProperty('prop3'); $this->assertEquals(count($classGenerator->getProperties()), 3); + + // Remove a property + $classGenerator->removeProperty('prop3'); + $this->assertEquals(count($classGenerator->getProperties()), 2); } public function testSetPropertyAlreadyExistsThrowsException() @@ -518,6 +522,20 @@ public function testCanAddConstant() $this->assertEquals($constant->getDefaultValue()->getValue(), 'value'); } + /** + * @group 6274 + */ + public function testCanRemoveConstant() + { + $classGenerator = new ClassGenerator(); + + $classGenerator->setName('My\Class'); + $classGenerator->addConstant('x', 'value'); + + $classGenerator->removeConstant('x'); + $this->assertEquals(count($classGenerator->getConstants()), 0); + } + /** * @group 6274 */