Debian Codegraph @

lib/3rdParty/composer/phpdocumentor/reflection-docblock/src/DocBlock/DescriptionFactory.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of phpDocumentor.
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * @link http://phpdoc.org
12 */
13
14 namespace phpDocumentor\Reflection\DocBlock;
15
16 use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory;
17 use phpDocumentor\Reflection\Types\Context as TypeContext;
18 use phpDocumentor\Reflection\Utils;
19
20 use function count;
21 use function implode;
22 use function ltrim;
23 use function min;
24 use function str_replace;
25 use function strlen;
26 use function strpos;
27 use function substr;
28 use function trim;
29
30 use const PREG_SPLIT_DELIM_CAPTURE;
31
32 /**
33 * Creates a new Description object given a body of text.
34 *
35 * Descriptions in phpDocumentor are somewhat complex entities as they can contain one or more tags inside their
36 * body that can be replaced with a readable output. The replacing is done by passing a Formatter object to the
37 * Description object's `render` method.
38 *
39 * In addition to the above does a Description support two types of escape sequences:
40 *
41 * 1. `{@}` to escape the `@` character to prevent it from being interpreted as part of a tag, i.e. `{{@}link}`
42 * 2. `{}` to escape the `}` character, this can be used if you want to use the `}` character in the description
43 * of an inline tag.
44 *
45 * If a body consists of multiple lines then this factory will also remove any superfluous whitespace at the beginning
46 * of each line while maintaining any indentation that is used. This will prevent formatting parsers from tripping
47 * over unexpected spaces as can be observed with tag descriptions.
48 */
49 class DescriptionFactory
50 {
51 private Factory $tagFactory;
52
53 /**
54 * Initializes this factory with the means to construct (inline) tags.
55 */
56 public function __construct(Factory $tagFactory)
57 {
58 $this->tagFactory = $tagFactory;
59 }
60
61 /**
62 * Returns the parsed text of this description.
63 */
64 public function create(string $contents, ?TypeContext $context = null): Description
65 {
66 $tokens = $this->lex($contents);
67 $count = count($tokens);
68 $tagCount = 0;
69 $tags = [];
70
71 for ($i = 1; $i < $count; $i += 2) {
72 $tags[] = $this->tagFactory->create($tokens[$i], $context);
73 $tokens[$i] = '%' . ++$tagCount . '$s';
74 }
75
76 //In order to allow "literal" inline tags, the otherwise invalid
77 //sequence "{@}" is changed to "@", and "{}" is changed to "}".
78 //"%" is escaped to "%%" because of vsprintf.
79 //See unit tests for examples.
80 for ($i = 0; $i < $count; $i += 2) {
81 $tokens[$i] = str_replace(['{@}', '{}', '%'], ['@', '}', '%%'], $tokens[$i]);
82 }
83
84 return new Description(implode('', $tokens), $tags);
85 }
86
87 /**
88 * Strips the contents from superfluous whitespace and splits the description into a series of tokens.
89 *
90 * @return string[] A series of tokens of which the description text is composed.
91 */
92 private function lex(string $contents): array
93 {
94 $contents = $this->removeSuperfluousStartingWhitespace($contents);
95
96 // performance optimalization; if there is no inline tag, don't bother splitting it up.
97 if (strpos($contents, '{@') === false) {
98 return [$contents];
99 }
100
101 return Utils::pregSplit(
102 '/\{
103 # "{@}" and "{@*}" are not a valid inline tags. This ensures that we do not treat them as one, but treat
104 # them literally.
105 (?!(?:@\}|@\*\}) )
106 # We want to capture the whole tag line, but without the inline tag delimiters.
107 (\@
108 # Match everything up to the next delimiter.
109 [^{}]*
110 # Nested inline tag content should not be captured, or it will appear in the result separately.
111 (?:
112 # Match nested inline tags.
113 (?:
114 # Because we did not catch the tag delimiters earlier, we must be explicit with them here.
115 # Notice that this also matches "{}", as a way to later introduce it as an escape sequence.
116 \{(?1)?\}
117 |
118 # Make sure we match hanging "{".
119 \{
120 )
121 # Match content after the nested inline tag.
122 [^{}]*
123 )* # If there are more inline tags, match them as well. We use "*" since there may not be any
124 # nested inline tags.
125 )
126 \}/Sux',
127 $contents,
128 0,
129 PREG_SPLIT_DELIM_CAPTURE
130 );
131 }
132
133 /**
134 * Removes the superfluous from a multi-line description.
135 *
136 * When a description has more than one line then it can happen that the second and subsequent lines have an
137 * additional indentation. This is commonly in use with tags like this:
138 *
139 * {@}since 1.1.0 This is an example
140 * description where we have an
141 * indentation in the second and
142 * subsequent lines.
143 *
144 * If we do not normalize the indentation then we have superfluous whitespace on the second and subsequent
145 * lines and this may cause rendering issues when, for example, using a Markdown converter.
146 */
147 private function removeSuperfluousStartingWhitespace(string $contents): string
148 {
149 $lines = Utils::pregSplit("/\r\n?|\n/", $contents);
150
151 // if there is only one line then we don't have lines with superfluous whitespace and
152 // can use the contents as-is
153 if (count($lines) <= 1) {
154 return $contents;
155 }
156
157 // determine how many whitespace characters need to be stripped
158 $startingSpaceCount = 9999999;
159 for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) {
160 // lines with a no length do not count as they are not indented at all
161 if (trim($lines[$i]) === '') {
162 continue;
163 }
164
165 // determine the number of prefixing spaces by checking the difference in line length before and after
166 // an ltrim
167 $startingSpaceCount = min($startingSpaceCount, strlen($lines[$i]) - strlen(ltrim($lines[$i])));
168 }
169
170 // strip the number of spaces from each line
171 if ($startingSpaceCount > 0) {
172 for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) {
173 $lines[$i] = substr($lines[$i], $startingSpaceCount);
174 }
175 }
176
177 return implode("\n", $lines);
178 }
179 }