Skip to content
This repository was 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 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
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
156 changes: 128 additions & 28 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ClassGenerator extends AbstractGenerator
* Build a Code Generation Php Object from a Class Reflection
*
* @param ClassReflection $classReflection
* @return ClassGenerator
* @return self
*/
public static function fromReflection(ClassReflection $classReflection)
{
Expand Down Expand Up @@ -167,7 +167,7 @@ public static function fromReflection(ClassReflection $classReflection)
*
* @throws Exception\InvalidArgumentException
* @param array $array
* @return ClassGenerator
* @return self
*/
public static function fromArray(array $array)
{
Expand Down Expand Up @@ -262,7 +262,7 @@ public function __construct(

/**
* @param string $name
* @return ClassGenerator
* @return self
*/
public function setName($name)
{
Expand All @@ -286,7 +286,7 @@ public function getName()

/**
* @param string $namespaceName
* @return ClassGenerator
* @return self
*/
public function setNamespaceName($namespaceName)
{
Expand All @@ -304,7 +304,7 @@ public function getNamespaceName()

/**
* @param FileGenerator $fileGenerator
* @return ClassGenerator
* @return self
*/
public function setContainingFileGenerator(FileGenerator $fileGenerator)
{
Expand All @@ -322,7 +322,7 @@ public function getContainingFileGenerator()

/**
* @param DocBlockGenerator $docBlock
* @return ClassGenerator
* @return self
*/
public function setDocBlock(DocBlockGenerator $docBlock)
{
Expand All @@ -340,7 +340,7 @@ public function getDocBlock()

/**
* @param array|string $flags
* @return ClassGenerator
* @return self
*/
public function setFlags($flags)
{
Expand All @@ -359,7 +359,7 @@ public function setFlags($flags)

/**
* @param string $flag
* @return ClassGenerator
* @return self
*/
public function addFlag($flag)
{
Expand All @@ -369,7 +369,7 @@ public function addFlag($flag)

/**
* @param string $flag
* @return ClassGenerator
* @return self
*/
public function removeFlag($flag)
{
Expand All @@ -379,7 +379,7 @@ public function removeFlag($flag)

/**
* @param bool $isAbstract
* @return ClassGenerator
* @return self
*/
public function setAbstract($isAbstract)
{
Expand All @@ -396,7 +396,7 @@ public function isAbstract()

/**
* @param bool $isFinal
* @return ClassGenerator
* @return self
*/
public function setFinal($isFinal)
{
Expand All @@ -413,7 +413,7 @@ public function isFinal()

/**
* @param string $extendedClass
* @return ClassGenerator
* @return self
*/
public function setExtendedClass($extendedClass)
{
Expand All @@ -429,12 +429,33 @@ public function getExtendedClass()
return $this->extendedClass;
}

/**
* @return bool
*/
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 self
*/
public function removeExtentedClass()
{
$this->setExtendedClass(null);
return $this;
}

/**
* @param array $implementedInterfaces
* @return ClassGenerator
* @return self
*/
public function setImplementedInterfaces(array $implementedInterfaces)
{
array_map(function ($implementedInterface) {
return (string) TypeGenerator::fromTypeString($implementedInterface);
}, $implementedInterfaces);

$this->implementedInterfaces = $implementedInterfaces;
return $this;
}
Expand All @@ -447,9 +468,29 @@ public function getImplementedInterfaces()
return $this->implementedInterfaces;
}

/**
* @param string $implementedInterface
* @return bool
*/
public function hasImplementedInterface($implementedInterface)
{
$implementedInterface = (string) TypeGenerator::fromTypeString($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 self
*/
public function removeImplementedInterface($implementedInterface)
{
$implementedInterface = (string) TypeGenerator::fromTypeString($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 +510,17 @@ public function getConstants()
return $this->constants;
}

/**
* @param string $constantName
* @return self
*/
public function removeConstant($constantName)
{
unset($this->constants[$constantName]);

return $this;
}

/**
* @param string $constantName
* @return bool
Expand All @@ -483,7 +535,7 @@ public function hasConstant($constantName)
*
* @param PropertyGenerator $constant
* @throws Exception\InvalidArgumentException
* @return ClassGenerator
* @return self
*/
public function addConstantFromGenerator(PropertyGenerator $constant)
{
Expand Down Expand Up @@ -516,7 +568,7 @@ public function addConstantFromGenerator(PropertyGenerator $constant)
*
* @throws Exception\InvalidArgumentException
*
* @return ClassGenerator
* @return self
*/
public function addConstant($name, $value)
{
Expand All @@ -537,7 +589,7 @@ public function addConstant($name, $value)
/**
* @param PropertyGenerator[]|array[] $constants
*
* @return ClassGenerator
* @return self
*/
public function addConstants(array $constants)
{
Expand All @@ -556,7 +608,7 @@ public function addConstants(array $constants)

/**
* @param array $properties
* @return ClassGenerator
* @return self
*/
public function addProperties(array $properties)
{
Expand All @@ -582,7 +634,7 @@ public function addProperties(array $properties)
* @param string|array $defaultValue
* @param int $flags
* @throws Exception\InvalidArgumentException
* @return ClassGenerator
* @return self
*/
public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC)
{
Expand All @@ -608,7 +660,7 @@ public function addProperty($name, $defaultValue = null, $flags = PropertyGenera
*
* @param PropertyGenerator $property
* @throws Exception\InvalidArgumentException
* @return ClassGenerator
* @return self
*/
public function addPropertyFromGenerator(PropertyGenerator $property)
{
Expand Down Expand Up @@ -659,14 +711,52 @@ public function getProperty($propertyName)
*
* @param string $use
* @param string|null $useAlias
* @return ClassGenerator
* @return self
*/
public function addUse($use, $useAlias = null)
{
$this->traitUsageGenerator->addUse($use, $useAlias);
return $this;
}

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

/**
* @param string $use
* @return self
*/
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 self
*/
public function removeUseAlias($use)
{
$this->traitUsageGenerator->removeUseAlias($use);
return $this;
}

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


/**
* @param string $propertyName
* @return self
*/
public function removeProperty($propertyName)
{
unset($this->properties[$propertyName]);

return $this;
}

/**
* @param string $propertyName
* @return bool
Expand All @@ -688,7 +790,7 @@ public function hasProperty($propertyName)

/**
* @param array $methods
* @return ClassGenerator
* @return self
*/
public function addMethods(array $methods)
{
Expand Down Expand Up @@ -716,7 +818,7 @@ public function addMethods(array $methods)
* @param string $body
* @param string $docBlock
* @throws Exception\InvalidArgumentException
* @return ClassGenerator
* @return self
*/
public function addMethod(
$name = null,
Expand All @@ -741,7 +843,7 @@ public function addMethod(
*
* @param MethodGenerator $method
* @throws Exception\InvalidArgumentException
* @return ClassGenerator
* @return self
*/
public function addMethodFromGenerator(MethodGenerator $method)
{
Expand Down Expand Up @@ -777,13 +879,11 @@ public function getMethod($methodName)

/**
* @param string $methodName
* @return ClassGenerator
* @return self
*/
public function removeMethod($methodName)
{
if ($this->hasMethod($methodName)) {
unset($this->methods[strtolower($methodName)]);
}
unset($this->methods[strtolower($methodName)]);

return $this;
}
Expand Down
Loading