Skip to content

Commit be6ed8b

Browse files
committed
add more unit tests and fixed the output
1 parent acf538a commit be6ed8b

File tree

10 files changed

+392
-13
lines changed

10 files changed

+392
-13
lines changed

src/DocBlock/Tags/Param.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public static function create(
8585
// if the next item starts with a $ or ...$ or &$ or &...$ it must be the variable name
8686
if (isset($parts[0]) && self::strStartsWithVariable($parts[0])) {
8787
$variableName = array_shift($parts);
88-
array_shift($parts);
88+
if ($type) {
89+
array_shift($parts);
90+
}
8991

9092
Assert::notNull($variableName);
9193

@@ -141,8 +143,8 @@ public function __toString() : string
141143
return ($this->type ? $this->type . ' ' : '')
142144
. ($this->isReference() ? '&' : '')
143145
. ($this->isVariadic() ? '...' : '')
144-
. ($this->variableName !== null ? '$' . $this->variableName : '')
145-
. ($this->description ? ' ' . $this->description : '');
146+
. ($this->variableName ? '$' . $this->variableName : '')
147+
. ($this->description ? ($this->variableName ? ' ' : '') . $this->description : '');
146148
}
147149

148150
private static function strStartsWithVariable(string $str) : bool

src/DocBlock/Tags/Property.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public static function create(
6868
array_unshift($parts, $firstPart);
6969
}
7070

71-
// if the next item starts with a $ or ...$ it must be the variable name
71+
// if the next item starts with a $ it must be the variable name
7272
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7373
$variableName = array_shift($parts);
74-
array_shift($parts);
74+
if ($type) {
75+
array_shift($parts);
76+
}
7577

7678
Assert::notNull($variableName);
7779

@@ -98,6 +100,6 @@ public function __toString() : string
98100
{
99101
return ($this->type ? $this->type . ' ' : '')
100102
. ($this->variableName ? '$' . $this->variableName : '')
101-
. ($this->description ? ' ' . $this->description : '');
103+
. ($this->description ? ($this->variableName ? ' ' : '') . $this->description : '');
102104
}
103105
}

src/DocBlock/Tags/PropertyRead.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public static function create(
7171
// if the next item starts with a $ or ...$ it must be the variable name
7272
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7373
$variableName = array_shift($parts);
74-
array_shift($parts);
74+
if ($type) {
75+
array_shift($parts);
76+
}
7577

7678
Assert::notNull($variableName);
7779

@@ -98,6 +100,6 @@ public function __toString() : string
98100
{
99101
return ($this->type ? $this->type . ' ' : '')
100102
. ($this->variableName ? '$' . $this->variableName : '')
101-
. ($this->description ? ' ' . $this->description : '');
103+
. ($this->description ? ($this->variableName ? ' ' : '') . $this->description : '');
102104
}
103105
}

src/DocBlock/Tags/PropertyWrite.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public static function create(
7171
// if the next item starts with a $ or ...$ it must be the variable name
7272
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7373
$variableName = array_shift($parts);
74-
array_shift($parts);
74+
if ($type) {
75+
array_shift($parts);
76+
}
7577

7678
Assert::notNull($variableName);
7779

@@ -98,6 +100,6 @@ public function __toString() : string
98100
{
99101
return ($this->type ? $this->type . ' ' : '')
100102
. ($this->variableName ? '$' . $this->variableName : '')
101-
. ($this->description ? ' ' . $this->description : '');
103+
. ($this->description ? ($this->variableName ? ' ' : '') . $this->description : '');
102104
}
103105
}

src/DocBlock/Tags/Var_.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public static function create(
7272
// if the next item starts with a $ or ...$ it must be the variable name
7373
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7474
$variableName = array_shift($parts);
75-
array_shift($parts);
75+
if ($type) {
76+
array_shift($parts);
77+
}
7678

7779
Assert::notNull($variableName);
7880

@@ -98,7 +100,7 @@ public function getVariableName() : ?string
98100
public function __toString() : string
99101
{
100102
return ($this->type ? $this->type . ' ' : '')
101-
. (empty($this->variableName) ? '' : '$' . $this->variableName)
102-
. ($this->description ? ' ' . $this->description : '');
103+
. ($this->variableName ? '$' . $this->variableName : '')
104+
. ($this->description ? ($this->variableName ? ' ' : '') . $this->description : '');
103105
}
104106
}

tests/unit/DocBlock/Tags/ParamTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Mockery as m;
1717
use phpDocumentor\Reflection\DocBlock\Description;
1818
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
19+
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
20+
use phpDocumentor\Reflection\FqsenResolver;
1921
use phpDocumentor\Reflection\TypeResolver;
2022
use phpDocumentor\Reflection\Types\Context;
23+
use phpDocumentor\Reflection\Types\Integer;
2124
use phpDocumentor\Reflection\Types\String_;
2225
use PHPUnit\Framework\TestCase;
2326

@@ -270,6 +273,128 @@ public function testFactoryMethodWithVariadicReference() : void
270273
$this->assertSame($description, $fixture->getDescription());
271274
}
272275

276+
/**
277+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
278+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
279+
* @uses \phpDocumentor\Reflection\DocBlock\Description
280+
* @uses \phpDocumentor\Reflection\Types\Context
281+
*
282+
* @covers ::create
283+
*/
284+
public function testFactoryMethodWithReferenceWithoutType() : void
285+
{
286+
$typeResolver = new TypeResolver();
287+
$fqsenResolver = new FqsenResolver();
288+
$tagFactory = new StandardTagFactory($fqsenResolver);
289+
$descriptionFactory = new DescriptionFactory($tagFactory);
290+
$context = new Context('');
291+
292+
$fixture = Param::create(
293+
'&$myParameter My Description',
294+
$typeResolver,
295+
$descriptionFactory,
296+
$context
297+
);
298+
299+
$this->assertSame('&$myParameter My Description', (string) $fixture);
300+
$this->assertSame('myParameter', $fixture->getVariableName());
301+
$this->assertNull($fixture->getType());
302+
$this->assertFalse($fixture->isVariadic());
303+
$this->assertTrue($fixture->isReference());
304+
$this->assertSame('My Description', $fixture->getDescription() . '');
305+
}
306+
307+
/**
308+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
309+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
310+
* @uses \phpDocumentor\Reflection\DocBlock\Description
311+
* @uses \phpDocumentor\Reflection\Types\Context
312+
*
313+
* @covers ::create
314+
*/
315+
public function testFactoryMethodWithVariadicReferenceWithoutType() : void
316+
{
317+
$typeResolver = new TypeResolver();
318+
$fqsenResolver = new FqsenResolver();
319+
$tagFactory = new StandardTagFactory($fqsenResolver);
320+
$descriptionFactory = new DescriptionFactory($tagFactory);
321+
$context = new Context('');
322+
323+
$fixture = Param::create(
324+
'&...$myParameter My Description',
325+
$typeResolver,
326+
$descriptionFactory,
327+
$context
328+
);
329+
330+
$this->assertSame('&...$myParameter My Description', (string) $fixture);
331+
$this->assertSame('myParameter', $fixture->getVariableName());
332+
$this->assertNull($fixture->getType());
333+
$this->assertTrue($fixture->isVariadic());
334+
$this->assertTrue($fixture->isReference());
335+
$this->assertSame('My Description', $fixture->getDescription() . '');
336+
}
337+
338+
/**
339+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
340+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
341+
* @uses \phpDocumentor\Reflection\DocBlock\Description
342+
* @uses \phpDocumentor\Reflection\Types\Context
343+
*
344+
* @covers ::create
345+
*/
346+
public function testFactoryMethodWithoutType() : void
347+
{
348+
$typeResolver = new TypeResolver();
349+
$fqsenResolver = new FqsenResolver();
350+
$tagFactory = new StandardTagFactory($fqsenResolver);
351+
$descriptionFactory = new DescriptionFactory($tagFactory);
352+
$context = new Context('');
353+
354+
$fixture = Param::create(
355+
'$myParameter My Description',
356+
$typeResolver,
357+
$descriptionFactory,
358+
$context
359+
);
360+
361+
$this->assertSame('$myParameter My Description', (string) $fixture);
362+
$this->assertSame('myParameter', $fixture->getVariableName());
363+
$this->assertNull($fixture->getType());
364+
$this->assertFalse($fixture->isVariadic());
365+
$this->assertFalse($fixture->isReference());
366+
$this->assertSame('My Description', $fixture->getDescription() . '');
367+
}
368+
369+
/**
370+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
371+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
372+
* @uses \phpDocumentor\Reflection\DocBlock\Description
373+
* @uses \phpDocumentor\Reflection\Types\Context
374+
*
375+
* @covers ::create
376+
*/
377+
public function testFactoryMethodWithType() : void
378+
{
379+
$typeResolver = new TypeResolver();
380+
$fqsenResolver = new FqsenResolver();
381+
$tagFactory = new StandardTagFactory($fqsenResolver);
382+
$descriptionFactory = new DescriptionFactory($tagFactory);
383+
$context = new Context('');
384+
385+
$fixture = Param::create(
386+
'int My Description',
387+
$typeResolver,
388+
$descriptionFactory,
389+
$context
390+
);
391+
392+
$this->assertSame('int My Description', (string) $fixture);
393+
$this->assertSame('', $fixture->getVariableName());
394+
$this->assertInstanceOf(Integer::class, $fixture->getType());
395+
$this->assertSame('My Description', $fixture->getDescription() . '');
396+
}
397+
273398
/**
274399
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
275400
* @uses \phpDocumentor\Reflection\TypeResolver

tests/unit/DocBlock/Tags/PropertyReadTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Mockery as m;
1717
use phpDocumentor\Reflection\DocBlock\Description;
1818
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
19+
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
20+
use phpDocumentor\Reflection\FqsenResolver;
1921
use phpDocumentor\Reflection\TypeResolver;
2022
use phpDocumentor\Reflection\Types\Context;
23+
use phpDocumentor\Reflection\Types\Integer;
2124
use phpDocumentor\Reflection\Types\String_;
2225
use PHPUnit\Framework\TestCase;
2326

@@ -169,6 +172,64 @@ public function testFactoryMethod() : void
169172
$this->assertSame($description, $fixture->getDescription());
170173
}
171174

175+
/**
176+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
177+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
178+
* @uses \phpDocumentor\Reflection\DocBlock\Description
179+
* @uses \phpDocumentor\Reflection\Types\Context
180+
*
181+
* @covers ::create
182+
*/
183+
public function testFactoryMethodWithoutType() : void
184+
{
185+
$typeResolver = new TypeResolver();
186+
$fqsenResolver = new FqsenResolver();
187+
$tagFactory = new StandardTagFactory($fqsenResolver);
188+
$descriptionFactory = new DescriptionFactory($tagFactory);
189+
$context = new Context('');
190+
191+
$fixture = PropertyRead::create(
192+
'$myParameter My Description',
193+
$typeResolver,
194+
$descriptionFactory,
195+
$context
196+
);
197+
198+
$this->assertSame('$myParameter My Description', (string) $fixture);
199+
$this->assertSame('myParameter', $fixture->getVariableName());
200+
$this->assertNull($fixture->getType());
201+
$this->assertSame('My Description', $fixture->getDescription() . '');
202+
}
203+
204+
/**
205+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
206+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
207+
* @uses \phpDocumentor\Reflection\DocBlock\Description
208+
* @uses \phpDocumentor\Reflection\Types\Context
209+
*
210+
* @covers ::create
211+
*/
212+
public function testFactoryMethodWithType() : void
213+
{
214+
$typeResolver = new TypeResolver();
215+
$fqsenResolver = new FqsenResolver();
216+
$tagFactory = new StandardTagFactory($fqsenResolver);
217+
$descriptionFactory = new DescriptionFactory($tagFactory);
218+
$context = new Context('');
219+
220+
$fixture = PropertyRead::create(
221+
'int My Description',
222+
$typeResolver,
223+
$descriptionFactory,
224+
$context
225+
);
226+
227+
$this->assertSame('int My Description', (string) $fixture);
228+
$this->assertSame('', $fixture->getVariableName());
229+
$this->assertInstanceOf(Integer::class, $fixture->getType());
230+
$this->assertSame('My Description', $fixture->getDescription() . '');
231+
}
232+
172233
/**
173234
* @uses \phpDocumentor\Reflection\DocBlock\Tags\PropertyRead::<public>
174235
* @uses \phpDocumentor\Reflection\TypeResolver

tests/unit/DocBlock/Tags/PropertyTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Mockery as m;
1717
use phpDocumentor\Reflection\DocBlock\Description;
1818
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
19+
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
20+
use phpDocumentor\Reflection\FqsenResolver;
1921
use phpDocumentor\Reflection\TypeResolver;
2022
use phpDocumentor\Reflection\Types\Context;
23+
use phpDocumentor\Reflection\Types\Integer;
2124
use phpDocumentor\Reflection\Types\String_;
2225
use PHPUnit\Framework\TestCase;
2326

@@ -164,6 +167,64 @@ public function testFactoryMethod() : void
164167
$this->assertSame($description, $fixture->getDescription());
165168
}
166169

170+
/**
171+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
172+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
173+
* @uses \phpDocumentor\Reflection\DocBlock\Description
174+
* @uses \phpDocumentor\Reflection\Types\Context
175+
*
176+
* @covers ::create
177+
*/
178+
public function testFactoryMethodWithoutType() : void
179+
{
180+
$typeResolver = new TypeResolver();
181+
$fqsenResolver = new FqsenResolver();
182+
$tagFactory = new StandardTagFactory($fqsenResolver);
183+
$descriptionFactory = new DescriptionFactory($tagFactory);
184+
$context = new Context('');
185+
186+
$fixture = Property::create(
187+
'$myParameter My Description',
188+
$typeResolver,
189+
$descriptionFactory,
190+
$context
191+
);
192+
193+
$this->assertSame('$myParameter My Description', (string) $fixture);
194+
$this->assertSame('myParameter', $fixture->getVariableName());
195+
$this->assertNull($fixture->getType());
196+
$this->assertSame('My Description', $fixture->getDescription() . '');
197+
}
198+
199+
/**
200+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Param::<public>
201+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
202+
* @uses \phpDocumentor\Reflection\DocBlock\Description
203+
* @uses \phpDocumentor\Reflection\Types\Context
204+
*
205+
* @covers ::create
206+
*/
207+
public function testFactoryMethodWithType() : void
208+
{
209+
$typeResolver = new TypeResolver();
210+
$fqsenResolver = new FqsenResolver();
211+
$tagFactory = new StandardTagFactory($fqsenResolver);
212+
$descriptionFactory = new DescriptionFactory($tagFactory);
213+
$context = new Context('');
214+
215+
$fixture = Property::create(
216+
'int My Description',
217+
$typeResolver,
218+
$descriptionFactory,
219+
$context
220+
);
221+
222+
$this->assertSame('int My Description', (string) $fixture);
223+
$this->assertSame('', $fixture->getVariableName());
224+
$this->assertInstanceOf(Integer::class, $fixture->getType());
225+
$this->assertSame('My Description', $fixture->getDescription() . '');
226+
}
227+
167228
/**
168229
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Property::<public>
169230
* @uses \phpDocumentor\Reflection\TypeResolver

0 commit comments

Comments
 (0)