PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/CodeSniffer/DocGenerators/Text.php

https://github.com/becoded/PHP_CodeSniffer
PHP | 267 lines | 157 code | 42 blank | 68 comment | 35 complexity | 086fce2ad69b919fcf4d58f3b43aa248 MD5 | raw file
  1. <?php
  2. /**
  3. * A doc generator that outputs text-based documentation.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @author Marc McIntyre <mmcintyre@squiz.net>
  11. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. if (class_exists('PHP_CodeSniffer_DocGenerators_Generator', true) === false) {
  16. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_Generator not found');
  17. }
  18. /**
  19. * A doc generator that outputs text-based documentation.
  20. *
  21. * Output is designed to be displayed in a terminal and is wrapped to 100 characters.
  22. *
  23. * @category PHP
  24. * @package PHP_CodeSniffer
  25. * @author Greg Sherwood <gsherwood@squiz.net>
  26. * @author Marc McIntyre <mmcintyre@squiz.net>
  27. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  28. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  29. * @version Release: @package_version@
  30. * @link http://pear.php.net/package/PHP_CodeSniffer
  31. */
  32. class PHP_CodeSniffer_DocGenerators_Text extends PHP_CodeSniffer_DocGenerators_Generator
  33. {
  34. /**
  35. * Process the documentation for a single sniff.
  36. *
  37. * @param DOMNode $doc The DOMNode object for the sniff.
  38. * It represents the "documentation" tag in the XML
  39. * standard file.
  40. *
  41. * @return void
  42. */
  43. public function processSniff(DOMNode $doc)
  44. {
  45. $this->printTitle($doc);
  46. foreach ($doc->childNodes as $node) {
  47. if ($node->nodeName === 'standard') {
  48. $this->printTextBlock($node);
  49. } else if ($node->nodeName === 'code_comparison') {
  50. $this->printCodeComparisonBlock($node);
  51. }
  52. }
  53. }//end processSniff()
  54. /**
  55. * Prints the title area for a single sniff.
  56. *
  57. * @param DOMNode $doc The DOMNode object for the sniff.
  58. * It represents the "documentation" tag in the XML
  59. * standard file.
  60. *
  61. * @return void
  62. */
  63. protected function printTitle(DOMNode $doc)
  64. {
  65. $title = $this->getTitle($doc);
  66. $standard = $this->getStandard();
  67. echo PHP_EOL;
  68. echo str_repeat('-', (strlen("$standard CODING STANDARD: $title") + 4));
  69. echo strtoupper(PHP_EOL."| $standard CODING STANDARD: $title |".PHP_EOL);
  70. echo str_repeat('-', (strlen("$standard CODING STANDARD: $title") + 4));
  71. echo PHP_EOL.PHP_EOL;
  72. }//end printTitle()
  73. /**
  74. * Print a text block found in a standard.
  75. *
  76. * @param DOMNode $node The DOMNode object for the text block.
  77. *
  78. * @return void
  79. */
  80. protected function printTextBlock($node)
  81. {
  82. $text = trim($node->nodeValue);
  83. $text = str_replace('<em>', '*', $text);
  84. $text = str_replace('</em>', '*', $text);
  85. $lines = array();
  86. $tempLine = '';
  87. $words = explode(' ', $text);
  88. foreach ($words as $word) {
  89. if (strlen($tempLine.$word) >= 99) {
  90. if (strlen($tempLine.$word) === 99) {
  91. // Adding the extra space will push us to the edge
  92. // so we are done.
  93. $lines[] = $tempLine.$word;
  94. $tempLine = '';
  95. } else if (strlen($tempLine.$word) === 100) {
  96. // We are already at the edge, so we are done.
  97. $lines[] = $tempLine.$word;
  98. $tempLine = '';
  99. } else {
  100. $lines[] = rtrim($tempLine);
  101. $tempLine = $word.' ';
  102. }
  103. } else {
  104. $tempLine .= $word.' ';
  105. }
  106. }//end foreach
  107. if ($tempLine !== '') {
  108. $lines[] = rtrim($tempLine);
  109. }
  110. echo implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
  111. }//end printTextBlock()
  112. /**
  113. * Print a code comparison block found in a standard.
  114. *
  115. * @param DOMNode $node The DOMNode object for the code comparison block.
  116. *
  117. * @return void
  118. */
  119. protected function printCodeComparisonBlock($node)
  120. {
  121. $codeBlocks = $node->getElementsByTagName('code');
  122. $first = trim($codeBlocks->item(0)->nodeValue);
  123. $firstTitle = $codeBlocks->item(0)->getAttribute('title');
  124. $firstTitleLines = array();
  125. $tempTitle = '';
  126. $words = explode(' ', $firstTitle);
  127. foreach ($words as $word) {
  128. if (strlen($tempTitle.$word) >= 45) {
  129. if (strlen($tempTitle.$word) === 45) {
  130. // Adding the extra space will push us to the edge
  131. // so we are done.
  132. $firstTitleLines[] = $tempTitle.$word;
  133. $tempTitle = '';
  134. } else if (strlen($tempTitle.$word) === 46) {
  135. // We are already at the edge, so we are done.
  136. $firstTitleLines[] = $tempTitle.$word;
  137. $tempTitle = '';
  138. } else {
  139. $firstTitleLines[] = $tempTitle;
  140. $tempTitle = $word;
  141. }
  142. } else {
  143. $tempTitle .= $word.' ';
  144. }
  145. }//end foreach
  146. if ($tempTitle !== '') {
  147. $firstTitleLines[] = $tempTitle;
  148. }
  149. $first = str_replace('<em>', '', $first);
  150. $first = str_replace('</em>', '', $first);
  151. $firstLines = explode("\n", $first);
  152. $second = trim($codeBlocks->item(1)->nodeValue);
  153. $secondTitle = $codeBlocks->item(1)->getAttribute('title');
  154. $secondTitleLines = array();
  155. $tempTitle = '';
  156. $words = explode(' ', $secondTitle);
  157. foreach ($words as $word) {
  158. if (strlen($tempTitle.$word) >= 45) {
  159. if (strlen($tempTitle.$word) === 45) {
  160. // Adding the extra space will push us to the edge
  161. // so we are done.
  162. $secondTitleLines[] = $tempTitle.$word;
  163. $tempTitle = '';
  164. } else if (strlen($tempTitle.$word) === 46) {
  165. // We are already at the edge, so we are done.
  166. $secondTitleLines[] = $tempTitle.$word;
  167. $tempTitle = '';
  168. } else {
  169. $secondTitleLines[] = $tempTitle;
  170. $tempTitle = $word;
  171. }
  172. } else {
  173. $tempTitle .= $word.' ';
  174. }
  175. }//end foreach
  176. if ($tempTitle !== '') {
  177. $secondTitleLines[] = $tempTitle;
  178. }
  179. $second = str_replace('<em>', '', $second);
  180. $second = str_replace('</em>', '', $second);
  181. $secondLines = explode("\n", $second);
  182. $maxCodeLines = max(count($firstLines), count($secondLines));
  183. $maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
  184. echo str_repeat('-', 41);
  185. echo ' CODE COMPARISON ';
  186. echo str_repeat('-', 42).PHP_EOL;
  187. for ($i = 0; $i < $maxTitleLines; $i++) {
  188. if (isset($firstTitleLines[$i]) === true) {
  189. $firstLineText = $firstTitleLines[$i];
  190. } else {
  191. $firstLineText = '';
  192. }
  193. if (isset($secondTitleLines[$i]) === true) {
  194. $secondLineText = $secondTitleLines[$i];
  195. } else {
  196. $secondLineText = '';
  197. }
  198. echo '| ';
  199. echo $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
  200. echo ' | ';
  201. echo $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
  202. echo ' |'.PHP_EOL;
  203. }//end for
  204. echo str_repeat('-', 100).PHP_EOL;
  205. for ($i = 0; $i < $maxCodeLines; $i++) {
  206. if (isset($firstLines[$i]) === true) {
  207. $firstLineText = $firstLines[$i];
  208. } else {
  209. $firstLineText = '';
  210. }
  211. if (isset($secondLines[$i]) === true) {
  212. $secondLineText = $secondLines[$i];
  213. } else {
  214. $secondLineText = '';
  215. }
  216. echo '| ';
  217. echo $firstLineText.str_repeat(' ', (47 - strlen($firstLineText)));
  218. echo '| ';
  219. echo $secondLineText.str_repeat(' ', (48 - strlen($secondLineText)));
  220. echo '|'.PHP_EOL;
  221. }//end for
  222. echo str_repeat('-', 100).PHP_EOL.PHP_EOL;
  223. }//end printCodeComparisonBlock()
  224. }//end class
  225. ?>