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

Commit

Permalink
#41 cleaning up VarTag additions, marking the setter as @internal
Browse files Browse the repository at this point in the history
… to prevent misuse, as it is needed for the `TagManager` to work correctly
  • Loading branch information
Ocramius committed Jul 23, 2017
1 parent e1f0d02 commit 3e4db4a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
28 changes: 15 additions & 13 deletions src/Generator/DocBlock/Tag/VarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class VarTag extends AbstractTypeableTag implements TagInterface
/**
* @var string|null
*/
protected $variableName;
private $variableName;

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

parent::__construct($types, $description);
Expand All @@ -39,13 +39,15 @@ public function getName()
}

/**
* @param string $variableName
* @return self
* @internal this code is only public for compatibility with the
* @see \Zend\Code\Generator\DocBlock\TagManager, which
* uses setters
*/
public function setVariableName($variableName)
public function setVariableName(?string $variableName) : void
{
$this->variableName = ltrim($variableName, '$');
return $this;
if (null !== $variableName) {
$this->variableName = ltrim($variableName, '$');
}
}

/**
Expand All @@ -59,11 +61,11 @@ public function getVariableName()
/**
* {@inheritDoc}
*/
public function generate()
public function generate() : string
{
return '@var'
. ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '')
. ((!empty($this->variableName)) ? ' $' . $this->variableName : '')
. (null !== $this->variableName ? ' $' . $this->variableName : '')
. ((!empty($this->description)) ? ' ' . $this->description : '');
}
}
60 changes: 31 additions & 29 deletions test/Generator/DocBlock/Tag/VarTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,85 @@

namespace ZendTest\Code\Generator\DocBlock\Tag;

use PHPUnit\Framework\TestCase;
use Zend\Code\Generator\DocBlock\Tag\VarTag;
use Zend\Code\Generator\DocBlock\TagManager;
use Zend\Code\Reflection\DocBlock\Tag\VarTag as ReflectionVarTag;
use Zend\Code\Reflection\DocBlockReflection;

/**
* @group Zend_Code_Generator
* @group Zend_Code_Generator_Php
* @covers \Zend\Code\Generator\DocBlock\Tag\VarTag
*/
class VarTagTest extends \PHPUnit_Framework_TestCase
class VarTagTest extends TestCase
{
/**
* @var VarTag
*/
protected $tag;
private $tag;

/**
* @var TagManager
*/
protected $tagmanager;
private $tagManager;

public function setUp()
protected function setUp() : void
{
$this->tag = new VarTag();
$this->tagmanager = new TagManager();
$this->tagmanager->initializeDefaultTags();
}
parent::setUp();

public function tearDown()
{
$this->tag = null;
$this->tagmanager = null;
$this->tag = new VarTag();
$this->tagManager = new TagManager();

$this->tagManager->initializeDefaultTags();
}

public function testGetterAndSetterPersistValue()
public function testGetterAndSetterPersistValue() : void
{
$this->tag->setVariableName('variable');
$this->assertEquals('variable', $this->tag->getVariableName());
}
$tag = new VarTag('variable');

self::assertSame('variable', $tag->getVariableName());
}

public function testGetterForVariableNameTrimsCorrectly()
public function testGetterForVariableNameTrimsCorrectly() : void
{
$this->tag->setVariableName('$variable$');
$this->assertEquals('variable$', $this->tag->getVariableName());
}

public function testNameIsCorrect()
public function testNameIsCorrect() : void
{
$this->assertEquals('var', $this->tag->getName());
}

public function testParamProducesCorrectDocBlockLine()
public function testParamProducesCorrectDocBlockLine() : void
{
$this->tag->setVariableName('variable');
$this->tag->setTypes('string[]');
$this->tag->setDescription('description');
$this->assertEquals('@var string[] $variable description', $this->tag->generate());
}

public function testConstructorWithOptions()
public function testConstructorWithOptions() : void
{
$this->tag->setOptions([
'variableName' => 'foo',
'types' => ['string'],
'description' => 'description'
'types' => ['string'],
'description' => 'description',
]);
$tagWithOptionsFromConstructor = new VarTag('foo', ['string'], 'description');
$this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate());
}

public function testCreatingTagFromReflection()
public function testCreatingTagFromReflection() : void
{
$docreflection = new DocBlockReflection('/** @var int $foo description');
$reflectionTag = $docreflection->getTag('var');
$reflectionTag = (new DocBlockReflection('/** @var int $foo description'))
->getTag('var');

self::assertInstanceOf(ReflectionVarTag::class, $reflectionTag);

/** @var VarTag $tag */
$tag = $this->tagmanager->createTagFromReflection($reflectionTag);
$this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\VarTag', $tag);
$tag = $this->tagManager->createTagFromReflection($reflectionTag);

$this->assertInstanceOf(VarTag::class, $tag);
$this->assertEquals('foo', $tag->getVariableName());
$this->assertEquals('description', $tag->getDescription());
$this->assertEquals('int', $tag->getTypesAsString());
Expand Down
3 changes: 1 addition & 2 deletions test/Generator/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
namespace ZendTest\Code\Generator;

use PHPUnit\Framework\TestCase;
use Zend\Code\Generator\DocBlock\Tag\GenericTag;
use Zend\Code\Generator\DocBlock\Tag\VarTag;
use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\Exception\RuntimeException;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\PropertyValueGenerator;
use Zend\Code\Generator\ValueGenerator;
use Zend\Code\Reflection\ClassReflection;
use Zend\Code\Reflection\DocBlock\Tag\VarTag;

/**
* @group Zend_Code_Generator
Expand Down
3 changes: 2 additions & 1 deletion test/Reflection/DocBlock/Tag/VarTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

namespace ZendTest\Code\Reflection\DocBlock\Tag;

use PHPUnit\Framework\TestCase;
use Zend\Code\Reflection\DocBlock\Tag\VarTag;

/**
* @group Zend_Reflection
* @group Zend_Reflection_DocBlock
*/
class VarTagTest extends \PHPUnit_Framework_TestCase
class VarTagTest extends TestCase
{
public function testParseName()
{
Expand Down

0 comments on commit 3e4db4a

Please sign in to comment.