Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Make it possible to remove properties and constants form a class. #68

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/book/generator/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
18 changes: 18 additions & 0 deletions test/Generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
*/
Expand Down