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

Commit 3e4db4a

Browse files
committed
#41 cleaning up VarTag additions, marking the setter as @internal to prevent misuse, as it is needed for the TagManager to work correctly
1 parent e1f0d02 commit 3e4db4a

File tree

4 files changed

+49
-45
lines changed

4 files changed

+49
-45
lines changed

src/Generator/DocBlock/Tag/VarTag.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ class VarTag extends AbstractTypeableTag implements TagInterface
1414
/**
1515
* @var string|null
1616
*/
17-
protected $variableName;
17+
private $variableName;
1818

1919
/**
20-
* @param string $variableName
20+
* @param string|null $variableName
2121
* @param string|string[] $types
22-
* @param string $description
22+
* @param string|null $description
2323
*/
24-
public function __construct($variableName = null, $types = [], $description = null)
24+
public function __construct(?string $variableName = null, $types = [], ?string $description = null)
2525
{
26-
if (!empty($variableName)) {
27-
$this->setVariableName($variableName);
26+
if (null !== $variableName) {
27+
$this->variableName = ltrim($variableName, '$');
2828
}
2929

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

4141
/**
42-
* @param string $variableName
43-
* @return self
42+
* @internal this code is only public for compatibility with the
43+
* @see \Zend\Code\Generator\DocBlock\TagManager, which
44+
* uses setters
4445
*/
45-
public function setVariableName($variableName)
46+
public function setVariableName(?string $variableName) : void
4647
{
47-
$this->variableName = ltrim($variableName, '$');
48-
return $this;
48+
if (null !== $variableName) {
49+
$this->variableName = ltrim($variableName, '$');
50+
}
4951
}
5052

5153
/**
@@ -59,11 +61,11 @@ public function getVariableName()
5961
/**
6062
* {@inheritDoc}
6163
*/
62-
public function generate()
64+
public function generate() : string
6365
{
6466
return '@var'
6567
. ((!empty($this->types)) ? ' ' . $this->getTypesAsString() : '')
66-
. ((!empty($this->variableName)) ? ' $' . $this->variableName : '')
68+
. (null !== $this->variableName ? ' $' . $this->variableName : '')
6769
. ((!empty($this->description)) ? ' ' . $this->description : '');
6870
}
6971
}

test/Generator/DocBlock/Tag/VarTagTest.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,85 @@
99

1010
namespace ZendTest\Code\Generator\DocBlock\Tag;
1111

12+
use PHPUnit\Framework\TestCase;
1213
use Zend\Code\Generator\DocBlock\Tag\VarTag;
1314
use Zend\Code\Generator\DocBlock\TagManager;
15+
use Zend\Code\Reflection\DocBlock\Tag\VarTag as ReflectionVarTag;
1416
use Zend\Code\Reflection\DocBlockReflection;
1517

1618
/**
17-
* @group Zend_Code_Generator
18-
* @group Zend_Code_Generator_Php
19+
* @covers \Zend\Code\Generator\DocBlock\Tag\VarTag
1920
*/
20-
class VarTagTest extends \PHPUnit_Framework_TestCase
21+
class VarTagTest extends TestCase
2122
{
2223
/**
2324
* @var VarTag
2425
*/
25-
protected $tag;
26+
private $tag;
27+
2628
/**
2729
* @var TagManager
2830
*/
29-
protected $tagmanager;
31+
private $tagManager;
3032

31-
public function setUp()
33+
protected function setUp() : void
3234
{
33-
$this->tag = new VarTag();
34-
$this->tagmanager = new TagManager();
35-
$this->tagmanager->initializeDefaultTags();
36-
}
35+
parent::setUp();
3736

38-
public function tearDown()
39-
{
40-
$this->tag = null;
41-
$this->tagmanager = null;
37+
$this->tag = new VarTag();
38+
$this->tagManager = new TagManager();
39+
40+
$this->tagManager->initializeDefaultTags();
4241
}
4342

44-
public function testGetterAndSetterPersistValue()
43+
public function testGetterAndSetterPersistValue() : void
4544
{
46-
$this->tag->setVariableName('variable');
47-
$this->assertEquals('variable', $this->tag->getVariableName());
48-
}
45+
$tag = new VarTag('variable');
4946

47+
self::assertSame('variable', $tag->getVariableName());
48+
}
5049

51-
public function testGetterForVariableNameTrimsCorrectly()
50+
public function testGetterForVariableNameTrimsCorrectly() : void
5251
{
5352
$this->tag->setVariableName('$variable$');
5453
$this->assertEquals('variable$', $this->tag->getVariableName());
5554
}
5655

57-
public function testNameIsCorrect()
56+
public function testNameIsCorrect() : void
5857
{
5958
$this->assertEquals('var', $this->tag->getName());
6059
}
6160

62-
public function testParamProducesCorrectDocBlockLine()
61+
public function testParamProducesCorrectDocBlockLine() : void
6362
{
6463
$this->tag->setVariableName('variable');
6564
$this->tag->setTypes('string[]');
6665
$this->tag->setDescription('description');
6766
$this->assertEquals('@var string[] $variable description', $this->tag->generate());
6867
}
6968

70-
public function testConstructorWithOptions()
69+
public function testConstructorWithOptions() : void
7170
{
7271
$this->tag->setOptions([
7372
'variableName' => 'foo',
74-
'types' => ['string'],
75-
'description' => 'description'
73+
'types' => ['string'],
74+
'description' => 'description',
7675
]);
7776
$tagWithOptionsFromConstructor = new VarTag('foo', ['string'], 'description');
7877
$this->assertEquals($this->tag->generate(), $tagWithOptionsFromConstructor->generate());
7978
}
8079

81-
public function testCreatingTagFromReflection()
80+
public function testCreatingTagFromReflection() : void
8281
{
83-
$docreflection = new DocBlockReflection('/** @var int $foo description');
84-
$reflectionTag = $docreflection->getTag('var');
82+
$reflectionTag = (new DocBlockReflection('/** @var int $foo description'))
83+
->getTag('var');
84+
85+
self::assertInstanceOf(ReflectionVarTag::class, $reflectionTag);
8586

8687
/** @var VarTag $tag */
87-
$tag = $this->tagmanager->createTagFromReflection($reflectionTag);
88-
$this->assertInstanceOf('Zend\Code\Generator\DocBlock\Tag\VarTag', $tag);
88+
$tag = $this->tagManager->createTagFromReflection($reflectionTag);
89+
90+
$this->assertInstanceOf(VarTag::class, $tag);
8991
$this->assertEquals('foo', $tag->getVariableName());
9092
$this->assertEquals('description', $tag->getDescription());
9193
$this->assertEquals('int', $tag->getTypesAsString());

test/Generator/PropertyGeneratorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
namespace ZendTest\Code\Generator;
1111

1212
use PHPUnit\Framework\TestCase;
13-
use Zend\Code\Generator\DocBlock\Tag\GenericTag;
13+
use Zend\Code\Generator\DocBlock\Tag\VarTag;
1414
use Zend\Code\Generator\DocBlockGenerator;
1515
use Zend\Code\Generator\Exception\RuntimeException;
1616
use Zend\Code\Generator\PropertyGenerator;
1717
use Zend\Code\Generator\PropertyValueGenerator;
1818
use Zend\Code\Generator\ValueGenerator;
1919
use Zend\Code\Reflection\ClassReflection;
20-
use Zend\Code\Reflection\DocBlock\Tag\VarTag;
2120

2221
/**
2322
* @group Zend_Code_Generator

test/Reflection/DocBlock/Tag/VarTagTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
namespace ZendTest\Code\Reflection\DocBlock\Tag;
1111

12+
use PHPUnit\Framework\TestCase;
1213
use Zend\Code\Reflection\DocBlock\Tag\VarTag;
1314

1415
/**
1516
* @group Zend_Reflection
1617
* @group Zend_Reflection_DocBlock
1718
*/
18-
class VarTagTest extends \PHPUnit_Framework_TestCase
19+
class VarTagTest extends TestCase
1920
{
2021
public function testParseName()
2122
{

0 commit comments

Comments
 (0)