diff --git a/src/Generator/DocBlock/Tag/VarTag.php b/src/Generator/DocBlock/Tag/VarTag.php index 98a03422..5dbf2ee2 100644 --- a/src/Generator/DocBlock/Tag/VarTag.php +++ b/src/Generator/DocBlock/Tag/VarTag.php @@ -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); @@ -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, '$'); + } } /** @@ -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 : ''); } } diff --git a/test/Generator/DocBlock/Tag/VarTagTest.php b/test/Generator/DocBlock/Tag/VarTagTest.php index 45c74e70..4a0ee06d 100644 --- a/test/Generator/DocBlock/Tag/VarTagTest.php +++ b/test/Generator/DocBlock/Tag/VarTagTest.php @@ -9,57 +9,56 @@ 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[]'); @@ -67,25 +66,28 @@ public function testParamProducesCorrectDocBlockLine() $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()); diff --git a/test/Generator/PropertyGeneratorTest.php b/test/Generator/PropertyGeneratorTest.php index 22cea5a1..4ae0f074 100644 --- a/test/Generator/PropertyGeneratorTest.php +++ b/test/Generator/PropertyGeneratorTest.php @@ -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 diff --git a/test/Reflection/DocBlock/Tag/VarTagTest.php b/test/Reflection/DocBlock/Tag/VarTagTest.php index 402312e7..6c7f7425 100644 --- a/test/Reflection/DocBlock/Tag/VarTagTest.php +++ b/test/Reflection/DocBlock/Tag/VarTagTest.php @@ -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() {