Skip to content

show sniff codes in the docs #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/Generators/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Autoload;
use PHP_CodeSniffer\Util;

abstract class Generator
{
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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.
*
Expand All @@ -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);
}

Expand Down
12 changes: 11 additions & 1 deletion src/Generators/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -194,6 +199,11 @@ public function processSniff(\DOMNode $doc)
echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL;
echo " <h2>$title</h2>".PHP_EOL;

$code = $this->getSniffCode($doc);
if (empty($code) === false) {
echo " <code>$code</code>".PHP_EOL;
}

foreach ($doc->childNodes as $node) {
if ($node->nodeName === 'standard') {
$this->printTextBlock($node);
Expand Down
12 changes: 11 additions & 1 deletion src/Generators/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/Generators/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down