diff --git a/src/Generator/DocBlock/Tag/VarTag.php b/src/Generator/DocBlock/Tag/VarTag.php new file mode 100644 index 00000000..98a03422 --- /dev/null +++ b/src/Generator/DocBlock/Tag/VarTag.php @@ -0,0 +1,69 @@ +setVariableName($variableName); + } + + parent::__construct($types, $description); + } + + /** + * {@inheritDoc} + */ + public function getName() + { + return 'var'; + } + + /** + * @param string $variableName + * @return self + */ + public function setVariableName($variableName) + { + $this->variableName = ltrim($variableName, '$'); + return $this; + } + + /** + * @return string|null + */ + public function getVariableName() + { + return $this->variableName; + } + + /** + * {@inheritDoc} + */ + public function generate() + { + return '@var' + . ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '') + . ((!empty($this->variableName)) ? ' $' . $this->variableName : '') + . ((!empty($this->description)) ? ' ' . $this->description : ''); + } +} diff --git a/src/Generator/DocBlock/TagManager.php b/src/Generator/DocBlock/TagManager.php index b8826669..cef4fcbb 100644 --- a/src/Generator/DocBlock/TagManager.php +++ b/src/Generator/DocBlock/TagManager.php @@ -35,6 +35,7 @@ public function initializeDefaultTags() $this->addPrototype(new Tag\AuthorTag()); $this->addPrototype(new Tag\LicenseTag()); $this->addPrototype(new Tag\ThrowsTag()); + $this->addPrototype(new Tag\VarTag()); $this->setGenericPrototype(new Tag\GenericTag()); } diff --git a/src/Reflection/DocBlock/Tag/VarTag.php b/src/Reflection/DocBlock/Tag/VarTag.php new file mode 100644 index 00000000..a3fd4104 --- /dev/null +++ b/src/Reflection/DocBlock/Tag/VarTag.php @@ -0,0 +1,87 @@ +types = explode('|', rtrim($match[1])); + } + + if ($match[2] !== '') { + $this->variableName = $match[2]; + } + + if ($match[3] !== '') { + $this->description = $match[3]; + } + } + + public function getTypes() + { + return $this->types; + } + + /** + * @return null|string + */ + public function getVariableName() + { + return $this->variableName; + } + + /** + * @return null|string + */ + public function getDescription() + { + return $this->description; + } + + public function __toString() + { + return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . PHP_EOL; + } +} diff --git a/src/Reflection/DocBlock/TagManager.php b/src/Reflection/DocBlock/TagManager.php index 4e07760e..f0b3ffeb 100644 --- a/src/Reflection/DocBlock/TagManager.php +++ b/src/Reflection/DocBlock/TagManager.php @@ -26,6 +26,7 @@ public function initializeDefaultTags() $this->addPrototype(new Tag\AuthorTag()); $this->addPrototype(new Tag\LicenseTag()); $this->addPrototype(new Tag\ThrowsTag()); + $this->addPrototype(new Tag\VarTag()); $this->setGenericPrototype(new Tag\GenericTag()); } diff --git a/test/Generator/DocBlock/Tag/GenericTagTest.php b/test/Generator/DocBlock/Tag/GenericTagTest.php index b0dd6fb0..fa5c4d62 100644 --- a/test/Generator/DocBlock/Tag/GenericTagTest.php +++ b/test/Generator/DocBlock/Tag/GenericTagTest.php @@ -68,13 +68,13 @@ public function testConstructorWithOptions() public function testCreatingTagFromReflection() { - $docreflection = new DocBlockReflection('/** @var string'); - $reflectionTag = $docreflection->getTag('var'); + $docreflection = new DocBlockReflection('/** @global string'); + $reflectionTag = $docreflection->getTag('global'); /** @var GenericTag $tag */ $tag = $this->tagmanager->createTagFromReflection($reflectionTag); $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\GenericTag', $tag); - $this->assertEquals('var', $tag->getName()); + $this->assertEquals('global', $tag->getName()); $this->assertEquals('string', $tag->getContent()); } } diff --git a/test/Generator/DocBlock/Tag/VarTagTest.php b/test/Generator/DocBlock/Tag/VarTagTest.php new file mode 100644 index 00000000..45c74e70 --- /dev/null +++ b/test/Generator/DocBlock/Tag/VarTagTest.php @@ -0,0 +1,93 @@ +tag = new VarTag(); + $this->tagmanager = new TagManager(); + $this->tagmanager->initializeDefaultTags(); + } + + public function tearDown() + { + $this->tag = null; + $this->tagmanager = null; + } + + public function testGetterAndSetterPersistValue() + { + $this->tag->setVariableName('variable'); + $this->assertEquals('variable', $this->tag->getVariableName()); + } + + + public function testGetterForVariableNameTrimsCorrectly() + { + $this->tag->setVariableName('$variable$'); + $this->assertEquals('variable$', $this->tag->getVariableName()); + } + + public function testNameIsCorrect() + { + $this->assertEquals('var', $this->tag->getName()); + } + + public function testParamProducesCorrectDocBlockLine() + { + $this->tag->setVariableName('variable'); + $this->tag->setTypes('string[]'); + $this->tag->setDescription('description'); + $this->assertEquals('@var string[] $variable description', $this->tag->generate()); + } + + public function testConstructorWithOptions() + { + $this->tag->setOptions([ + 'variableName' => 'foo', + 'types' => ['string'], + 'description' => 'description' + ]); + $tagWithOptionsFromConstructor = new VarTag('foo', ['string'], 'description'); + $this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate()); + } + + public function testCreatingTagFromReflection() + { + $docreflection = new DocBlockReflection('/** @var int $foo description'); + $reflectionTag = $docreflection->getTag('var'); + + /** @var VarTag $tag */ + $tag = $this->tagmanager->createTagFromReflection($reflectionTag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\VarTag', $tag); + $this->assertEquals('foo', $tag->getVariableName()); + $this->assertEquals('description', $tag->getDescription()); + $this->assertEquals('int', $tag->getTypesAsString()); + } +} diff --git a/test/Generator/PropertyGeneratorTest.php b/test/Generator/PropertyGeneratorTest.php index 06c9b3d8..f9464ef3 100644 --- a/test/Generator/PropertyGeneratorTest.php +++ b/test/Generator/PropertyGeneratorTest.php @@ -22,7 +22,7 @@ class PropertyGeneratorTest extends \PHPUnit_Framework_TestCase public function testPropertyConstructor() { $codeGenProperty = new PropertyGenerator(); - $this->isInstanceOf($codeGenProperty, 'Zend\Code\Generator\PropertyGenerator'); + $this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', $codeGenProperty); } /** @@ -257,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\GenericTag', $tag); + $this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\VarTag', $tag); $this->assertEquals('var', $tag->getName()); } @@ -266,9 +266,8 @@ public function testPropertyDocBlockWillLoadFromReflection() * @dataProvider dataSetTypeSetValueGenerate * @param string $type * @param mixed $value - * @param string $code */ - public function testSetDefaultValue($type, $value, $code) + public function testSetDefaultValue($type, $value) { $property = new PropertyGenerator(); $property->setDefaultValue($value, $type); diff --git a/test/Reflection/DocBlock/Tag/VarTagTest.php b/test/Reflection/DocBlock/Tag/VarTagTest.php new file mode 100644 index 00000000..402312e7 --- /dev/null +++ b/test/Reflection/DocBlock/Tag/VarTagTest.php @@ -0,0 +1,53 @@ +initialize('$test'); + $this->assertEquals('var', $tag->getName()); + $this->assertEquals('$test', $tag->getVariableName()); + $this->assertNull($tag->getDescription()); + } + + public function testParseTypeAndName() + { + $tag = new VarTag(); + $tag->initialize('string|null $test'); + $this->assertEquals('$test', $tag->getVariableName()); + $this->assertNull($tag->getDescription()); + $this->assertEquals(['string', 'null'], $tag->getTypes()); + } + + public function testParseNameAndDescription() + { + $tag = new VarTag(); + $tag->initialize('$test I\'m test property'); + $this->assertEquals('$test', $tag->getVariableName()); + $this->assertEquals('I\'m test property', $tag->getDescription()); + } + + public function testParseTypeAndNameAndDescription() + { + $tag = new VarTag(); + $tag->initialize('string $test I\'m test variable'); + $this->assertEquals('$test', $tag->getVariableName()); + $this->assertEquals('I\'m test variable', $tag->getDescription()); + } +}