diff --git a/src/Generators/Generator.php b/src/Generators/Generator.php index 483cae800f..9d27ace938 100644 --- a/src/Generators/Generator.php +++ b/src/Generators/Generator.php @@ -14,6 +14,7 @@ use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Autoload; +use PHP_CodeSniffer\Util; abstract class Generator { @@ -45,16 +46,17 @@ public function __construct(Ruleset $ruleset) $this->ruleset = $ruleset; foreach ($ruleset->sniffs as $className => $sniffClass) { - $file = Autoload::getLoadedFileName($className); - $docFile = str_replace( + $file = Autoload::getLoadedFileName($className); + $docFile = str_replace( DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.'Docs'.DIRECTORY_SEPARATOR, $file ); - $docFile = str_replace('Sniff.php', 'Standard.xml', $docFile); + $docFile = str_replace('Sniff.php', 'Standard.xml', $docFile); + $sniffCode = Util\Common::getSniffCode($className); if (is_file($docFile) === true) { - $this->docFiles[] = $docFile; + $this->docFiles[$sniffCode] = $docFile; } } @@ -77,6 +79,28 @@ protected function getTitle(\DOMNode $doc) }//end getTitle() + /** + * Retrieves the code of the sniff from the DOMNode supplied. + * + * @param \DOMNode $doc The DOMNode object for the sniff. + * It represents the "documentation" tag in the XML + * standard file. + * + * @return string + */ + protected function getSniffCode(\DOMNode $doc) + { + $code = $doc->getAttribute('code'); + + if (empty($code) === true) { + return ''; + } + + return $code; + + }//end getSniffCode() + + /** * Generates the documentation for a standard. * @@ -89,10 +113,15 @@ protected function getTitle(\DOMNode $doc) */ public function generate() { - foreach ($this->docFiles as $file) { + foreach ($this->docFiles as $code => $file) { $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); + + if (empty($documentation->getAttribute('code')) === true) { + $documentation->setAttribute('code', $code); + } + $this->processSniff($documentation); } diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index 55acd9e172..fba43c6e71 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -31,10 +31,15 @@ public function generate() $this->printHeader(); $this->printToc(); - foreach ($this->docFiles as $file) { + foreach ($this->docFiles as $code => $file) { $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); + + if (empty($documentation->getAttribute('code')) === true) { + $documentation->setAttribute('code', $code); + } + $this->processSniff($documentation); } @@ -194,6 +199,11 @@ public function processSniff(\DOMNode $doc) echo ' '.PHP_EOL; echo "

$title

".PHP_EOL; + $code = $this->getSniffCode($doc); + if (empty($code) === false) { + echo " $code".PHP_EOL; + } + foreach ($doc->childNodes as $node) { if ($node->nodeName === 'standard') { $this->printTextBlock($node); diff --git a/src/Generators/Markdown.php b/src/Generators/Markdown.php index 23bed27e20..39bf20b269 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -26,10 +26,15 @@ public function generate() ob_start(); $this->printHeader(); - foreach ($this->docFiles as $file) { + foreach ($this->docFiles as $code => $file) { $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); + + if (empty($documentation->getAttribute('code')) === true) { + $documentation->setAttribute('code', $code); + } + $this->processSniff($documentation); } @@ -86,6 +91,11 @@ protected function processSniff(\DOMNode $doc) $title = $this->getTitle($doc); echo PHP_EOL."## $title".PHP_EOL; + $code = $this->getSniffCode($doc); + if (empty($code) === false) { + echo PHP_EOL."`$code`".PHP_EOL.PHP_EOL; + } + foreach ($doc->childNodes as $node) { if ($node->nodeName === 'standard') { $this->printTextBlock($node); diff --git a/src/Generators/Text.php b/src/Generators/Text.php index 7ec95ec9c7..d67ae521b0 100644 --- a/src/Generators/Text.php +++ b/src/Generators/Text.php @@ -28,6 +28,11 @@ public function processSniff(\DOMNode $doc) { $this->printTitle($doc); + $code = $this->getSniffCode($doc); + if (empty($code) === false) { + echo "$code".PHP_EOL.PHP_EOL; + } + foreach ($doc->childNodes as $node) { if ($node->nodeName === 'standard') { $this->printTextBlock($node);