diff --git a/library/Zend/Code/Generator/DocBlock/Tag.php b/library/Zend/Code/Generator/DocBlock/Tag.php index e5426bf8fef..4241724f7c6 100644 --- a/library/Zend/Code/Generator/DocBlock/Tag.php +++ b/library/Zend/Code/Generator/DocBlock/Tag.php @@ -9,94 +9,43 @@ namespace Zend\Code\Generator\DocBlock; -use ReflectionClass; -use ReflectionMethod; -use Zend\Code\Generator\AbstractGenerator; -use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionDocBlockTag; +use Zend\Code\Generator\DocBlock\Tag\GenericTag; +use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionTagInterface; -class Tag extends AbstractGenerator +/** + * @deprecated Deprecated in 2.3. Use GenericTag instead + */ +class Tag extends GenericTag { - /** - * @var string - */ - protected $name = null; /** - * @var string - */ - protected $description = null; - - /** - * Build a Tag generator object from a reflection object - * - * @param ReflectionDocBlockTag $reflectionTag + * @param ReflectionTagInterface $reflectionTag * @return Tag + * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionDocBlockTag $reflectionTag) + public static function fromReflection(ReflectionTagInterface $reflectionTag) { - $tagName = $reflectionTag->getName(); - - $codeGenDocBlockTag = new static(); - $codeGenDocBlockTag->setName($tagName); - - // transport any properties via accessors and mutators from reflection to codegen object - $reflectionClass = new ReflectionClass($reflectionTag); - foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - if (substr($method->getName(), 0, 3) == 'get') { - $propertyName = substr($method->getName(), 3); - if (method_exists($codeGenDocBlockTag, 'set' . $propertyName)) { - $codeGenDocBlockTag->{'set' . $propertyName}($reflectionTag->{'get' . $propertyName}()); - } - } - } - - return $codeGenDocBlockTag; - } - - /** - * @param string $name - * @return Tag - */ - public function setName($name) - { - $this->name = ltrim($name, '@'); - return $this; - } - - /** - * @return string - */ - public function getName() - { - return $this->name; + $tagManager = new TagManager(); + $tagManager->initializeDefaultTags(); + return $tagManager->createTagFromReflection($reflectionTag); } /** * @param string $description * @return Tag + * @deprecated Deprecated in 2.3. Use GenericTag::setContent() instead */ public function setDescription($description) { - $this->description = $description; - return $this; + return $this->setContent($description); } /** * @return string + * @deprecated Deprecated in 2.3. Use GenericTag::getContent() instead */ public function getDescription() { - return $this->description; - } - - /** - * @return string - */ - public function generate() - { - $output = '@' . $this->name - . (($this->description != null) ? ' ' . $this->description : ''); - - return $output; + return $this->getContent(); } } diff --git a/library/Zend/Code/Generator/DocBlock/Tag/AbstractTypeableTag.php b/library/Zend/Code/Generator/DocBlock/Tag/AbstractTypeableTag.php new file mode 100644 index 00000000000..ed6340af6fa --- /dev/null +++ b/library/Zend/Code/Generator/DocBlock/Tag/AbstractTypeableTag.php @@ -0,0 +1,96 @@ +setTypes($types); + } + + if (!empty($description)) { + $this->setDescription($description); + } + } + + /** + * @param string $description + * @return ReturnTag + */ + public function setDescription($description) + { + $this->description = $description; + return $this; + } + + /** + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * Array of types or string with types delimited by pipe (|) + * e.g. array('int', 'null') or "int|null" + * + * @param array|string $types + * @return ReturnTag + */ + public function setTypes($types) + { + if (is_string($types)) { + $types = explode('|', $types); + } + $this->types = $types; + return $this; + } + + /** + * @return array + */ + public function getTypes() + { + return $this->types; + } + + /** + * @param string $delimiter + * @return string + */ + public function getTypesAsString($delimiter = '|') + { + return implode($delimiter, $this->types); + } +} diff --git a/library/Zend/Code/Generator/DocBlock/Tag/AuthorTag.php b/library/Zend/Code/Generator/DocBlock/Tag/AuthorTag.php index bced4e2d5cb..edf40dfcdd4 100644 --- a/library/Zend/Code/Generator/DocBlock/Tag/AuthorTag.php +++ b/library/Zend/Code/Generator/DocBlock/Tag/AuthorTag.php @@ -9,71 +9,91 @@ namespace Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionDocBlockTag; +use Zend\Code\Generator\AbstractGenerator; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionTagInterface; -class AuthorTag extends Tag +class AuthorTag extends AbstractGenerator implements TagInterface { /** * @var string */ - protected $datatype = null; + protected $authorName = null; /** * @var string */ - protected $paramName = null; + protected $authorEmail = null; /** - * @param ReflectionDocBlockTag $reflectionTagParam - * @return AuthorTag + * @param string $authorName + * @param string $authorEmail + */ + public function __construct($authorName = null, $authorEmail = null) + { + if (!empty($authorName)) { + $this->setAuthorName($authorName); + } + + if (!empty($authorEmail)) { + $this->setAuthorEmail($authorEmail); + } + } + + /** + * @param ReflectionTagInterface $reflectionTag + * @return ReturnTag + * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionDocBlockTag $reflectionTagParam) + public static function fromReflection(ReflectionTagInterface $reflectionTag) { - $authorTag = new self(); - $authorTag - ->setName('author') - ->setAuthorName($reflectionTagParam->getType()) // @todo rename - ->setAuthorEmail($reflectionTagParam->getVariableName()) - ->setDescription($reflectionTagParam->getDescription()); + $tagManager = new TagManager(); + $tagManager->initializeDefaultTags(); + return $tagManager->createTagFromReflection($reflectionTag); + } - return $authorTag; + /** + * @return string + */ + public function getName() + { + return 'author'; } /** - * @param string $datatype + * @param string $authorEmail * @return AuthorTag */ - public function setDatatype($datatype) + public function setAuthorEmail($authorEmail) { - $this->datatype = (string) $datatype; + $this->authorEmail = $authorEmail; return $this; } /** * @return string */ - public function getDatatype() + public function getAuthorEmail() { - return $this->datatype; + return $this->authorEmail; } /** - * @param string $paramName + * @param string $authorName * @return AuthorTag */ - public function setParamName($paramName) + public function setAuthorName($authorName) { - $this->paramName = (string) $paramName; + $this->authorName = $authorName; return $this; } /** * @return string */ - public function getParamName() + public function getAuthorName() { - return $this->paramName; + return $this->authorName; } /** @@ -81,10 +101,9 @@ public function getParamName() */ public function generate() { - $output = '@param ' - . (($this->datatype != null) ? $this->datatype : 'unknown') - . (($this->paramName != null) ? ' $' . $this->paramName : '') - . (($this->description != null) ? ' ' . $this->description : ''); + $output = '@author' + . ((!empty($this->authorName)) ? ' ' . $this->authorName : '') + . ((!empty($this->authorEmail)) ? ' <' . $this->authorEmail . '>' : ''); return $output; } diff --git a/library/Zend/Code/Generator/DocBlock/Tag/GenericTag.php b/library/Zend/Code/Generator/DocBlock/Tag/GenericTag.php new file mode 100644 index 00000000000..63afa951bfd --- /dev/null +++ b/library/Zend/Code/Generator/DocBlock/Tag/GenericTag.php @@ -0,0 +1,88 @@ +setName($name); + } + + if (!empty($content)) { + $this->setContent($content); + } + } + + /** + * @param string $name + * @return GenericTag + */ + public function setName($name) + { + $this->name = ltrim($name, '@'); + return $this; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $content + * @return GenericTag + */ + public function setContent($content) + { + $this->content = $content; + return $this; + } + + /** + * @return string + */ + public function getContent() + { + return $this->content; + } + + /** + * @return string + */ + public function generate() + { + $output = '@' . $this->name + . ((!empty($this->content)) ? ' ' . $this->content : ''); + + return $output; + } +} diff --git a/library/Zend/Code/Generator/DocBlock/Tag/LicenseTag.php b/library/Zend/Code/Generator/DocBlock/Tag/LicenseTag.php index d6149c31037..be686d3b321 100644 --- a/library/Zend/Code/Generator/DocBlock/Tag/LicenseTag.php +++ b/library/Zend/Code/Generator/DocBlock/Tag/LicenseTag.php @@ -9,10 +9,11 @@ namespace Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionDocBlockTag; +use Zend\Code\Generator\AbstractGenerator; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionTagInterface; -class LicenseTag extends Tag +class LicenseTag extends AbstractGenerator implements TagInterface { /** * @var string @@ -25,38 +26,42 @@ class LicenseTag extends Tag protected $licenseName = null; /** - * @param array $options + * @param string $url + * @param string $licenseName */ - public function __construct(array $options = array()) + public function __construct($url = null, $licenseName = null) { - parent::__construct($options); - - if (isset($options['url'])) { - $this->setUrl($options['url']); + if (!empty($url)) { + $this->setUrl($url); } - if (empty($this->name)) { - $this->setName('license'); + if (!empty($licenseName)) { + $this->setLicenseName($licenseName); } } /** - * @param ReflectionDocBlockTag $reflectionTagLicense - * @return LicenseTag + * @param ReflectionTagInterface $reflectionTag + * @return ReturnTag + * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionDocBlockTag $reflectionTagLicense) + public static function fromReflection(ReflectionTagInterface $reflectionTag) { - $licenseTag = new static(); - $licenseTag - ->setName('license') - ->setUrl($reflectionTagLicense->getUrl()) - ->setLicenseName($reflectionTagLicense->getDescription()); + $tagManager = new TagManager(); + $tagManager->initializeDefaultTags(); + return $tagManager->createTagFromReflection($reflectionTag); + } - return $licenseTag; + /** + * @return string + */ + public function getName() + { + return 'license'; } /** - * @param string $url + * @param string $url * @return LicenseTag */ public function setUrl($url) @@ -96,10 +101,9 @@ public function getLicenseName() */ public function generate() { - $output = '@license ' - . (($this->url != null) ? $this->url : 'unknown') - . (($this->licenseName != null) ? ' ' . $this->licenseName : '') - . (($this->description != null) ? ' ' . $this->description : ''); + $output = '@license' + . ((!empty($this->url)) ? ' ' . $this->url : '') + . ((!empty($this->licenseName)) ? ' ' . $this->licenseName : ''); return $output; } diff --git a/library/Zend/Code/Generator/DocBlock/Tag/MethodTag.php b/library/Zend/Code/Generator/DocBlock/Tag/MethodTag.php new file mode 100644 index 00000000000..65b88522b4d --- /dev/null +++ b/library/Zend/Code/Generator/DocBlock/Tag/MethodTag.php @@ -0,0 +1,98 @@ +setMethodName($methodName); + } + + $this->setIsStatic((bool) $isStatic); + + parent::__construct($types, $description); + } + + /** + * @return string + */ + public function getName() + { + return 'method'; + } + + /** + * @param boolean $isStatic + * @return MethodTag + */ + public function setIsStatic($isStatic) + { + $this->isStatic = $isStatic; + return $this; + } + + /** + * @return boolean + */ + public function isStatic() + { + return $this->isStatic; + } + + /** + * @param string $methodName + * @return MethodTag + */ + public function setMethodName($methodName) + { + $this->methodName = rtrim($methodName, ')('); + return $this; + } + + /** + * @return string + */ + public function getMethodName() + { + return $this->methodName; + } + + /** + * @return string + */ + public function generate() + { + $output = '@method' + . (($this->isStatic) ? ' static' : '') + . ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '') + . ((!empty($this->methodName)) ? ' ' . $this->methodName . '()' : '') + . ((!empty($this->description)) ? ' ' . $this->description : ''); + + return $output; + } +} diff --git a/library/Zend/Code/Generator/DocBlock/Tag/ParamTag.php b/library/Zend/Code/Generator/DocBlock/Tag/ParamTag.php index 175df2f18b8..cadfb8665c2 100644 --- a/library/Zend/Code/Generator/DocBlock/Tag/ParamTag.php +++ b/library/Zend/Code/Generator/DocBlock/Tag/ParamTag.php @@ -9,71 +9,104 @@ namespace Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionDocBlockTag; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionTagInterface; -class ParamTag extends Tag +class ParamTag extends AbstractTypeableTag implements TagInterface { /** * @var string */ - protected $datatype = null; + protected $variableName = null; /** - * @var string + * @param string $variableName + * @param array $types + * @param string $description */ - protected $paramName = null; + public function __construct($variableName = null, $types = array(), $description = null) + { + if (!empty($variableName)) { + $this->setVariableName($variableName); + } + + parent::__construct($types, $description); + } /** - * @param ReflectionDocBlockTag $reflectionTagParam - * @return ParamTag + * @param ReflectionTagInterface $reflectionTag + * @return ReturnTag + * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - public static function fromReflection(ReflectionDocBlockTag $reflectionTagParam) + public static function fromReflection(ReflectionTagInterface $reflectionTag) { - $paramTag = new static(); - $paramTag - ->setName('param') - ->setDatatype($reflectionTagParam->getType()) // @todo rename - ->setParamName($reflectionTagParam->getVariableName()) - ->setDescription($reflectionTagParam->getDescription()); + $tagManager = new TagManager(); + $tagManager->initializeDefaultTags(); + return $tagManager->createTagFromReflection($reflectionTag); + } - return $paramTag; + /** + * @return string + */ + public function getName() + { + return 'param'; } /** - * @param string $datatype + * @param string $variableName * @return ParamTag */ - public function setDatatype($datatype) + public function setVariableName($variableName) { - $this->datatype = $datatype; + $this->variableName = ltrim($variableName, '$'); return $this; } /** * @return string */ + public function getVariableName() + { + return $this->variableName; + } + + /** + * @param string $datatype + * @return ReturnTag + * @deprecated Deprecated in 2.3. Use setTypes() instead + */ + public function setDatatype($datatype) + { + return $this->setTypes($datatype); + } + + /** + * @return string + * @deprecated Deprecated in 2.3. Use getTypes() or getTypesAsString() instead + */ public function getDatatype() { - return $this->datatype; + return $this->getTypesAsString(); } /** * @param string $paramName * @return ParamTag + * @deprecated Deprecated in 2.3. Use setVariableName() instead */ public function setParamName($paramName) { - $this->paramName = $paramName; - return $this; + return $this->setVariableName($paramName); } /** * @return string + * @deprecated Deprecated in 2.3. Use getVariableName() instead */ public function getParamName() { - return $this->paramName; + return $this->getVariableName(); } /** @@ -81,10 +114,10 @@ public function getParamName() */ public function generate() { - $output = '@param ' - . (($this->datatype != null) ? $this->datatype : 'unknown') - . (($this->paramName != null) ? ' $' . $this->paramName : '') - . (($this->description != null) ? ' ' . $this->description : ''); + $output = '@param' + . ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '') + . ((!empty($this->variableName)) ? ' $' . $this->variableName : '') + . ((!empty($this->description)) ? ' ' . $this->description : ''); return $output; } diff --git a/library/Zend/Code/Generator/DocBlock/Tag/PropertyTag.php b/library/Zend/Code/Generator/DocBlock/Tag/PropertyTag.php new file mode 100644 index 00000000000..b5a5580862b --- /dev/null +++ b/library/Zend/Code/Generator/DocBlock/Tag/PropertyTag.php @@ -0,0 +1,71 @@ +setPropertyName($propertyName); + } + + parent::__construct($types, $description); + } + + /** + * @return string + */ + public function getName() + { + return 'property'; + } + + /** + * @param string $propertyName + * @return MethodTag + */ + public function setPropertyName($propertyName) + { + $this->propertyName = ltrim($propertyName, '$'); + return $this; + } + + /** + * @return string + */ + public function getPropertyName() + { + return $this->propertyName; + } + + /** + * @return string + */ + public function generate() + { + $output = '@property' + . ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '') + . ((!empty($this->propertyName)) ? ' $' . $this->propertyName : '') + . ((!empty($this->description)) ? ' ' . $this->description : ''); + + return $output; + } +} diff --git a/library/Zend/Code/Generator/DocBlock/Tag/ReturnTag.php b/library/Zend/Code/Generator/DocBlock/Tag/ReturnTag.php index 8ff364b457b..059a934bf13 100644 --- a/library/Zend/Code/Generator/DocBlock/Tag/ReturnTag.php +++ b/library/Zend/Code/Generator/DocBlock/Tag/ReturnTag.php @@ -9,47 +9,48 @@ namespace Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Generator\DocBlock\Tag; -use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionDocBlockTag; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlock\Tag\TagInterface as ReflectionTagInterface; -class ReturnTag extends Tag +class ReturnTag extends AbstractTypeableTag implements TagInterface { /** - * @var string + * @param ReflectionTagInterface $reflectionTag + * @return ReturnTag + * @deprecated Deprecated in 2.3. Use TagManager::createTagFromReflection() instead */ - protected $datatype = null; + public static function fromReflection(ReflectionTagInterface $reflectionTag) + { + $tagManager = new TagManager(); + $tagManager->initializeDefaultTags(); + return $tagManager->createTagFromReflection($reflectionTag); + } /** - * @param ReflectionDocBlockTag $reflectionTagReturn - * @return ReturnTag + * @return string */ - public static function fromReflection(ReflectionDocBlockTag $reflectionTagReturn) + public function getName() { - $returnTag = new static(); - $returnTag - ->setName('return') - ->setDatatype($reflectionTagReturn->getType()) // @todo rename - ->setDescription($reflectionTagReturn->getDescription()); - - return $returnTag; + return 'return'; } /** - * @param string $datatype + * @param string $datatype * @return ReturnTag + * @deprecated Deprecated in 2.3. Use setTypes() instead */ public function setDatatype($datatype) { - $this->datatype = $datatype; - return $this; + return $this->setTypes($datatype); } /** * @return string + * @deprecated Deprecated in 2.3. Use getTypes() or getTypesAsString() instead */ public function getDatatype() { - return $this->datatype; + return $this->getTypesAsString(); } /** @@ -57,6 +58,10 @@ public function getDatatype() */ public function generate() { - return '@return ' . $this->datatype . ' ' . $this->description; + $output = '@return ' + . $this->getTypesAsString() + . ((!empty($this->description)) ? ' ' . $this->description : ''); + + return $output; } } diff --git a/library/Zend/Code/Generator/DocBlock/Tag/TagInterface.php b/library/Zend/Code/Generator/DocBlock/Tag/TagInterface.php new file mode 100644 index 00000000000..149ec8e9b29 --- /dev/null +++ b/library/Zend/Code/Generator/DocBlock/Tag/TagInterface.php @@ -0,0 +1,16 @@ +types)) ? ' ' . $this->getTypesAsString() : '') + . ((!empty($this->description)) ? ' ' . $this->description : ''); + + return $output; + } +} diff --git a/library/Zend/Code/Generator/DocBlock/TagManager.php b/library/Zend/Code/Generator/DocBlock/TagManager.php new file mode 100644 index 00000000000..149d4299626 --- /dev/null +++ b/library/Zend/Code/Generator/DocBlock/TagManager.php @@ -0,0 +1,70 @@ +addPrototype(new Tag\ParamTag()); + $this->addPrototype(new Tag\ReturnTag()); + $this->addPrototype(new Tag\MethodTag()); + $this->addPrototype(new Tag\PropertyTag()); + $this->addPrototype(new Tag\AuthorTag()); + $this->addPrototype(new Tag\LicenseTag()); + $this->addPrototype(new Tag\ThrowsTag()); + $this->setGenericPrototype(new Tag\GenericTag()); + } + + /** + * @param ReflectionTagInterface $reflectionTag + * @return TagInterface + */ + public function createTagFromReflection(ReflectionTagInterface $reflectionTag) + { + $tagName = $reflectionTag->getName(); + + /* @var TagInterface $newTag */ + $newTag = $this->getClonedPrototype($tagName); + + // transport any properties via accessors and mutators from reflection to codegen object + $reflectionClass = new \ReflectionClass($reflectionTag); + foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { + if (substr($method->getName(), 0, 3) == 'get') { + $propertyName = substr($method->getName(), 3); + if (method_exists($newTag, 'set' . $propertyName)) { + $newTag->{'set' . $propertyName}($reflectionTag->{'get' . $propertyName}()); + } + } elseif (substr($method->getName(), 0, 2) == 'is') { + $propertyName = ucfirst($method->getName()); + if (method_exists($newTag, 'set' . $propertyName)) { + $newTag->{'set' . $propertyName}($reflectionTag->{$method->getName()}()); + } + } + } + return $newTag; + } +} diff --git a/library/Zend/Code/Generator/DocBlockGenerator.php b/library/Zend/Code/Generator/DocBlockGenerator.php index 78f0b7febfc..7d37f8738d9 100644 --- a/library/Zend/Code/Generator/DocBlockGenerator.php +++ b/library/Zend/Code/Generator/DocBlockGenerator.php @@ -9,7 +9,9 @@ namespace Zend\Code\Generator; -use Zend\Code\Generator\DocBlock\Tag as DocBlockTag; +use Zend\Code\Generator\DocBlock\Tag; +use Zend\Code\Generator\DocBlock\Tag\TagInterface; +use Zend\Code\Generator\DocBlock\TagManager; use Zend\Code\Reflection\DocBlockReflection; class DocBlockGenerator extends AbstractGenerator @@ -39,6 +41,8 @@ class DocBlockGenerator extends AbstractGenerator */ protected $wordwrap = true; + protected static $tagManager; + /** * Build a DocBlock generator object from a reflection object * @@ -56,7 +60,7 @@ public static function fromReflection(DocBlockReflection $reflectionDocBlock) $docBlock->setLongDescription($reflectionDocBlock->getLongDescription()); foreach ($reflectionDocBlock->getTags() as $tag) { - $docBlock->setTag(DocBlockTag::fromReflection($tag)); + $docBlock->setTag(self::getTagManager()->createTagFromReflection($tag)); } return $docBlock; @@ -82,6 +86,7 @@ public static function fromArray(array $array) switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) { case 'shortdescription': $docBlock->setShortDescription($value); + break; case 'longdescription': $docBlock->setLongDescription($value); break; @@ -94,6 +99,16 @@ public static function fromArray(array $array) return $docBlock; } + protected static function getTagManager() + { + if (!isset(self::$tagManager)) { + self::$tagManager = new TagManager(); + self::$tagManager->initializeDefaultTags(); + } + return self::$tagManager; + } + + /** * @param string $shortDescription * @param string $longDescription @@ -162,17 +177,20 @@ public function setTags(array $tags) } /** - * @param array|DocBlockTag $tag + * @param array|TagInterface $tag * @throws Exception\InvalidArgumentException * @return DocBlockGenerator */ public function setTag($tag) { if (is_array($tag)) { - $tag = new DocBlockTag($tag); - } elseif (!$tag instanceof DocBlockTag) { + // use deprecated Tag class for backward compatiblity to old array-keys + $genericTag = new Tag(); + $genericTag->setOptions($tag); + $tag = $genericTag; + } elseif (!$tag instanceof TagInterface) { throw new Exception\InvalidArgumentException(sprintf( - '%s expects either an array of method options or an instance of %s\DocBlock\Tag', + '%s expects either an array of method options or an instance of %s\DocBlock\Tag\TagInterface', __METHOD__, __NAMESPACE__ )); @@ -183,7 +201,7 @@ public function setTag($tag) } /** - * @return DocBlockTag[] + * @return TagInterface[] */ public function getTags() { @@ -191,10 +209,8 @@ public function getTags() } /** - * Set the word wrap - * * @param bool $value - * @return \Zend\Code\Generator\DocBlockGenerator + * @return DocBlockGenerator */ public function setWordWrap($value) { @@ -203,8 +219,6 @@ public function setWordWrap($value) } /** - * Get the word wrap - * * @return bool */ public function getWordWrap() @@ -229,6 +243,7 @@ public function generate() $output .= $ld . self::LINE_FEED . self::LINE_FEED; } + /* @var $tag GeneratorInterface */ foreach ($this->getTags() as $tag) { $output .= $tag->generate() . self::LINE_FEED; } diff --git a/library/Zend/Code/Generic/Prototype/PrototypeClassFactory.php b/library/Zend/Code/Generic/Prototype/PrototypeClassFactory.php new file mode 100644 index 00000000000..dd84ec68584 --- /dev/null +++ b/library/Zend/Code/Generic/Prototype/PrototypeClassFactory.php @@ -0,0 +1,122 @@ +addPrototype($prototype); + } + + if ($genericPrototype) { + $this->setGenericPrototype($genericPrototype); + } + } + + /** + * @param PrototypeInterface $prototype + * @throws Exception\InvalidArgumentException + */ + public function addPrototype(PrototypeInterface $prototype) + { + $prototypeName = $this->normalizeName($prototype->getName()); + + if (isset($this->prototypes[$prototypeName])) { + throw new Exception\InvalidArgumentException('A prototype with this name already exists in this manager'); + } + + $this->prototypes[$prototypeName] = $prototype; + } + + /** + * @param PrototypeGenericInterface $prototype + * @throws Exception\InvalidArgumentException + */ + public function setGenericPrototype(PrototypeGenericInterface $prototype) + { + if (isset($this->genericPrototype)) { + throw new Exception\InvalidArgumentException('A default prototype is already set'); + } + + $this->genericPrototype = $prototype; + } + + /** + * @param string $name + * @return mixed + */ + protected function normalizeName($name) + { + return str_replace(array('-', '_'), '', $name); + } + + /** + * @param string $name + * @return bool + */ + public function hasPrototype($name) + { + $name = $this->normalizeName($name); + return isset($this->prototypes[$name]); + } + + /** + * @param string$prototypeName + * @return PrototypeInterface + * @throws Exception\RuntimeException + */ + public function getClonedPrototype($prototypeName) + { + $prototypeName = $this->normalizeName($prototypeName); + + if (!$this->hasPrototype($prototypeName) && !isset($this->genericPrototype)) { + throw new Exception\RuntimeException('This tag name is not supported by this tag manager'); + } + + if (!$this->hasPrototype($prototypeName)) { + $newPrototype = clone $this->genericPrototype; + $newPrototype->setName($prototypeName); + } else { + $newPrototype = clone $this->prototypes[$prototypeName]; + } + + return $newPrototype; + } +} diff --git a/library/Zend/Code/Generic/Prototype/PrototypeGenericInterface.php b/library/Zend/Code/Generic/Prototype/PrototypeGenericInterface.php new file mode 100644 index 00000000000..cd85f006e5d --- /dev/null +++ b/library/Zend/Code/Generic/Prototype/PrototypeGenericInterface.php @@ -0,0 +1,18 @@ +addTagPrototype($prototype); - } - } elseif ($prototypes === self::USE_DEFAULT_PROTOTYPES) { - $this->useDefaultPrototypes(); - } - } - /** * @return void */ - public function useDefaultPrototypes() + public function initializeDefaultTags() { - $this->addTagPrototype(new Tag\ParamTag()); - $this->addTagPrototype(new Tag\ReturnTag()); - $this->addTagPrototype(new Tag\MethodTag()); - $this->addTagPrototype(new Tag\PropertyTag()); - $this->addTagPrototype(new Tag\AuthorTag()); - $this->addTagPrototype(new Tag\LicenseTag()); - $this->addTagPrototype(new Tag\ThrowsTag()); - $this->addTagPrototype(new Tag\GenericTag()); + $this->addPrototype(new Tag\ParamTag()); + $this->addPrototype(new Tag\ReturnTag()); + $this->addPrototype(new Tag\MethodTag()); + $this->addPrototype(new Tag\PropertyTag()); + $this->addPrototype(new Tag\AuthorTag()); + $this->addPrototype(new Tag\LicenseTag()); + $this->addPrototype(new Tag\ThrowsTag()); + $this->setGenericPrototype(new Tag\GenericTag()); } /** - * @param TagInterface $tag - * @throws Exception\InvalidArgumentException - */ - public function addTagPrototype(TagInterface $tag) - { - $tagName = str_replace(array('-', '_'), '', $tag->getName()); - - if (in_array($tagName, $this->tagNames)) { - throw new Exception\InvalidArgumentException('A tag with this name already exists in this manager'); - } - - $this->tagNames[] = $tagName; - $this->tags[] = $tag; - - if ($tag instanceof GenericTag) { - $this->genericTag = $tag; - } - } - - /** - * @param string $tagName - * @return bool - */ - public function hasTag($tagName) - { - // otherwise, only if its name exists as a key - return in_array(str_replace(array('-', '_'), '', $tagName), $this->tagNames); - } - - /** - * @param string $tagName - * @param string $content - * @return GenericTag - * @throws Exception\RuntimeException + * @param string $tagName + * @param string $content + * @return TagInterface */ public function createTag($tagName, $content = null) { - $tagName = str_replace(array('-', '_'), '', $tagName); - - if (!$this->hasTag($tagName) && !isset($this->genericTag)) { - throw new Exception\RuntimeException('This tag name is not supported by this tag manager'); - } + /* @var TagInterface $newTag */ + $newTag = $this->getClonedPrototype($tagName); - $index = array_search($tagName, $this->tagNames); - - /* @var TagInterface $tag */ - $tag = ($index !== false) ? $this->tags[$index] : $this->genericTag; - - $newTag = clone $tag; if ($content) { $newTag->initialize($content); } - if ($newTag instanceof GenericTag) { - $newTag->setName($tagName); - } - return $newTag; } } diff --git a/library/Zend/Code/Reflection/DocBlockReflection.php b/library/Zend/Code/Reflection/DocBlockReflection.php index 3b2257e927f..f57ca3d976d 100644 --- a/library/Zend/Code/Reflection/DocBlockReflection.php +++ b/library/Zend/Code/Reflection/DocBlockReflection.php @@ -84,7 +84,11 @@ public static function export() */ public function __construct($commentOrReflector, DocBlockTagManager $tagManager = null) { - $this->tagManager = $tagManager ? : new DocBlockTagManager(DocBlockTagManager::USE_DEFAULT_PROTOTYPES); + if (!$tagManager) { + $tagManager = new DocBlockTagManager(); + $tagManager->initializeDefaultTags(); + } + $this->tagManager = $tagManager; if ($commentOrReflector instanceof Reflector) { $this->reflector = $commentOrReflector; diff --git a/tests/ZendTest/Code/Generator/AbstractGeneratorTest.php b/tests/ZendTest/Code/Generator/AbstractGeneratorTest.php index 07f9171cba8..1543d8b8c33 100644 --- a/tests/ZendTest/Code/Generator/AbstractGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/AbstractGeneratorTest.php @@ -5,16 +5,11 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ diff --git a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php index 44f7d9fa87f..1b5f6d6bd15 100644 --- a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php @@ -16,9 +16,6 @@ use Zend\Code\Reflection\ClassReflection; /** - * @category Zend - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/AuthorTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/AuthorTagTest.php index 96a5e878a26..7d33012da65 100644 --- a/tests/ZendTest/Code/Generator/DocBlock/Tag/AuthorTagTest.php +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/AuthorTagTest.php @@ -5,18 +5,15 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator\DocBlock\Tag; use Zend\Code\Generator\DocBlock\Tag\AuthorTag; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlockReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ @@ -27,46 +24,63 @@ class AuthorTagTest extends \PHPUnit_Framework_TestCase */ protected $tag; + /** + * @var TagManager + */ + protected $tagmanager; + public function setUp() { $this->tag = new AuthorTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); } public function tearDown() { $this->tag = null; + $this->tagmanager = null; } - public function testDatatypeGetterAndSetterPersistValue() + public function testGetterAndSetterPersistValue() { - $this->tag->setDatatype('Foo'); - $this->assertEquals('Foo', $this->tag->getDatatype()); + $this->tag->setAuthorName('Foo'); + $this->tag->setAuthorEmail('Bar'); + $this->assertEquals('Foo', $this->tag->getAuthorName()); + $this->assertEquals('Bar', $this->tag->getAuthorEmail()); } - public function testParamNameGetterAndSetterPersistValue() + public function testParamProducesCorrectDocBlockLine() { - $this->tag->setParamName('Foo'); - $this->assertEquals('Foo', $this->tag->getParamName()); + $this->tag->setAuthorName('foo'); + $this->tag->setAuthorEmail('string'); + $this->assertEquals('@author foo ', $this->tag->generate()); } - public function testParamProducesCorrectDocBlockLine() + public function testNameIsCorrect() { - $this->tag->setParamName('foo'); - $this->tag->setDatatype('string'); - $this->tag->setDescription('bar bar bar'); - $this->assertEquals('@param string $foo bar bar bar', $this->tag->generate()); + $this->assertEquals('author', $this->tag->getName()); } public function testConstructorWithOptions() { $this->tag->setOptions(array( - 'datatype' => 'string', - 'paramName' => 'foo', - )); - $tagWithOptionsFromConstructor = new AuthorTag(array( - 'datatype' => 'string', - 'paramName' => 'foo', + 'authorEmail' => 'string', + 'authorName' => 'foo', )); + $tagWithOptionsFromConstructor = new AuthorTag('foo', 'string'); $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @author Mister Miller '); + $reflectionTag = $docreflection->getTag('author'); + + /** @var AuthorTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\AuthorTag', $tag); + $this->assertEquals('Mister Miller', $tag->getAuthorName()); + $this->assertEquals('mister.miller@zend.com', $tag->getAuthorEmail()); + } } diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/GenericTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/GenericTagTest.php new file mode 100644 index 00000000000..27b69f03b96 --- /dev/null +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/GenericTagTest.php @@ -0,0 +1,80 @@ +tag = new GenericTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); + } + + public function tearDown() + { + $this->tag = null; + $this->tagmanager = null; + } + + public function testGetterAndSetterPersistValue() + { + $this->tag->setName('var'); + $this->tag->setContent('string'); + $this->assertEquals('var', $this->tag->getName()); + $this->assertEquals('string', $this->tag->getContent()); + } + + public function testParamProducesCorrectDocBlockLine() + { + $this->tag->setName('var'); + $this->tag->setContent('string'); + $this->assertEquals('@var string', $this->tag->generate()); + } + + public function testConstructorWithOptions() + { + $this->tag->setOptions(array( + 'name' => 'var', + 'content' => 'string', + )); + $tagWithOptionsFromConstructor = new GenericTag('var', 'string'); + $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); + } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @var string'); + $reflectionTag = $docreflection->getTag('var'); + + /** @var GenericTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\GenericTag', $tag); + $this->assertEquals('var', $tag->getName()); + $this->assertEquals('string', $tag->getContent()); + } +} diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/LicenseTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/LicenseTagTest.php index 6cf36675741..1a810b01dc5 100644 --- a/tests/ZendTest/Code/Generator/DocBlock/Tag/LicenseTagTest.php +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/LicenseTagTest.php @@ -5,18 +5,15 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator\DocBlock\Tag; use Zend\Code\Generator\DocBlock\Tag\LicenseTag; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlockReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ @@ -26,18 +23,25 @@ class LicenseTagTest extends \PHPUnit_Framework_TestCase * @var LicenseTag */ protected $tag; + /** + * @var TagManager + */ + protected $tagmanager; public function setUp() { $this->tag = new LicenseTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); } public function tearDown() { $this->tag = null; + $this->tagmanager = null; } - public function testUrlGetterAndSetterPersistValue() + public function testGetterAndSetterPersistValue() { $this->tag->setUrl('foo'); $this->tag->setLicenseName('bar'); @@ -46,6 +50,11 @@ public function testUrlGetterAndSetterPersistValue() $this->assertEquals('bar', $this->tag->getLicenseName()); } + public function testNameIsCorrect() + { + $this->assertEquals('license', $this->tag->getName()); + } + public function testLicenseProducesCorrectDocBlockLine() { $this->tag->setUrl('foo'); @@ -59,10 +68,19 @@ public function testConstructorWithOptions() 'url' => 'foo', 'licenseName' => 'bar', )); - $tagWithOptionsFromConstructor = new LicenseTag(array( - 'url' => 'foo', - 'licenseName' => 'bar', - )); + $tagWithOptionsFromConstructor = new LicenseTag('foo', 'bar'); $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @license http://zend.com License'); + $reflectionTag = $docreflection->getTag('license'); + + /** @var LicenseTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\LicenseTag', $tag); + $this->assertEquals('http://zend.com', $tag->getUrl()); + $this->assertEquals('License', $tag->getLicenseName()); + } } diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/MethodTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/MethodTagTest.php new file mode 100644 index 00000000000..281dce4a905 --- /dev/null +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/MethodTagTest.php @@ -0,0 +1,98 @@ +tag = new MethodTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); + } + + public function tearDown() + { + $this->tag = null; + $this->tagmanager = null; + } + + public function testGetterAndSetterPersistValue() + { + $this->tag->setIsStatic(true); + $this->tag->setMethodName('method'); + $this->assertEquals(true, $this->tag->isStatic()); + $this->assertEquals('method', $this->tag->getMethodName()); + } + + public function testGetterForMethodNameTrimsCorrectly() + { + $this->tag->setMethodName('()method()'); + $this->assertEquals('()method', $this->tag->getMethodName()); + } + + public function testNameIsCorrect() + { + $this->assertEquals('method', $this->tag->getName()); + } + + public function testParamProducesCorrectDocBlockLine() + { + $this->tag->setIsStatic(true); + $this->tag->setMethodName('method'); + $this->tag->setTypes('int'); + $this->tag->setDescription('method(string $a)'); + $this->assertEquals('@method static int method() method(string $a)', $this->tag->generate()); + } + + public function testConstructorWithOptions() + { + $this->tag->setOptions(array( + 'isStatic' => true, + 'methodName' => 'method', + 'types' => array('string'), + 'description' => 'description' + )); + $tagWithOptionsFromConstructor = new MethodTag('method', array('string'), 'description', true); + $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); + } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @method static int method() method(int $a)'); + $reflectionTag = $docreflection->getTag('method'); + + /** @var MethodTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\MethodTag', $tag); + $this->assertEquals(true, $tag->isStatic()); + $this->assertEquals('int', $tag->getTypesAsString()); + $this->assertEquals('method', $tag->getMethodName()); + $this->assertEquals('method(int $a)', $tag->getDescription()); + } +} diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/ParamTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/ParamTagTest.php index 2fee8c1c875..19fe920ea6b 100644 --- a/tests/ZendTest/Code/Generator/DocBlock/Tag/ParamTagTest.php +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/ParamTagTest.php @@ -5,18 +5,15 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator\DocBlock\Tag; use Zend\Code\Generator\DocBlock\Tag\ParamTag; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlockReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ @@ -26,47 +23,70 @@ class ParamTagTest extends \PHPUnit_Framework_TestCase * @var ParamTag */ protected $tag; + /** + * @var TagManager + */ + protected $tagmanager; public function setUp() { $this->tag = new ParamTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); } public function tearDown() { $this->tag = null; + $this->tagmanager = null; + } + + public function testGetterAndSetterPersistValue() + { + $this->tag->setVariableName('Foo'); + $this->assertEquals('Foo', $this->tag->getVariableName()); } - public function testDatatypeGetterAndSetterPersistValue() + public function testGetterForVariableNameTrimsCorrectly() { - $this->tag->setDatatype('Foo'); - $this->assertEquals('Foo', $this->tag->getDatatype()); + $this->tag->setVariableName('$param$'); + $this->assertEquals('param$', $this->tag->getVariableName()); } - public function testParamNameGetterAndSetterPersistValue() + public function testNameIsCorrect() { - $this->tag->setParamName('Foo'); - $this->assertEquals('Foo', $this->tag->getParamName()); + $this->assertEquals('param', $this->tag->getName()); } public function testParamProducesCorrectDocBlockLine() { - $this->tag->setParamName('foo'); - $this->tag->setDatatype('string'); - $this->tag->setDescription('bar bar bar'); - $this->assertEquals('@param string $foo bar bar bar', $this->tag->generate()); + $this->tag->setVariableName('foo'); + $this->tag->setTypes('string|null'); + $this->tag->setDescription('description'); + $this->assertEquals('@param string|null $foo description', $this->tag->generate()); } public function testConstructorWithOptions() { $this->tag->setOptions(array( - 'datatype' => 'string', - 'paramName' => 'foo', - )); - $tagWithOptionsFromConstructor = new ParamTag(array( - 'datatype' => 'string', - 'paramName' => 'foo', + 'variableName' => 'foo', + 'types' => array('string'), + 'description' => 'description' )); + $tagWithOptionsFromConstructor = new ParamTag('foo', array('string'), 'description'); $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @param int $foo description'); + $reflectionTag = $docreflection->getTag('param'); + + /** @var ParamTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\ParamTag', $tag); + $this->assertEquals('foo', $tag->getVariableName()); + $this->assertEquals('description', $tag->getDescription()); + $this->assertEquals('int', $tag->getTypesAsString()); + } } diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/PropertyTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/PropertyTagTest.php new file mode 100644 index 00000000000..f50eb52ac42 --- /dev/null +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/PropertyTagTest.php @@ -0,0 +1,93 @@ +tag = new PropertyTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); + } + + public function tearDown() + { + $this->tag = null; + $this->tagmanager = null; + } + + public function testGetterAndSetterPersistValue() + { + $this->tag->setPropertyName('property'); + $this->assertEquals('property', $this->tag->getPropertyName()); + } + + + public function testGetterForVariableNameTrimsCorrectly() + { + $this->tag->setPropertyName('$property$'); + $this->assertEquals('property$', $this->tag->getPropertyName()); + } + + public function testNameIsCorrect() + { + $this->assertEquals('property', $this->tag->getName()); + } + + public function testParamProducesCorrectDocBlockLine() + { + $this->tag->setPropertyName('property'); + $this->tag->setTypes('string[]'); + $this->tag->setDescription('description'); + $this->assertEquals('@property string[] $property description', $this->tag->generate()); + } + + public function testConstructorWithOptions() + { + $this->tag->setOptions(array( + 'propertyName' => 'property', + 'types' => array('string'), + 'description' => 'description' + )); + $tagWithOptionsFromConstructor = new PropertyTag('property', array('string'), 'description'); + $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); + } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @property int $foo description'); + $reflectionTag = $docreflection->getTag('property'); + + /** @var PropertyTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\PropertyTag', $tag); + $this->assertEquals('foo', $tag->getPropertyName()); + $this->assertEquals('description', $tag->getDescription()); + $this->assertEquals('int', $tag->getTypesAsString()); + } +} diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/ReturnTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/ReturnTagTest.php index 9e1d662bcdf..81c2550dce5 100644 --- a/tests/ZendTest/Code/Generator/DocBlock/Tag/ReturnTagTest.php +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/ReturnTagTest.php @@ -5,18 +5,15 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator\DocBlock\Tag; use Zend\Code\Generator\DocBlock\Tag\ReturnTag; +use Zend\Code\Generator\DocBlock\TagManager; +use Zend\Code\Reflection\DocBlockReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ @@ -26,38 +23,45 @@ class ReturnTagTest extends \PHPUnit_Framework_TestCase * @var ReturnTag */ protected $tag; + /** + * @var TagManager + */ + protected $tagmanager; public function setUp() { $this->tag = new ReturnTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); } public function tearDown() { $this->tag = null; + $this->tagmanager = null; } - public function testDatatypeGetterAndSetterPersistValue() + public function testNameIsCorrect() { - $this->tag->setDatatype('Foo'); - $this->assertEquals('Foo', $this->tag->getDatatype()); + $this->assertEquals('return', $this->tag->getName()); } public function testReturnProducesCorrectDocBlockLine() { - $this->tag->setDatatype('string'); + $this->tag->setTypes('string|int'); $this->tag->setDescription('bar bar bar'); - $this->assertEquals('@return string bar bar bar', $this->tag->generate()); + $this->assertEquals('@return string|int bar bar bar', $this->tag->generate()); } - public function testConstructorWithOptions() + public function testCreatingTagFromReflection() { - $this->tag->setOptions(array( - 'datatype' => 'string|null', - )); - $tagWithOptionsFromConstructor = new ReturnTag(array( - 'datatype' => 'string|null', - )); - $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); + $docreflection = new DocBlockReflection('/** @return int The return'); + $reflectionTag = $docreflection->getTag('return'); + + /** @var ReturnTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\ReturnTag', $tag); + $this->assertEquals('The return', $tag->getDescription()); + $this->assertEquals('int', $tag->getTypesAsString()); } } diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/ThrowsTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/ThrowsTagTest.php new file mode 100644 index 00000000000..8c25fb564fe --- /dev/null +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/ThrowsTagTest.php @@ -0,0 +1,67 @@ +tag = new ThrowsTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); + } + + public function tearDown() + { + $this->tag = null; + $this->tagmanager = null; + } + + public function testNameIsCorrect() + { + $this->assertEquals('throws', $this->tag->getName()); + } + + public function testParamProducesCorrectDocBlockLine() + { + $this->tag->setTypes('Exception\\MyException'); + $this->tag->setDescription('description'); + $this->assertEquals('@throws Exception\\MyException description', $this->tag->generate()); + } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @throws Exception\Invalid description'); + $reflectionTag = $docreflection->getTag('throws'); + + /** @var ThrowsTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\ThrowsTag', $tag); + $this->assertEquals('description', $tag->getDescription()); + $this->assertEquals('Exception\Invalid', $tag->getTypesAsString()); + } +} diff --git a/tests/ZendTest/Code/Generator/DocBlock/Tag/TypableTagTest.php b/tests/ZendTest/Code/Generator/DocBlock/Tag/TypableTagTest.php new file mode 100644 index 00000000000..1ca3155e374 --- /dev/null +++ b/tests/ZendTest/Code/Generator/DocBlock/Tag/TypableTagTest.php @@ -0,0 +1,76 @@ +tag = new TypeableTag(); + } + + public function tearDown() + { + $this->tag = null; + } + + public function testGetterAndSetterPersistValue() + { + $this->tag->setTypes(array('string', 'null')); + $this->tag->setDescription('Description'); + $this->assertEquals(array('string', 'null'), $this->tag->getTypes()); + $this->assertEquals('Description', $this->tag->getDescription()); + } + + public function testGetterForTypesAsStringWithSingleType() + { + $this->tag->setTypes(array('string')); + $this->assertEquals('string', $this->tag->getTypesAsString()); + } + + public function testGetterForTypesAsStringWithSingleTypeAndDelimiter() + { + $this->tag->setTypes(array('string')); + $this->assertEquals('string', $this->tag->getTypesAsString('/')); + } + + public function testGetterForTypesAsStringWithMultipleTypes() + { + $this->tag->setTypes(array('string', 'null')); + $this->assertEquals('string|null', $this->tag->getTypesAsString()); + } + + public function testGetterForTypesAsStringWithMultipleTypesAndDelimiter() + { + $this->tag->setTypes(array('string', 'null')); + $this->assertEquals('string/null', $this->tag->getTypesAsString('/')); + } + + public function testConstructorWithOptions() + { + $this->tag->setOptions(array( + 'types' => array('string', 'null'), + 'description' => 'description', + )); + $tagWithOptionsFromConstructor = new TypeableTag(array('string', 'null'), 'description'); + $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); + } +} diff --git a/tests/ZendTest/Code/Generator/DocBlock/TagTest.php b/tests/ZendTest/Code/Generator/DocBlock/TagTest.php deleted file mode 100644 index 38e89f33d3a..00000000000 --- a/tests/ZendTest/Code/Generator/DocBlock/TagTest.php +++ /dev/null @@ -1,83 +0,0 @@ -tag = new Tag(); - } - - public function tearDown() - { - $this->tag = null; - } - - public function testCanPassNameToConstructor() - { - $tag = new Tag(array('name' => 'Foo')); - $this->assertEquals('Foo', $tag->getName()); - } - - public function testCanPassDescriptionToConstructor() - { - $tag = new Tag(array('description' => 'Foo')); - $this->assertEquals('Foo', $tag->getDescription()); - } - - public function testCanGenerateLicenseTag() - { - $tag = new LicenseTag(array( - 'url' => 'http://test.license.com', - 'description' => 'Test License', - )); - $this->assertEquals( - '@license http://test.license.com Test License', - $tag->generate() - ); - } - - public function testNameGetterAndSetterPersistValue() - { - $this->tag->setName('Foo'); - $this->assertEquals('Foo', $this->tag->getName()); - } - - public function testDescriptionGetterAndSetterPersistValue() - { - $this->tag->setDescription('Foo foo foo'); - $this->assertEquals('Foo foo foo', $this->tag->getDescription()); - } - - public function testParamProducesCorrectDocBlockTag() - { - $this->tag->setName('foo'); - $this->tag->setDescription('bar bar bar'); - $this->assertEquals('@foo bar bar bar', $this->tag->generate()); - } -} diff --git a/tests/ZendTest/Code/Generator/DocBlockGeneratorTest.php b/tests/ZendTest/Code/Generator/DocBlockGeneratorTest.php index 43108858a25..682426da027 100644 --- a/tests/ZendTest/Code/Generator/DocBlockGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/DocBlockGeneratorTest.php @@ -5,19 +5,15 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; use Zend\Code\Generator\DocBlockGenerator; use Zend\Code\Generator\DocBlock\Tag; +use Zend\Code\Reflection\DocBlockReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ @@ -28,9 +24,26 @@ class DocBlockGeneratorTest extends \PHPUnit_Framework_TestCase */ protected $docBlockGenerator; + /** + * @var DocBlockGenerator + */ + protected $reflectionDocBlockGenerator; + protected function setUp() { $this->docBlockGenerator = $this->docBlockGenerator = new DocBlockGenerator(); + $reflectionDocBlock = new DocBlockReflection( + '/** + * Short Description + * Long Description + * @param string $foo comment + * @author Zend + * @license http://license The License + * @return int + */' + ); + + $this->reflectionDocBlockGenerator = DocBlockGenerator::fromReflection($reflectionDocBlock); } public function testCanPassTagsToConstructor() @@ -138,4 +151,55 @@ public function testGenerateWithWordWrapDisabled() . ' 80 characters'. DocBlockGenerator::LINE_FEED . ' */' . DocBlockGenerator::LINE_FEED; $this->assertEquals($expected, $this->docBlockGenerator->generate()); } + + public function testDocBlockFromRefelectionLongDescription() + { + $this->assertEquals('Long Description', $this->reflectionDocBlockGenerator->getLongDescription()); + } + + public function testDocBlockFromRefelectionShortDescription() + { + $this->assertEquals('Short Description', $this->reflectionDocBlockGenerator->getShortDescription()); + } + + public function testDocBlockFromRefelectionTagsCount() + { + $this->assertCount(4, $this->reflectionDocBlockGenerator->getTags()); + } + + /** + * @depends testDocBlockFromRefelectionTagsCount + */ + public function testDocBlockFromRefelectionParamTag() + { + $tags = $this->reflectionDocBlockGenerator->getTags(); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\ParamTag', $tags[0]); + } + + /** + * @depends testDocBlockFromRefelectionTagsCount + */ + public function testDocBlockFromRefelectionAuthorTag() + { + $tags = $this->reflectionDocBlockGenerator->getTags(); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\AuthorTag', $tags[1]); + } + + /** + * @depends testDocBlockFromRefelectionTagsCount + */ + public function testDocBlockFromRefelectionLicenseTag() + { + $tags = $this->reflectionDocBlockGenerator->getTags(); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\LicenseTag', $tags[2]); + } + + /** + * @depends testDocBlockFromRefelectionTagsCount + */ + public function testDocBlockFromRefelectionReturnTag() + { + $tags = $this->reflectionDocBlockGenerator->getTags(); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\ReturnTag', $tags[3]); + } } diff --git a/tests/ZendTest/Code/Generator/FileGeneratorTest.php b/tests/ZendTest/Code/Generator/FileGeneratorTest.php index 50cdb4266bb..2bc4b67692d 100644 --- a/tests/ZendTest/Code/Generator/FileGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/FileGeneratorTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; @@ -16,12 +15,6 @@ use Zend\Code\Reflection\FileReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) - * @license http://framework.zend.com/license/new-bsd New BSD License - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php * @group Zend_Code_Generator_Php_File diff --git a/tests/ZendTest/Code/Generator/MethodGeneratorTest.php b/tests/ZendTest/Code/Generator/MethodGeneratorTest.php index 5180444e844..4a4cb1e3560 100644 --- a/tests/ZendTest/Code/Generator/MethodGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/MethodGeneratorTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; @@ -16,10 +15,6 @@ use Zend\Code\Reflection\MethodReflection; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ diff --git a/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php b/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php index 8a4bdf59763..15eb609a4a3 100644 --- a/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php @@ -14,9 +14,6 @@ use Zend\Code\Reflection\ParameterReflection; /** - * @category Zend - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ diff --git a/tests/ZendTest/Code/Generator/PropertyGeneratorTest.php b/tests/ZendTest/Code/Generator/PropertyGeneratorTest.php index ab296245431..bd688ce16d3 100644 --- a/tests/ZendTest/Code/Generator/PropertyGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/PropertyGeneratorTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; @@ -14,10 +13,6 @@ use Zend\Code\Generator\PropertyValueGenerator; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ @@ -262,7 +257,7 @@ public function testPropertyDocBlockWillLoadFromReflection() $this->assertInternalType('array', $tags); $this->assertEquals(1, count($tags)); $tag = array_shift($tags); - $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag', $tag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\GenericTag', $tag); $this->assertEquals('var', $tag->getName()); } diff --git a/tests/ZendTest/Code/Generator/PropertyValueGeneratorTest.php b/tests/ZendTest/Code/Generator/PropertyValueGeneratorTest.php index 6c9192b61db..628a63d3c73 100644 --- a/tests/ZendTest/Code/Generator/PropertyValueGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/PropertyValueGeneratorTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; @@ -13,10 +12,6 @@ use Zend\Code\Generator\PropertyValueGenerator; /** - * @category Zend - * @package Zend_Code_Generator - * @subpackage UnitTests - * * @group Zend_Code_Generator * @group Zend_Code_Generator_Php */ diff --git a/tests/ZendTest/Code/Generator/TestAsset/PrototypeClass.php b/tests/ZendTest/Code/Generator/TestAsset/PrototypeClass.php new file mode 100644 index 00000000000..7de2227ac63 --- /dev/null +++ b/tests/ZendTest/Code/Generator/TestAsset/PrototypeClass.php @@ -0,0 +1,24 @@ +prototypeFactory = new PrototypeClassFactory(); + } + + public function tearDown() + { + $this->prototypeFactory = null; + } + + public function testAddAndGetPrototype() + { + $proto = new PrototypeClass(); + $this->prototypeFactory->addPrototype($proto); + $this->assertNotSame($proto, $this->prototypeFactory->getClonedPrototype($proto->getName())); + $this->assertEquals($proto, $this->prototypeFactory->getClonedPrototype($proto->getName())); + } + + public function testFallBackToGeneric() + { + $proto = new PrototypeGenericClass(); + $this->prototypeFactory->setGenericPrototype($proto); + $this->assertNotSame($proto, $this->prototypeFactory->getClonedPrototype('notexist')); + $this->assertEquals($proto, $this->prototypeFactory->getClonedPrototype('notexist')); + } + + public function testSetNameOnGenericIsCalledOnce() + { + $mockProto = $this->getMock('ZendTest\Code\Generator\TestAsset\PrototypeGenericClass', array('setName')); + $mockProto->expects($this->once())->method('setName')->will($this->returnValue('notexist')); + $this->prototypeFactory->setGenericPrototype($mockProto); + $this->prototypeFactory->getClonedPrototype('notexist'); + } +}