Completed
Push — master ( 43f90a...f256f0 )
by Jaap
21:44 queued 11:35
created

InvalidTag::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace phpDocumentor\Reflection\DocBlock\Tags;
6
7
use phpDocumentor\Reflection\DocBlock\Tag;
8
use Throwable;
9
10
/**
11
 * This class represents an exception during the tag creation
12
 *
13
 * Since the internals of the library are relaying on the correct syntax of a docblock
14
 * we cannot simply throw exceptions at all time because the exceptions will break the creation of a
15
 * docklock. Just silently ignore the exceptions is not an option because the user as an issue to fix.
16
 *
17
 * This tag holds that error information until a using application is able to display it. The object wil just behave
18
 * like any normal tag. So the normal application flow will not break.
19
 */
20
final class InvalidTag implements Tag
21
{
22
    /** @var string */
23
    private $name;
24
25
    /** @var string */
26
    private $body;
27
28
    /** @var Throwable|null */
29
    private $throwable;
30
31
    private function __construct(string $name, string $body)
32
    {
33
        $this->name = $name;
34
        $this->body = $body;
35
    }
36
37
    public function getException() : ?Throwable
38
    {
39
        return $this->throwable;
40
    }
41
42
    public function getName() : string
43
    {
44
        return $this->name;
45
    }
46
47
    /**
48
     * @return self
49
     *
50
     * @inheritDoc
51
     */
52
    public static function create(string $body, string $name = '')
53
    {
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;
61
62
        return $tag;
63
    }
64
65
    public function render(?Formatter $formatter = null) : string
66
    {
67
        if ($formatter === null) {
68
            $formatter = new Formatter\PassthroughFormatter();
69
        }
70
71
        return $formatter->format($this);
72
    }
73
74
    public function __toString() : string
75
    {
76
        return $this->body;
77
    }
78
}
79