Skip to content

Commit c27aeed

Browse files
committed
Rewritten Var tag and added test
1 parent 2bab7d9 commit c27aeed

File tree

2 files changed

+303
-6
lines changed

2 files changed

+303
-6
lines changed

src/DocBlock/Tags/Var_.php

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,118 @@
11
<?php
22
/**
3-
* phpDocumentor
3+
* This file is part of phpDocumentor.
44
*
5-
* PHP Version 5.3
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
67
*
7-
* @author Mike van Riel <[email protected]>
8-
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
8+
* @copyright 2010-2015 Mike van Riel<[email protected]>
99
* @license http://www.opensource.org/licenses/mit-license.php MIT
1010
* @link http://phpdoc.org
1111
*/
1212

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

15+
use phpDocumentor\Reflection\DocBlock\Description;
16+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\TypeResolver;
19+
use phpDocumentor\Reflection\Types\Context;
20+
use Webmozart\Assert\Assert;
21+
1522
/**
16-
* Reflection class for a @var tag in a Docblock.
23+
* Reflection class for a {@}var tag in a Docblock.
1724
*/
18-
class Var_ extends Param
25+
class Var_ extends BaseTag
1926
{
27+
/** @var string */
28+
protected $name = 'var';
29+
30+
/** @var Type */
31+
private $type;
32+
33+
/** @var string */
34+
protected $variableName = '';
35+
36+
/**
37+
* @param string $variableName
38+
* @param Type $type
39+
* @param Description $description
40+
*/
41+
public function __construct($variableName, Type $type = null, Description $description = null)
42+
{
43+
Assert::string($variableName);
44+
45+
$this->variableName = $variableName;
46+
$this->type = $type;
47+
$this->description = $description;
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public static function create(
54+
$body,
55+
TypeResolver $typeResolver = null,
56+
DescriptionFactory $descriptionFactory = null,
57+
Context $context = null
58+
) {
59+
Assert::stringNotEmpty($body);
60+
Assert::allNotNull([$typeResolver, $descriptionFactory]);
61+
62+
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
63+
$type = null;
64+
$variableName = '';
65+
66+
// if the first item that is encountered is not a variable; it is a type
67+
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] !== '$')) {
68+
$type = $typeResolver->resolve(array_shift($parts), $context);
69+
array_shift($parts);
70+
}
71+
72+
// if the next item starts with a $ or ...$ it must be the variable name
73+
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] == '$')) {
74+
$variableName = array_shift($parts);
75+
array_shift($parts);
76+
77+
if (substr($variableName, 0, 1) === '$') {
78+
$variableName = substr($variableName, 1);
79+
}
80+
}
81+
82+
$description = $descriptionFactory->create(implode('', $parts), $context);
83+
84+
return new static($variableName, $type, $description);
85+
}
86+
87+
/**
88+
* Returns the variable's name.
89+
*
90+
* @return string
91+
*/
92+
public function getVariableName()
93+
{
94+
return $this->variableName;
95+
}
96+
97+
/**
98+
* Returns the variable's type or null if unknown.
99+
*
100+
* @return Type|null
101+
*/
102+
public function getType()
103+
{
104+
return $this->type;
105+
}
106+
107+
/**
108+
* Returns a string representation for this tag.
109+
*
110+
* @return string
111+
*/
112+
public function __toString()
113+
{
114+
return ($this->type ? $this->type . ' ' : '')
115+
. '$' . $this->variableName
116+
. ($this->description ? ' ' . $this->description : '');
117+
}
20118
}

tests/unit/DocBlock/Tags/VarTest.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
/**
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @copyright 2010-2015 Mike van Riel<[email protected]>
9+
* @license http://www.opensource.org/licenses/mit-license.php MIT
10+
* @link http://phpdoc.org
11+
*/
12+
13+
namespace phpDocumentor\Reflection\DocBlock\Tags;
14+
15+
use Mockery as m;
16+
use phpDocumentor\Reflection\DocBlock\Description;
17+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18+
use phpDocumentor\Reflection\TypeResolver;
19+
use phpDocumentor\Reflection\Types\Context;
20+
use phpDocumentor\Reflection\Types\String_;
21+
22+
/**
23+
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Var_
24+
* @covers ::<private>
25+
*/
26+
class VarTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__construct
30+
* @uses \phpDocumentor\Reflection\DocBlock\Description
31+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
32+
*/
33+
public function testIfCorrectTagNameIsReturned()
34+
{
35+
$fixture = new Var_('myVariable', null, new Description('Description'));
36+
37+
$this->assertSame('var', $fixture->getName());
38+
}
39+
40+
/**
41+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__construct
42+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__toString
43+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
44+
* @uses \phpDocumentor\Reflection\DocBlock\Description
45+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
46+
*/
47+
public function testIfTagCanBeRenderedUsingDefaultFormatter()
48+
{
49+
$fixture = new Var_('myVariable', new String_(), new Description('Description'));
50+
$this->assertSame('string $myVariable Description', $fixture->render());
51+
52+
$fixture = new Var_('myVariable', null, new Description('Description'));
53+
$this->assertSame('$myVariable Description', $fixture->render());
54+
55+
$fixture = new Var_('myVariable');
56+
$this->assertSame('$myVariable', $fixture->render());
57+
}
58+
59+
/**
60+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__construct
61+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
62+
*/
63+
public function testIfTagCanBeRenderedUsingSpecificFormatter()
64+
{
65+
$fixture = new Var_('myVariable');
66+
67+
$formatter = m::mock(Formatter::class);
68+
$formatter->shouldReceive('format')->with($fixture)->andReturn('Rendered output');
69+
70+
$this->assertSame('Rendered output', $fixture->render($formatter));
71+
}
72+
73+
/**
74+
* @covers ::__construct
75+
* @covers ::getVariableName
76+
*/
77+
public function testHasVariableName()
78+
{
79+
$expected = 'myVariable';
80+
81+
$fixture = new Var_($expected);
82+
83+
$this->assertSame($expected, $fixture->getVariableName());
84+
}
85+
86+
/**
87+
* @covers ::__construct
88+
* @covers ::getType
89+
*/
90+
public function testHasType()
91+
{
92+
$expected = new String_();
93+
94+
$fixture = new Var_('myVariable', $expected);
95+
96+
$this->assertSame($expected, $fixture->getType());
97+
}
98+
99+
/**
100+
* @covers ::__construct
101+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription
102+
* @uses \phpDocumentor\Reflection\DocBlock\Description
103+
*/
104+
public function testHasDescription()
105+
{
106+
$expected = new Description('Description');
107+
108+
$fixture = new Var_('1.0', null, $expected);
109+
110+
$this->assertSame($expected, $fixture->getDescription());
111+
}
112+
113+
/**
114+
* @covers ::__construct
115+
* @covers ::__toString
116+
* @uses \phpDocumentor\Reflection\DocBlock\Description
117+
* @uses \phpDocumentor\Reflection\Types\String_
118+
*/
119+
public function testStringRepresentationIsReturned()
120+
{
121+
$fixture = new Var_('myVariable', new String_(), new Description('Description'));
122+
123+
$this->assertSame('string $myVariable Description', (string)$fixture);
124+
}
125+
126+
/**
127+
* @covers ::create
128+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::<public>
129+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
130+
* @uses \phpDocumentor\Reflection\DocBlock\Description
131+
* @uses \phpDocumentor\Reflection\Types\Context
132+
*/
133+
public function testFactoryMethod()
134+
{
135+
$typeResolver = new TypeResolver();
136+
$descriptionFactory = m::mock(DescriptionFactory::class);
137+
$context = new Context('');
138+
139+
$description = new Description('My Description');
140+
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
141+
142+
$fixture = Var_::create('string $myVariable My Description', $typeResolver, $descriptionFactory, $context);
143+
144+
$this->assertSame('string $myVariable My Description', (string)$fixture);
145+
$this->assertSame('myVariable', $fixture->getVariableName());
146+
$this->assertInstanceOf(String_::class, $fixture->getType());
147+
$this->assertSame($description, $fixture->getDescription());
148+
}
149+
150+
/**
151+
* @covers ::create
152+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::<public>
153+
* @uses \phpDocumentor\Reflection\TypeResolver
154+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
155+
* @expectedException \InvalidArgumentException
156+
*/
157+
public function testFactoryMethodFailsIfEmptyBodyIsGiven()
158+
{
159+
$descriptionFactory = m::mock(DescriptionFactory::class);
160+
Var_::create('', new TypeResolver(), $descriptionFactory);
161+
}
162+
163+
/**
164+
* @covers ::create
165+
* @expectedException \InvalidArgumentException
166+
*/
167+
public function testFactoryMethodFailsIfBodyIsNotString()
168+
{
169+
Var_::create([]);
170+
}
171+
172+
/**
173+
* @covers ::create
174+
* @expectedException \InvalidArgumentException
175+
*/
176+
public function testFactoryMethodFailsIfResolverIsNull()
177+
{
178+
Var_::create('body');
179+
}
180+
181+
/**
182+
* @covers ::create
183+
* @uses \phpDocumentor\Reflection\TypeResolver
184+
* @expectedException \InvalidArgumentException
185+
*/
186+
public function testFactoryMethodFailsIfDescriptionFactoryIsNull()
187+
{
188+
Var_::create('body', new TypeResolver());
189+
}
190+
191+
/**
192+
* @covers ::__construct
193+
* @expectedException \InvalidArgumentException
194+
*/
195+
public function testExceptionIsThrownIfVariableNameIsNotString()
196+
{
197+
new Var_([]);
198+
}
199+
}

0 commit comments

Comments
 (0)