Completed
Pull Request — master (#171)
by
unknown
03:16
created

Description::getBodyTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of phpDocumentor.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright 2010-2018 Mike van Riel<[email protected]>
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Reflection\DocBlock;
15
16
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
17
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
18
19
/**
20
 * Object representing to description for a DocBlock.
21
 *
22
 * A Description object can consist of plain text but can also include tags. A Description Formatter can then combine
23
 * a body template with sprintf-style placeholders together with formatted tags in order to reconstitute a complete
24
 * description text using the format that you would prefer.
25
 *
26
 * Because parsing a Description text can be a verbose process this is handled by the {@see DescriptionFactory}. It is
27
 * thus recommended to use that to create a Description object, like this:
28
 *
29
 *     $description = $descriptionFactory->create('This is a {@see Description}', $context);
30
 *
31
 * The description factory will interpret the given body and create a body template and list of tags from them, and pass
32
 * that onto the constructor if this class.
33
 *
34
 * > The $context variable is a class of type {@see \phpDocumentor\Reflection\Types\Context} and contains the namespace
35
 * > and the namespace aliases that apply to this DocBlock. These are used by the Factory to resolve and expand partial
36
 * > type names and FQSENs.
37
 *
38
 * If you do not want to use the DescriptionFactory you can pass a body template and tag listing like this:
39
 *
40
 *     $description = new Description(
41
 *         'This is a %1$s',
42
 *         [ new See(new Fqsen('\phpDocumentor\Reflection\DocBlock\Description')) ]
43
 *     );
44
 *
45
 * It is generally recommended to use the Factory as that will also apply escaping rules, while the Description object
46
 * is mainly responsible for rendering.
47
 *
48
 * @see DescriptionFactory to create a new Description.
49
 * @see Description\Formatter for the formatting of the body and tags.
50
 */
51
class Description
52
{
53
    /** @var string */
54
    private $bodyTemplate;
55
56
    /** @var Tag[] */
57
    private $tags;
58
59
    /**
60
     * Initializes a Description with its body (template) and a listing of the tags used in the body template.
61
     *
62
     * @param Tag[] $tags
63
     */
64
    public function __construct(string $bodyTemplate, array $tags = [])
65 4
    {
66
        $this->bodyTemplate = $bodyTemplate;
67 4
        $this->tags = $tags;
68
    }
69 3
    
70 3
    /**
71 3
     * Returns the body template
72
     * @return string
73
     */
74
    public function getBodyTemplate(): string
75
    {
76
        return $this->bodyTemplate;
77
    }
78 1
79
    /**
80 1
     * Returns the tags for this DocBlock.
81
     *
82
     * @return Tag[]
83
     */
84
    public function getTags(): array
85
    {
86
        return $this->tags;
87
    }
88
89
    /**
90
     * Renders this description as a string where the provided formatter will format the tags in the expected string
91 3
     * format.
92
     */
93 3
    public function render(?Formatter $formatter = null): string
94 3
    {
95
        if ($formatter === null) {
96
            $formatter = new PassthroughFormatter();
97 3
        }
98 3
99 3
        $tags = [];
100
        foreach ($this->tags as $tag) {
101
            $tags[] = '{' . $formatter->format($tag) . '}';
102 3
        }
103
104
        return vsprintf($this->bodyTemplate, $tags);
105
    }
106
107
    /**
108
     * Returns a plain string representation of this description.
109
     */
110 2
    public function __toString(): string
111
    {
112 2
        return $this->render();
113
    }
114
}
115