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

Add @var tag support in dockblocks #41

Closed
wants to merge 1 commit 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
69 changes: 69 additions & 0 deletions src/Generator/DocBlock/Tag/VarTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Code\Generator\DocBlock\Tag;

class VarTag extends AbstractTypeableTag implements TagInterface
{
/**
* @var string|null
*/
protected $variableName;

/**
* @param string $variableName
* @param string|string[] $types
* @param string $description
*/
public function __construct($variableName = null, $types = [], $description = null)
{
if (!empty($variableName)) {
$this->setVariableName($variableName);
}

parent::__construct($types, $description);
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'var';
}

/**
* @param string $variableName
* @return self
*/
public function setVariableName($variableName)
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this setter, unless strictly required. Making it private is also OK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Variable name is allowed to be null in constructor, so there should be method to set it later. Other tags like @param or @author are working same way.

{
$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 : '');
}
}
1 change: 1 addition & 0 deletions src/Generator/DocBlock/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
87 changes: 87 additions & 0 deletions src/Reflection/DocBlock/Tag/VarTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Code\Reflection\DocBlock\Tag;

class VarTag implements TagInterface, PhpDocTypedTagInterface
{
/**
* @var array
*/
protected $types = [];

/**
* @var string
*/
protected $variableName = null;

/**
* @var string
*/
protected $description = null;

/**
* @return string
*/
public function getName()
{
return 'var';
}

/**
* Initializer
*
* @param string $tagDocblockLine
*/
public function initialize($tagDocblockLine)
{
$match = [];
if (!preg_match('#^(.+)?(\$[\S]+)\s*(.*)$#m', $tagDocblockLine, $match)) {
return;
}

if ($match[1] !== '') {
$this->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;
}
}
1 change: 1 addition & 0 deletions src/Reflection/DocBlock/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
6 changes: 3 additions & 3 deletions test/Generator/DocBlock/Tag/GenericTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
93 changes: 93 additions & 0 deletions test/Generator/DocBlock/Tag/VarTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Code\Generator\DocBlock\Tag;

use Zend\Code\Generator\DocBlock\Tag\VarTag;
use Zend\Code\Generator\DocBlock\TagManager;
use Zend\Code\Reflection\DocBlockReflection;

/**
* @group Zend_Code_Generator
* @group Zend_Code_Generator_Php
*/
class VarTagTest extends \PHPUnit_Framework_TestCase
{
/**
* @var VarTag
*/
protected $tag;
/**
* @var TagManager
*/
protected $tagmanager;

public function setUp()
{
$this->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());
}
}
7 changes: 3 additions & 4 deletions test/Generator/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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());
}

Expand All @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions test/Reflection/DocBlock/Tag/VarTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Code\Reflection\DocBlock\Tag;

use Zend\Code\Reflection\DocBlock\Tag\VarTag;

/**
* @group Zend_Reflection
* @group Zend_Reflection_DocBlock
*/
class VarTagTest extends \PHPUnit_Framework_TestCase
{
public function testParseName()
{
$tag = new VarTag();
$tag->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());
}
}