/vendor/psy/psysh/src/Psy/Formatter/DocblockFormatter.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 168 lines · 86 code · 23 blank · 59 comment · 7 complexity · d75a9ef8115c4b022e0284ecf6b1af71 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2015 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\Util\Docblock;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. /**
  14. * A pretty-printer for docblocks.
  15. */
  16. class DocblockFormatter implements Formatter
  17. {
  18. private static $vectorParamTemplates = array(
  19. 'type' => 'info',
  20. 'var' => 'strong',
  21. );
  22. /**
  23. * Format a docblock.
  24. *
  25. * @param \Reflector $reflector
  26. *
  27. * @return string Formatted docblock
  28. */
  29. public static function format(\Reflector $reflector)
  30. {
  31. $docblock = new Docblock($reflector);
  32. $chunks = array();
  33. if (!empty($docblock->desc)) {
  34. $chunks[] = '<comment>Description:</comment>';
  35. $chunks[] = self::indent(OutputFormatter::escape($docblock->desc), ' ');
  36. $chunks[] = '';
  37. }
  38. if (!empty($docblock->tags)) {
  39. foreach ($docblock::$vectors as $name => $vector) {
  40. if (isset($docblock->tags[$name])) {
  41. $chunks[] = sprintf('<comment>%s:</comment>', self::inflect($name));
  42. $chunks[] = self::formatVector($vector, $docblock->tags[$name]);
  43. $chunks[] = '';
  44. }
  45. }
  46. $tags = self::formatTags(array_keys($docblock::$vectors), $docblock->tags);
  47. if (!empty($tags)) {
  48. $chunks[] = $tags;
  49. $chunks[] = '';
  50. }
  51. }
  52. return rtrim(implode("\n", $chunks));
  53. }
  54. /**
  55. * Format a docblock vector, for example, `@throws`, `@param`, or `@return`.
  56. *
  57. * @see DocBlock::$vectors
  58. *
  59. * @param array $vector
  60. * @param array $lines
  61. *
  62. * @return string
  63. */
  64. private static function formatVector(array $vector, array $lines)
  65. {
  66. $template = array(' ');
  67. foreach ($vector as $type) {
  68. $max = 0;
  69. foreach ($lines as $line) {
  70. $chunk = $line[$type];
  71. $cur = empty($chunk) ? 0 : strlen($chunk) + 1;
  72. if ($cur > $max) {
  73. $max = $cur;
  74. }
  75. }
  76. $template[] = self::getVectorParamTemplate($type, $max);
  77. }
  78. $template = implode(' ', $template);
  79. return implode("\n", array_map(function ($line) use ($template) {
  80. $escaped = array_map(array('Symfony\Component\Console\Formatter\OutputFormatter', 'escape'), $line);
  81. return rtrim(vsprintf($template, $escaped));
  82. }, $lines));
  83. }
  84. /**
  85. * Format docblock tags.
  86. *
  87. * @param array $skip Tags to exclude
  88. * @param array $tags Tags to format
  89. *
  90. * @return string formatted tags
  91. */
  92. private static function formatTags(array $skip, array $tags)
  93. {
  94. $chunks = array();
  95. foreach ($tags as $name => $values) {
  96. if (in_array($name, $skip)) {
  97. continue;
  98. }
  99. foreach ($values as $value) {
  100. $chunks[] = sprintf('<comment>%s%s</comment> %s', self::inflect($name), empty($value) ? '' : ':', OutputFormatter::escape($value));
  101. }
  102. $chunks[] = '';
  103. }
  104. return implode("\n", $chunks);
  105. }
  106. /**
  107. * Get a docblock vector template.
  108. *
  109. * @param string $type Vector type
  110. * @param int $max Pad width
  111. *
  112. * @return string
  113. */
  114. private static function getVectorParamTemplate($type, $max)
  115. {
  116. if (!isset(self::$vectorParamTemplates[$type])) {
  117. return sprintf('%%-%ds', $max);
  118. }
  119. return sprintf('<%s>%%-%ds</%s>', self::$vectorParamTemplates[$type], $max, self::$vectorParamTemplates[$type]);
  120. }
  121. /**
  122. * Indent a string.
  123. *
  124. * @param string $text String to indent
  125. * @param string $indent (default: ' ')
  126. *
  127. * @return string
  128. */
  129. private static function indent($text, $indent = ' ')
  130. {
  131. return $indent . str_replace("\n", "\n" . $indent, $text);
  132. }
  133. /**
  134. * Convert underscored or whitespace separated words into sentence case.
  135. *
  136. * @param string $text
  137. *
  138. * @return string
  139. */
  140. private static function inflect($text)
  141. {
  142. $words = trim(preg_replace('/[\s_-]+/', ' ', preg_replace('/([a-z])([A-Z])/', '$1 $2', $text)));
  143. return implode(' ', array_map('ucfirst', explode(' ', $words)));
  144. }
  145. }