PageRenderTime 21ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/laravel_tintuc/vendor/psy/psysh/src/Formatter/CodeFormatter.php

https://gitlab.com/nmhieucoder/laravel_tintuc
PHP | 320 lines | 168 code | 45 blank | 107 comment | 24 complexity | 8edcbb9a4235bce9dbbcf23653f37e4b MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2020 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Formatter;
  11. use Psy\Exception\RuntimeException;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. /**
  14. * A pretty-printer for code.
  15. */
  16. class CodeFormatter implements ReflectorFormatter
  17. {
  18. const LINE_MARKER = ' <urgent>></urgent> ';
  19. const NO_LINE_MARKER = ' ';
  20. const HIGHLIGHT_DEFAULT = 'default';
  21. const HIGHLIGHT_KEYWORD = 'keyword';
  22. const HIGHLIGHT_PUBLIC = 'public';
  23. const HIGHLIGHT_PROTECTED = 'protected';
  24. const HIGHLIGHT_PRIVATE = 'private';
  25. const HIGHLIGHT_CONST = 'const';
  26. const HIGHLIGHT_NUMBER = 'number';
  27. const HIGHLIGHT_STRING = 'string';
  28. const HIGHLIGHT_COMMENT = 'comment';
  29. const HIGHLIGHT_INLINE_HTML = 'inline_html';
  30. private static $tokenMap = [
  31. // Not highlighted
  32. \T_OPEN_TAG => self::HIGHLIGHT_DEFAULT,
  33. \T_OPEN_TAG_WITH_ECHO => self::HIGHLIGHT_DEFAULT,
  34. \T_CLOSE_TAG => self::HIGHLIGHT_DEFAULT,
  35. \T_STRING => self::HIGHLIGHT_DEFAULT,
  36. \T_VARIABLE => self::HIGHLIGHT_DEFAULT,
  37. \T_NS_SEPARATOR => self::HIGHLIGHT_DEFAULT,
  38. // Visibility
  39. \T_PUBLIC => self::HIGHLIGHT_PUBLIC,
  40. \T_PROTECTED => self::HIGHLIGHT_PROTECTED,
  41. \T_PRIVATE => self::HIGHLIGHT_PRIVATE,
  42. // Constants
  43. \T_DIR => self::HIGHLIGHT_CONST,
  44. \T_FILE => self::HIGHLIGHT_CONST,
  45. \T_METHOD_C => self::HIGHLIGHT_CONST,
  46. \T_NS_C => self::HIGHLIGHT_CONST,
  47. \T_LINE => self::HIGHLIGHT_CONST,
  48. \T_CLASS_C => self::HIGHLIGHT_CONST,
  49. \T_FUNC_C => self::HIGHLIGHT_CONST,
  50. \T_TRAIT_C => self::HIGHLIGHT_CONST,
  51. // Types
  52. \T_DNUMBER => self::HIGHLIGHT_NUMBER,
  53. \T_LNUMBER => self::HIGHLIGHT_NUMBER,
  54. \T_ENCAPSED_AND_WHITESPACE => self::HIGHLIGHT_STRING,
  55. \T_CONSTANT_ENCAPSED_STRING => self::HIGHLIGHT_STRING,
  56. // Comments
  57. \T_COMMENT => self::HIGHLIGHT_COMMENT,
  58. \T_DOC_COMMENT => self::HIGHLIGHT_COMMENT,
  59. // @todo something better here?
  60. \T_INLINE_HTML => self::HIGHLIGHT_INLINE_HTML,
  61. ];
  62. /**
  63. * Format the code represented by $reflector for shell output.
  64. *
  65. * @param \Reflector $reflector
  66. * @param string|null $colorMode (deprecated and ignored)
  67. *
  68. * @return string formatted code
  69. */
  70. public static function format(\Reflector $reflector, $colorMode = null)
  71. {
  72. if (self::isReflectable($reflector)) {
  73. if ($code = @\file_get_contents($reflector->getFileName())) {
  74. return self::formatCode($code, self::getStartLine($reflector), $reflector->getEndLine());
  75. }
  76. }
  77. throw new RuntimeException('Source code unavailable');
  78. }
  79. /**
  80. * Format code for shell output.
  81. *
  82. * Optionally, restrict by $startLine and $endLine line numbers, or pass $markLine to add a line marker.
  83. *
  84. * @param string $code
  85. * @param int $startLine
  86. * @param int|null $endLine
  87. * @param int|null $markLine
  88. *
  89. * @return string formatted code
  90. */
  91. public static function formatCode($code, $startLine = 1, $endLine = null, $markLine = null)
  92. {
  93. $spans = self::tokenizeSpans($code);
  94. $lines = self::splitLines($spans, $startLine, $endLine);
  95. $lines = self::formatLines($lines);
  96. $lines = self::numberLines($lines, $markLine);
  97. return \implode('', \iterator_to_array($lines));
  98. }
  99. /**
  100. * Get the start line for a given Reflector.
  101. *
  102. * Tries to incorporate doc comments if possible.
  103. *
  104. * This is typehinted as \Reflector but we've narrowed the input via self::isReflectable already.
  105. *
  106. * @param \ReflectionClass|\ReflectionFunctionAbstract $reflector
  107. *
  108. * @return int
  109. */
  110. private static function getStartLine(\Reflector $reflector)
  111. {
  112. $startLine = $reflector->getStartLine();
  113. if ($docComment = $reflector->getDocComment()) {
  114. $startLine -= \preg_match_all('/(\r\n?|\n)/', $docComment) + 1;
  115. }
  116. return \max($startLine, 1);
  117. }
  118. /**
  119. * Split code into highlight spans.
  120. *
  121. * Tokenize via \token_get_all, then map these tokens to internal highlight types, combining
  122. * adjacent spans of the same highlight type.
  123. *
  124. * @todo consider switching \token_get_all() out for PHP-Parser-based formatting at some point.
  125. *
  126. * @param string $code
  127. *
  128. * @return \Generator [$spanType, $spanText] highlight spans
  129. */
  130. private static function tokenizeSpans($code)
  131. {
  132. $spanType = null;
  133. $buffer = '';
  134. foreach (\token_get_all($code) as $token) {
  135. $nextType = self::nextHighlightType($token, $spanType);
  136. $spanType = $spanType ?: $nextType;
  137. if ($spanType !== $nextType) {
  138. yield [$spanType, $buffer];
  139. $spanType = $nextType;
  140. $buffer = '';
  141. }
  142. $buffer .= \is_array($token) ? $token[1] : $token;
  143. }
  144. if ($spanType !== null && $buffer !== '') {
  145. yield [$spanType, $buffer];
  146. }
  147. }
  148. /**
  149. * Given a token and the current highlight span type, compute the next type.
  150. *
  151. * @param array|string $token \token_get_all token
  152. * @param string|null $currentType
  153. *
  154. * @return string|null
  155. */
  156. private static function nextHighlightType($token, $currentType)
  157. {
  158. if ($token === '"') {
  159. return self::HIGHLIGHT_STRING;
  160. }
  161. if (\is_array($token)) {
  162. if ($token[0] === \T_WHITESPACE) {
  163. return $currentType;
  164. }
  165. if (\array_key_exists($token[0], self::$tokenMap)) {
  166. return self::$tokenMap[$token[0]];
  167. }
  168. }
  169. return self::HIGHLIGHT_KEYWORD;
  170. }
  171. /**
  172. * Group highlight spans into an array of lines.
  173. *
  174. * Optionally, restrict by start and end line numbers.
  175. *
  176. * @param \Generator $spans as [$spanType, $spanText] pairs
  177. * @param int $startLine
  178. * @param int|null $endLine
  179. *
  180. * @return \Generator lines, each an array of [$spanType, $spanText] pairs
  181. */
  182. private static function splitLines(\Generator $spans, $startLine = 1, $endLine = null)
  183. {
  184. $lineNum = 1;
  185. $buffer = [];
  186. foreach ($spans as list($spanType, $spanText)) {
  187. foreach (\preg_split('/(\r\n?|\n)/', $spanText) as $index => $spanLine) {
  188. if ($index > 0) {
  189. if ($lineNum >= $startLine) {
  190. yield $lineNum => $buffer;
  191. }
  192. $lineNum++;
  193. $buffer = [];
  194. if ($endLine !== null && $lineNum > $endLine) {
  195. return;
  196. }
  197. }
  198. if ($spanLine !== '') {
  199. $buffer[] = [$spanType, $spanLine];
  200. }
  201. }
  202. }
  203. if (!empty($buffer)) {
  204. yield $lineNum => $buffer;
  205. }
  206. }
  207. /**
  208. * Format lines of highlight spans for shell output.
  209. *
  210. * @param \Generator $spanLines lines, each an array of [$spanType, $spanText] pairs
  211. *
  212. * @return \Generator Formatted lines
  213. */
  214. private static function formatLines(\Generator $spanLines)
  215. {
  216. foreach ($spanLines as $lineNum => $spanLine) {
  217. $line = '';
  218. foreach ($spanLine as list($spanType, $spanText)) {
  219. if ($spanType === self::HIGHLIGHT_DEFAULT) {
  220. $line .= OutputFormatter::escape($spanText);
  221. } else {
  222. $line .= \sprintf('<%s>%s</%s>', $spanType, OutputFormatter::escape($spanText), $spanType);
  223. }
  224. }
  225. yield $lineNum => $line.\PHP_EOL;
  226. }
  227. }
  228. /**
  229. * Prepend line numbers to formatted lines.
  230. *
  231. * Lines must be in an associative array with the correct keys in order to be numbered properly.
  232. *
  233. * Optionally, pass $markLine to add a line marker.
  234. *
  235. * @param \Generator $lines Formatted lines
  236. * @param int|null $markLine
  237. *
  238. * @return \Generator Numbered, formatted lines
  239. */
  240. private static function numberLines(\Generator $lines, $markLine = null)
  241. {
  242. $lines = \iterator_to_array($lines);
  243. // Figure out how much space to reserve for line numbers.
  244. \end($lines);
  245. $pad = \strlen(\key($lines));
  246. // If $markLine is before or after our line range, don't bother reserving space for the marker.
  247. if ($markLine !== null) {
  248. if ($markLine > \key($lines)) {
  249. $markLine = null;
  250. }
  251. \reset($lines);
  252. if ($markLine < \key($lines)) {
  253. $markLine = null;
  254. }
  255. }
  256. foreach ($lines as $lineNum => $line) {
  257. $mark = '';
  258. if ($markLine !== null) {
  259. $mark = ($markLine === $lineNum) ? self::LINE_MARKER : self::NO_LINE_MARKER;
  260. }
  261. yield \sprintf("%s<aside>%${pad}s</aside>: %s", $mark, $lineNum, $line);
  262. }
  263. }
  264. /**
  265. * Check whether a Reflector instance is reflectable by this formatter.
  266. *
  267. * @param \Reflector $reflector
  268. *
  269. * @return bool
  270. */
  271. private static function isReflectable(\Reflector $reflector)
  272. {
  273. return ($reflector instanceof \ReflectionClass || $reflector instanceof \ReflectionFunctionAbstract) && \is_file($reflector->getFileName());
  274. }
  275. }