/public_html/lib/pear/PHP/CodeSniffer/DocGenerators/Generator.php

https://github.com/hatone/moodle · PHP · 191 lines · 60 code · 30 blank · 101 comment · 5 complexity · 3f3bef9953722aceb248e6a320b00755 MD5 · raw file

  1. <?php
  2. /**
  3. * The base class for all PHP_CodeSniffer documentation generators.
  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 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @version CVS: $Id: Generator.php,v 1.2 2010/12/14 17:35:53 moodlerobot Exp $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * The base class for all PHP_CodeSniffer documentation generators.
  18. *
  19. * Documentation generators are used to print documentation about code sniffs
  20. * in a standard.
  21. *
  22. * @category PHP
  23. * @package PHP_CodeSniffer
  24. * @author Greg Sherwood <gsherwood@squiz.net>
  25. * @author Marc McIntyre <mmcintyre@squiz.net>
  26. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  27. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  28. * @version Release: 1.1.0
  29. * @link http://pear.php.net/package/PHP_CodeSniffer
  30. */
  31. class PHP_CodeSniffer_DocGenerators_Generator
  32. {
  33. /**
  34. * The name of the coding standard we are generating docs for.
  35. *
  36. * @var string
  37. */
  38. private $_standard = '';
  39. /**
  40. * An array of sniffs that we are limiting the generated docs to.
  41. *
  42. * If this array is empty, docs are generated for all sniffs in the
  43. * supplied coding standard.
  44. *
  45. * @var string
  46. */
  47. private $_sniffs = array();
  48. /**
  49. * Constructs a PHP_CodeSniffer_DocGenerators_Generator object.
  50. *
  51. * @param string $standard The name of the coding standard to generate
  52. * docs for.
  53. * @param array $sniffs An array of sniffs that we are limiting the
  54. * generated docs to.
  55. *
  56. * @see generate()
  57. */
  58. public function __construct($standard, array $sniffs=array())
  59. {
  60. $this->_standard = $standard;
  61. $this->_sniffs = $sniffs;
  62. }//end __construct()
  63. /**
  64. * Retrieves the title of the sniff from the DOMNode supplied.
  65. *
  66. * @param DOMNode $doc The DOMNode object for the sniff.
  67. * It represents the "documentation" tag in the XML
  68. * standard file.
  69. *
  70. * @return string
  71. */
  72. protected function getTitle(DOMNode $doc)
  73. {
  74. return $doc->getAttribute('title');
  75. }//end getTitle()
  76. /**
  77. * Retrieves the name of the standard we are generating docs for.
  78. *
  79. * @return string
  80. */
  81. protected function getStandard()
  82. {
  83. return $this->_standard;
  84. }//end getStandard()
  85. /**
  86. * Generates the documentation for a standard.
  87. *
  88. * It's probably wise for doc generators to override this method so they
  89. * have control over how the docs are produced. Otherwise, the processSniff
  90. * method should be overridden to output content for each sniff.
  91. *
  92. * @return void
  93. * @see processSniff()
  94. */
  95. public function generate()
  96. {
  97. $standardFiles = $this->getStandardFiles();
  98. foreach ($standardFiles as $standard) {
  99. $doc = new DOMDocument();
  100. $doc->load($standard);
  101. $documentation = $doc->getElementsByTagName('documentation')->item(0);
  102. $this->processSniff($documentation);
  103. }
  104. }//end generate()
  105. /**
  106. * Returns a list of paths to XML standard files for all sniffs in a standard.
  107. *
  108. * Any sniffs that do not have an XML standard file are obviously not included
  109. * in the returned array. If documentation is only being generated for some
  110. * sniffs (ie. $this->_sniffs is not empty) then all others sniffs will
  111. * be filtered from the results as well.
  112. *
  113. * @return array(string)
  114. */
  115. protected function getStandardFiles()
  116. {
  117. if (is_dir($this->_standard) === true) {
  118. // This is a custom standard.
  119. $standardDir = $this->_standard;
  120. $standard = basename($this->_standard);
  121. } else {
  122. $standardDir = realpath(dirname(__FILE__).'/../Standards/'.$this->_standard);
  123. $standard = $this->_standard;
  124. }
  125. $sniffs = PHP_CodeSniffer::getSniffFiles($standardDir, $standard);
  126. $standardFiles = array();
  127. foreach ($sniffs as $sniff) {
  128. if (empty($this->_sniffs) === false) {
  129. // We are limiting the docs to certain sniffs only, so filter
  130. // out any unwanted sniffs.
  131. $sniffName = substr($sniff, (strrpos($sniff, '/') + 1));
  132. $sniffName = substr($sniffName, 0, -9);
  133. if (in_array($sniffName, $this->_sniffs) === false) {
  134. continue;
  135. }
  136. }
  137. $standardFile = str_replace(DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.'Docs'.DIRECTORY_SEPARATOR, $sniff);
  138. $standardFile = str_replace('Sniff.php', 'Standard.xml', $standardFile);
  139. if (is_file($standardFile) === true) {
  140. $standardFiles[] = $standardFile;
  141. }
  142. }
  143. return $standardFiles;
  144. }//end getStandardFiles()
  145. /**
  146. * Process the documentation for a single sniff.
  147. *
  148. * Doc generators should override this function to produce output.
  149. *
  150. * @param DOMNode $doc The DOMNode object for the sniff.
  151. * It represents the "documentation" tag in the XML
  152. * standard file.
  153. *
  154. * @return void
  155. * @see generate()
  156. */
  157. protected function processSniff(DOMNode $doc)
  158. {
  159. }//end processSniff()
  160. }//end class
  161. ?>