Skip to content

Commit bb629f6

Browse files
committed
Improvements to code
1 parent 1b78639 commit bb629f6

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

src/DocBlock/StandardTagFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private function createTag(string $body, string $name, TypeContext $context) : T
229229
$callable = [$handlerClassName, 'create'];
230230
return call_user_func_array($callable, $arguments);
231231
} catch (InvalidArgumentException $e) {
232-
return InvalidTag::create($body, $name, $e);
232+
return InvalidTag::create($body, $name)->withError($e);
233233
}
234234
}
235235

src/DocBlock/Tags/InvalidTag.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use phpDocumentor\Reflection\DocBlock\Tag;
88
use Throwable;
9-
use Webmozart\Assert\Assert;
109

1110
/**
1211
* This class represents an exception during the tag creation
@@ -26,17 +25,16 @@ final class InvalidTag implements Tag
2625
/** @var string */
2726
private $body;
2827

29-
/** @var Throwable */
28+
/** @var Throwable|null */
3029
private $throwable;
3130

32-
private function __construct(string $name, string $body, Throwable $throwable)
31+
private function __construct(string $name, string $body)
3332
{
34-
$this->name = $name;
35-
$this->body = $body;
36-
$this->throwable = $throwable;
33+
$this->name = $name;
34+
$this->body = $body;
3735
}
3836

39-
public function getException() : Throwable
37+
public function getException() : ?Throwable
4038
{
4139
return $this->throwable;
4240
}
@@ -47,13 +45,21 @@ public function getName() : string
4745
}
4846

4947
/**
48+
* @return self
49+
*
5050
* @inheritDoc
5151
*/
52-
public static function create(string $body, string $name = '', ?Throwable $exception = null)
52+
public static function create(string $body, string $name = '')
5353
{
54-
Assert::notNull($exception);
54+
return new self($name, $body);
55+
}
56+
57+
public function withError(Throwable $exception) : self
58+
{
59+
$tag = new self($this->name, $this->body);
60+
$tag->throwable = $exception;
5561

56-
return new self($name, $body, $exception);
62+
return $tag;
5763
}
5864

5965
public function render(?Formatter $formatter = null) : string

tests/unit/DocBlock/StandardTagFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function testReturnTagIsMappedCorrectly() : void
332332
$this->assertSame('return', $tag->getName());
333333
}
334334

335-
public function testInvalidTagIsReturnedOnFailure()
335+
public function testInvalidTagIsReturnedOnFailure() : void
336336
{
337337
$tagFactory = new StandardTagFactory(m::mock(FqsenResolver::class));
338338

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags;
6+
7+
use Exception;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/**
11+
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
12+
* @covers ::<private>
13+
* @covers ::getName
14+
* @covers ::render
15+
* @covers ::getException
16+
* @covers ::create
17+
*/
18+
final class InvalidTagTest extends TestCase
19+
{
20+
public function testCreationWithoutError() : void
21+
{
22+
$tag = InvalidTag::create('Body', 'name');
23+
24+
self::assertSame('name', $tag->getName());
25+
self::assertSame('@name Body', $tag->render());
26+
self::assertNull($tag->getException());
27+
}
28+
29+
/**
30+
* @covers ::withError
31+
*/
32+
public function testCreationWithError() : void
33+
{
34+
$exception = new Exception();
35+
$tag = InvalidTag::create('Body', 'name')->withError($exception);
36+
37+
self::assertSame('name', $tag->getName());
38+
self::assertSame('@name Body', $tag->render());
39+
self::assertSame($exception, $tag->getException());
40+
}
41+
}

0 commit comments

Comments
 (0)