| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class DescriptionFactory |
| 50 |
{ |
| 51 |
private Factory $tagFactory; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function __construct(Factory $tagFactory) |
| 57 |
{ |
| 58 |
$this->tagFactory = $tagFactory; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private function lex(string $contents): array |
| 93 |
{ |
| 94 |
$contents = $this->removeSuperfluousStartingWhitespace($contents); |
| 95 |
|
| 96 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private function removeSuperfluousStartingWhitespace(string $contents): string |
| 148 |
{ |
| 149 |
$lines = Utils::pregSplit("/\r\n?|\n/", $contents); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if (count($lines) <= 1) { |
| 154 |
return $contents; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
$startingSpaceCount = 9999999; |
| 159 |
for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { |
| 160 |
|
| 161 |
if (trim($lines[$i]) === '') { |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
$startingSpaceCount = min($startingSpaceCount, strlen($lines[$i]) - strlen(ltrim($lines[$i]))); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 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 |
} |