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

Commit 79450e5

Browse files
committed
Fixed var tag pattern
Var tag pattern now allows var tags without the variable name. This fixes #128.
1 parent 6b1059d commit 79450e5

File tree

4 files changed

+178
-36
lines changed

4 files changed

+178
-36
lines changed

src/Reflection/DocBlock/Tag/VarTag.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public function initialize($tagDocblockLine) : void
4141
{
4242
$match = [];
4343

44-
if (! preg_match('#^(.+)?(\$[\S]+)\s*(.*)$#m', $tagDocblockLine, $match)) {
44+
if (! preg_match(
45+
'#^([^\$]\S+)?\s*(\$[\S]+)?\s*(.*)$#m',
46+
$tagDocblockLine,
47+
$match
48+
)) {
4549
return;
4650
}
4751

test/Reflection/DocBlock/Tag/VarTagTest.php

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,86 @@
1818
*/
1919
class VarTagTest extends TestCase
2020
{
21-
public function testParseName()
22-
{
21+
/**
22+
* @dataProvider varTagProvider
23+
*/
24+
public function testParse(
25+
string $line,
26+
array $expectedTypes,
27+
?string $expectedVariableName,
28+
?string $expectedDescription
29+
) {
2330
$tag = new VarTag();
24-
$tag->initialize('$test');
25-
$this->assertEquals('var', $tag->getName());
26-
$this->assertEquals('$test', $tag->getVariableName());
27-
$this->assertNull($tag->getDescription());
31+
$tag->initialize($line);
32+
$this->assertSame($expectedTypes, $tag->getTypes());
33+
$this->assertSame($expectedVariableName, $tag->getVariableName());
34+
$this->assertSame($expectedDescription, $tag->getDescription());
2835
}
2936

30-
public function testParseTypeAndName()
37+
public function varTagProvider(): array
3138
{
32-
$tag = new VarTag();
33-
$tag->initialize('string|null $test');
34-
$this->assertEquals('$test', $tag->getVariableName());
35-
$this->assertNull($tag->getDescription());
36-
$this->assertEquals(['string', 'null'], $tag->getTypes());
37-
}
39+
return [
40+
'only type' => [
41+
'string',
42+
['string'],
43+
null,
44+
null
45+
],
46+
'only multiple types' => [
47+
'string|int',
48+
['string', 'int'],
49+
null,
50+
null
51+
],
52+
'type and name' => [
53+
'string $test',
54+
['string'],
55+
'$test',
56+
null
57+
],
58+
'multiple types and name' => [
59+
'string|int $test',
60+
['string', 'int'],
61+
'$test',
62+
null
63+
],
64+
'only name' => [
65+
'$test',
66+
[],
67+
'$test',
68+
null
69+
],
70+
'name and description' => [
71+
'$test Foo Bar',
72+
[],
73+
'$test',
74+
'Foo Bar'
75+
],
76+
'type and description' => [
77+
'string Foo bar',
78+
['string'],
79+
null,
80+
'Foo bar'
81+
],
82+
'multiple types and description' => [
83+
'string|int Foo bar',
84+
['string', 'int'],
85+
null,
86+
'Foo bar'
87+
],
88+
'type, name and description' => [
89+
'string $test Foo bar',
90+
['string'],
91+
'$test',
92+
'Foo bar'
93+
],
94+
'multiple types, name and description' => [
95+
'string|int $test Foo bar',
96+
['string', 'int'],
97+
'$test',
98+
'Foo bar'
99+
],
38100

39-
public function testParseNameAndDescription()
40-
{
41-
$tag = new VarTag();
42-
$tag->initialize('$test I\'m test property');
43-
$this->assertEquals('$test', $tag->getVariableName());
44-
$this->assertEquals('I\'m test property', $tag->getDescription());
45-
}
46-
47-
public function testParseTypeAndNameAndDescription()
48-
{
49-
$tag = new VarTag();
50-
$tag->initialize('string $test I\'m test variable');
51-
$this->assertEquals('$test', $tag->getVariableName());
52-
$this->assertEquals('I\'m test variable', $tag->getDescription());
101+
];
53102
}
54103
}

test/Reflection/ReflectionDocBlockTagTest.php

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace ZendTest\Code\Reflection;
1111

1212
use PHPUnit\Framework\TestCase;
13+
use Zend\Code\Generator\DocBlock\Tag\VarTag;
1314
use Zend\Code\Reflection;
1415

1516
/**
@@ -41,8 +42,16 @@ public function testTagShouldAllowMultipleWhitespacesBeforeDescription()
4142
$classReflection = new Reflection\ClassReflection(TestAsset\TestSampleClass6::class);
4243

4344
$tag = $classReflection->getMethod('doSomething')->getDocBlock()->getTag('descriptionTag');
44-
self::assertNotEquals(' A tag with just a description', $tag->getContent(), 'Final Match Failed');
45-
self::assertEquals('A tag with just a description', $tag->getContent(), 'Final Match Failed');
45+
self::assertNotEquals(
46+
' A tag with just a description',
47+
$tag->getContent(),
48+
'Final Match Failed'
49+
);
50+
self::assertEquals(
51+
'A tag with just a description',
52+
$tag->getContent(),
53+
'Final Match Failed'
54+
);
4655
}
4756

4857
public function testToString()
@@ -53,7 +62,7 @@ public function testToString()
5362

5463
$expectedString = 'DocBlock Tag [ * @descriptionTag ]' . "\n";
5564

56-
self::assertEquals($expectedString, (string) $tag);
65+
self::assertEquals($expectedString, (string)$tag);
5766
}
5867

5968
public function testTypeParam()
@@ -79,10 +88,13 @@ public function testAllowsMultipleSpacesInDocBlockTagLine()
7988

8089
$paramTag = $classReflection->getMethod('doSomething')->getDocBlock()->getTag('param');
8190

82-
8391
self::assertEquals('int', $paramTag->getType(), 'Second Match Failed');
8492
self::assertEquals('$var', $paramTag->getVariableName(), 'Third Match Failed');
85-
self::assertEquals('Description of $var', $paramTag->getDescription(), 'Final Match Failed');
93+
self::assertEquals(
94+
'Description of $var',
95+
$paramTag->getDescription(),
96+
'Final Match Failed'
97+
);
8698
}
8799

88100
/**
@@ -91,8 +103,7 @@ public function testAllowsMultipleSpacesInDocBlockTagLine()
91103
public function testNamespaceInParam()
92104
{
93105
$classReflection = new Reflection\ClassReflection(TestAsset\TestSampleClass7::class);
94-
$paramTag = $classReflection->getMethod('doSomething')->getDocBlock()->getTag('param');
95-
106+
$paramTag = $classReflection->getMethod('doSomething')->getDocBlock()->getTag('param');
96107

97108
self::assertEquals('Zend\Foo\Bar', $paramTag->getType());
98109
self::assertEquals('$var', $paramTag->getVariableName());
@@ -114,7 +125,11 @@ public function testAllowsMultipleSpacesInDocBlockTagLine2()
114125
$paramTag = $classReflection->getMethod('doSomething')->getDocBlock()->getTag('return');
115126

116127
self::assertEquals('string', $paramTag->getType(), 'Second Match Failed');
117-
self::assertEquals('Description of return value', $paramTag->getDescription(), 'Final Match Failed');
128+
self::assertEquals(
129+
'Description of return value',
130+
$paramTag->getDescription(),
131+
'Final Match Failed'
132+
);
118133
}
119134

120135
/**
@@ -128,4 +143,48 @@ public function testReturnClassWithNamespace()
128143

129144
self::assertEquals('Zend\Code\Reflection\DocBlock', $paramTag->getType());
130145
}
146+
147+
/**
148+
* @dataProvider propertyVarDocProvider
149+
*/
150+
public function testPropertyVarDoc(
151+
string $property,
152+
array $expectedTypes,
153+
?string $expectedName,
154+
?string $expectedDescription
155+
) {
156+
$classReflection = new Reflection\ClassReflection(
157+
TestAsset\TestSampleClass14::class
158+
);
159+
160+
/** @var VarTag $varTag */
161+
$varTag = $classReflection
162+
->getProperty($property)
163+
->getDocBlock()
164+
->getTag('var');
165+
166+
self::assertSame($expectedTypes, $varTag->getTypes());
167+
self::assertSame($expectedName, $varTag->getVariableName());
168+
self::assertSame($expectedDescription, $varTag->getDescription());
169+
}
170+
171+
public function propertyVarDocProvider(): array
172+
{
173+
return [
174+
'only type' => ['onlyType', ['string'], null, null],
175+
'type and description' => [
176+
'typeDescription',
177+
['string'],
178+
null,
179+
'Foo bar'
180+
],
181+
'type and name' => ['typeName', ['string'], '$typeName', null],
182+
'type, name and description' => [
183+
'typeNameDescription',
184+
['string'],
185+
'$typeNameDescription',
186+
'Foo bar'
187+
],
188+
];
189+
}
131190
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* @author Pieter Wiersema <[email protected]>
4+
* @copyright Copyright (c) 2011-now AllCode, https://allcode.nl
5+
*/
6+
7+
namespace ZendTest\Code\Reflection\TestAsset;
8+
9+
class TestSampleClass14
10+
{
11+
/**
12+
* @var string
13+
*/
14+
public $onlyType;
15+
16+
/**
17+
* @var string Foo bar
18+
*/
19+
public $typeDescription;
20+
21+
/**
22+
* @var string $typeName
23+
*/
24+
public $typeName;
25+
26+
/**
27+
* @var string $typeNameDescription Foo bar
28+
*/
29+
public $typeNameDescription;
30+
}

0 commit comments

Comments
 (0)