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

completes hasSomething & removeSomething methods on the Classgenerator #26

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 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
10 changes: 10 additions & 0 deletions doc/book/generator/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen
public function addConstant($property)
public function getConstants()
public function getConstant($propertyName)
public function removeConstant($constantName)
public function setDocblock(Zend\Code\Generator\DocBlockGenerator $docblock)
public function getDocblock()
public function setName($name)
Expand All @@ -112,17 +113,26 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen
public function isAbstract()
public function setExtendedClass($extendedClass)
public function getExtendedClass()
public function hasExtentedClass()
public function removeExtentedClass()
public function setImplementedInterfaces(Array $implementedInterfaces)
public function getImplementedInterfaces()
public function addProperties(Array $properties)
public function addProperty($property)
public function getProperties()
public function getProperty($propertyName)
public function removeProperty($propertyName)
public function addMethods(Array $methods)
public function addMethod($method)
public function getMethods()
public function getMethod($methodName)
public function hasMethod($methodName)
public function hasUse($use)
public function removeUse($use)
public function hasUseAlias($use)
public function removeUseAlias($use)
public function hasImplementedInterface($implementedInterface)
public function removeImplementedInterface($implementedInterface)
public function isSourceDirty()
public function generate()
}
Expand Down
102 changes: 101 additions & 1 deletion src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ public function getExtendedClass()
return $this->extendedClass;
}

/**
* @return boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool (due to PHP7 compliance)

*/
public function hasExtentedClass()
{
return !empty($this->extendedClass);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if empty() is used to compare against null, then use null !== $this->extendedClass instead, as an empty string would pass this check (and would be invalid)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!empty($this->extendedClass)) {
    $output .= ' extends ' . $this->extendedClass;
}

both null and "" should be false.

This was present. I just reused...

}

/**
* @return ClassGenerator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self

*/
public function removeExtentedClass()
{
$this->setExtendedClass(null);
return $this;
}

/**
* @param array $implementedInterfaces
* @return ClassGenerator
Expand All @@ -447,9 +464,29 @@ public function getImplementedInterfaces()
return $this->implementedInterfaces;
}

/**
* @param string $implementedInterface
* @return bool
*/
public function hasImplementedInterface($implementedInterface)
{
return in_array($implementedInterface, $this->implementedInterfaces);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also check against \\ strings. Since you are basing against the 7.0 branch, you may want to check the type generator class

}

/**
* @param $implementedInterface
* @return ClassGenerator
*/
public function removeImplementedInterface($implementedInterface)
{
if ($this->hasImplementedInterface($implementedInterface)) {
unset($this->implementedInterfaces[array_search($implementedInterface, $this->implementedInterfaces)]);
}
return $this;
}

/**
* @param string $constantName
*
* @return PropertyGenerator|false
*/
public function getConstant($constantName)
Expand All @@ -469,6 +506,18 @@ public function getConstants()
return $this->constants;
}

/**
* @param string $constantName
* @return ClassGenerator
*/
public function removeConstant($constantName)
{
if ($this->hasConstant($constantName)) {
unset($this->constants[$constantName]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can actually always directly unset(), as unset() doesn't raise any errors nor has any return values

}
return $this;
}

/**
* @param string $constantName
* @return bool
Expand Down Expand Up @@ -667,6 +716,44 @@ public function addUse($use, $useAlias = null)
return $this;
}

/**
* @param string $use
* @return ClassGenerator
*/
public function hasUse($use)
{
return $this->traitUsageGenerator->hasUse($use);
}

/**
* @param string $use
* @return ClassGenerator
*/
public function removeUse($use)
{
$this->traitUsageGenerator->removeUse($use);
return $this;
}

/**
* @param string $use
* @return bool
*/
public function hasUseAlias($use)
{
return $this->traitUsageGenerator->hasUseAlias($use);
}

/**
* @param $use
* @return ClassGenerator
*/
public function removeUseAlias($use)
{
$this->traitUsageGenerator->removeUseAlias($use);
return $this;
}

/**
* Returns the "use" classes
*
Expand All @@ -677,6 +764,19 @@ public function getUses()
return $this->traitUsageGenerator->getUses();
}


/**
* @param string $propertyName
* @return ClassGenerator
*/
public function removeProperty($propertyName)
{
if ($this->hasProperty($propertyName)) {
unset($this->properties[$propertyName]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can always directly unset(), no check needed

}
return $this;
}

/**
* @param string $propertyName
* @return bool
Expand Down
66 changes: 66 additions & 0 deletions src/Generator/TraitUsageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function __construct(ClassGenerator $classGenerator)
*/
public function addUse($use, $useAlias = null)
{
$this->removeUse($use);

if (! empty($useAlias)) {
$use .= ' as ' . $useAlias;
}
Expand All @@ -64,6 +66,70 @@ public function getUses()
return array_values($this->uses);
}

/**
* @param $use
* @return bool
*/
public function hasUse($use)
{
foreach ($this->uses as $key => $value) {
$parts = explode(' ', $value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, the internal representation of this thing is stringly typed? :X ouch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish $this->uses wasn't protected: makes it so hard to migrate away from it :-(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

if ($parts[0] === $use) {
return true;
}
}

return false;
}

/**
* @param $use
* @return bool
*/
public function hasUseAlias($use)
{
foreach ($this->uses as $key => $value) {
$parts = explode(' as ', $value);
if ($parts[0] === $use and count($parts) == 2) {
return true;
}
};

return false;
}

/**
* @param $use
* @return TraitUsageGenerator
*/
public function removeUse($use)
{
foreach ($this->uses as $key => $value) {
$parts = explode(' ', $value);
if ($parts[0] === $use) {
unset($this->uses[$value]);
}
};

return $this;
}

/**
* @param $use
* @return TraitUsageGenerator
*/
public function removeUseAlias($use)
{
foreach ($this->uses as $key => $value) {
$parts = explode(' as ', $value);
if ($parts[0] === $use and count($parts) == 2) {
unset($this->uses[$value]);
}
};

return $this;
}

/**
* @inherit Zend\Code\Generator\TraitUsageInterface
*/
Expand Down
100 changes: 100 additions & 0 deletions test/Generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,51 @@ public function testExtendedClassAccessors()
$this->assertEquals($classGenerator->getExtendedClass(), 'ExtendedClass');
}

public function testHasExtendedClass()
{
$classGenerator = new ClassGenerator();
$classGenerator->setExtendedClass('ExtendedClass');

$this->assertTrue($classGenerator->hasExtentedClass());
}

public function testRemoveExtendedClass()
{
$classGenerator = new ClassGenerator();
$classGenerator->setExtendedClass('ExtendedClass');
$this->assertTrue($classGenerator->hasExtentedClass());

$classGenerator->removeExtentedClass();
$this->assertFalse($classGenerator->hasExtentedClass());
}

public function testImplementedInterfacesAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setImplementedInterfaces(['Class1', 'Class2']);
$this->assertEquals($classGenerator->getImplementedInterfaces(), ['Class1', 'Class2']);
}

public function testHasImplementedInterface()
{
$classGenerator = new ClassGenerator();
$classGenerator->setImplementedInterfaces(['Class1', 'Class2']);

$this->assertTrue($classGenerator->hasImplementedInterface('Class1'));
}

public function testRemoveImplementedInterface()
{
$classGenerator = new ClassGenerator();
$classGenerator->setImplementedInterfaces(['Class1', 'Class2']);

$this->assertTrue($classGenerator->hasImplementedInterface('Class1'));

$classGenerator->removeImplementedInterface('Class1');
$this->assertFalse($classGenerator->hasImplementedInterface('Class1'));
$this->assertTrue($classGenerator->hasImplementedInterface('Class2'));
}

public function testPropertyAccessors()
{
$classGenerator = new ClassGenerator();
Expand Down Expand Up @@ -193,6 +231,16 @@ public function testHasProperty()
$this->assertTrue($classGenerator->hasProperty('propertyOne'));
}

public function testRemoveProperty()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperty('propertyOne');
$this->assertTrue($classGenerator->hasProperty('propertyOne'));

$classGenerator->removeProperty('propertyOne');
$this->assertFalse($classGenerator->hasProperty('propertyOne'));
}

public function testToString()
{
$classGenerator = ClassGenerator::fromArray([
Expand Down Expand Up @@ -374,6 +422,48 @@ public function testPassingANamespacedClassnameShouldGenerateAClassnameWithoutIt
$this->assertContains('class FunClass', $received, $received);
}

public function testHasUse()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');

$this->assertTrue($classGenerator->hasUse('My\First\Use\Class'));
$this->assertTrue($classGenerator->hasUse('My\Second\Use\Class'));
}

public function testRemoveUse()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');

$this->assertTrue($classGenerator->hasUse('My\First\Use\Class'));
$this->assertTrue($classGenerator->hasUse('My\Second\Use\Class'));
$classGenerator->removeUse('My\First\Use\Class');
$classGenerator->removeUse('My\Second\Use\Class');
$this->assertFalse($classGenerator->hasUse('My\First\Use\Class'));
$this->assertFalse($classGenerator->hasUse('My\Second\Use\Class'));
}

public function testHasUseAlias()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');
$this->assertFalse($classGenerator->hasUseAlias('My\First\Use\Class'));
$this->assertTrue($classGenerator->hasUseAlias('My\Second\Use\Class'));
}

public function testRemoveUseAlias()
{
$classGenerator = new ClassGenerator();
$classGenerator->addUse('My\First\Use\Class', 'MyAlias');
$this->assertTrue($classGenerator->hasUseAlias('My\First\Use\Class'));
$classGenerator->removeUseAlias('My\First\Use\Class');
$this->assertFalse($classGenerator->hasUseAlias('My\First\Use\Class'));
}

/**
* @group ZF2-151
*/
Expand Down Expand Up @@ -642,6 +732,16 @@ public function testAddConstantThrowsExceptionOnDuplicate()
$classGenerator->addConstant('x', 'value1');
}

public function testRemoveConstant()
{
$classGenerator = new ClassGenerator();
$classGenerator->addConstant('constantOne', 'foo');
$this->assertTrue($classGenerator->hasConstant('constantOne'));

$classGenerator->removeConstant('constantOne');
$this->assertFalse($classGenerator->hasConstant('constantOne'));
}

/**
* @group 6274
*/
Expand Down