Completed
Push — master ( fc4fe7...332c96 )
by Mike
03:28
created

Description::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 5
b 0
f 1
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Reflection\DocBlock;
14
15
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
16
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
17
use Webmozart\Assert\Assert;
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 string $bodyTemplate
63
     * @param Tag[] $tags
64
     */
65 3
    public function __construct($bodyTemplate, array $tags = [])
66
    {
67 3
        Assert::string($bodyTemplate);
68
69 2
        $this->bodyTemplate = $bodyTemplate;
70 2
        $this->tags = $tags;
71 2
    }
72
73
    /**
74
     * Renders this description as a string where the provided formatter will format the tags in the expected string
75
     * format.
76
     *
77
     * @param Formatter|null $formatter
78
     *
79
     * @return string
80
     */
81 2
    public function render(Formatter $formatter = null)
82
    {
83 2
        if ($formatter === null) {
84 2
            $formatter = new PassthroughFormatter();
85 2
        }
86
87 2
        $tags = [];
88 2
        foreach ($this->tags as $tag) {
89 2
            $tags[] = '{' . $formatter->format($tag) . '}';
90 2
        }
91 2
        return vsprintf($this->bodyTemplate, $tags);
92
    }
93
94
    /**
95
     * Returns a plain string representation of this description.
96
     *
97
     * @return string
98
     */
99 1
    public function __toString()
100
    {
101 1
        return $this->render();
102
    }
103
}
104