From 0bc8a3f1b6e8bf47745211b251b87c08a1d5d88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Hernik?= Date: Sun, 28 Jan 2024 22:19:17 +0100 Subject: [PATCH 1/5] add sniff codes to docFiles array --- src/Generators/Generator.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Generators/Generator.php b/src/Generators/Generator.php index 483cae800f..b6a201f244 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 { @@ -52,9 +53,10 @@ public function __construct(Ruleset $ruleset) $file ); $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; } } @@ -89,7 +91,7 @@ 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); From 5f1b02f1d5a373ddacdfcbe4ef7ea231e9a920dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Hernik?= Date: Sun, 28 Jan 2024 22:20:34 +0100 Subject: [PATCH 2/5] detect sniff code from documentation attribute --- src/Generators/Generator.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Generators/Generator.php b/src/Generators/Generator.php index b6a201f244..19e860eab4 100644 --- a/src/Generators/Generator.php +++ b/src/Generators/Generator.php @@ -78,6 +78,22 @@ 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'); + + return ! empty($code) ? $code : ''; + }//end getSniffCode() + /** * Generates the documentation for a standard. From 1c957f748734a23b466a9ae1a2185626b9826e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Hernik?= Date: Sun, 28 Jan 2024 22:22:24 +0100 Subject: [PATCH 3/5] set sniff code when not set in the docs --- src/Generators/Generator.php | 3 +++ src/Generators/HTML.php | 5 ++++- src/Generators/Markdown.php | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Generators/Generator.php b/src/Generators/Generator.php index 19e860eab4..b42c624dd8 100644 --- a/src/Generators/Generator.php +++ b/src/Generators/Generator.php @@ -111,6 +111,9 @@ public function generate() $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); + if (empty($documentation->getAttribute('code'))) { + $documentation->setAttribute('code', $code); + } $this->processSniff($documentation); } diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index 55acd9e172..908c37803a 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -31,10 +31,13 @@ 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'))) { + $documentation->setAttribute('code', $code); + } $this->processSniff($documentation); } diff --git a/src/Generators/Markdown.php b/src/Generators/Markdown.php index 23bed27e20..427d84baef 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -26,10 +26,13 @@ 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'))) { + $documentation->setAttribute('code', $code); + } $this->processSniff($documentation); } From e3ddb98da1e0f95bb08bfefbaa32f2f7693cf307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Hernik?= Date: Sun, 28 Jan 2024 22:22:49 +0100 Subject: [PATCH 4/5] display sniff codes in reports --- src/Generators/HTML.php | 5 +++++ src/Generators/Markdown.php | 5 +++++ src/Generators/Text.php | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index 908c37803a..d4e0de3a04 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -197,6 +197,11 @@ public function processSniff(\DOMNode $doc) echo ' '.PHP_EOL; echo "

$title

".PHP_EOL; + $code = $this->getSniffCode($doc); + if (! empty($code)) { + 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 427d84baef..0ef4e2ceb4 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -89,6 +89,11 @@ protected function processSniff(\DOMNode $doc) $title = $this->getTitle($doc); echo PHP_EOL."## $title".PHP_EOL; + $code = $this->getSniffCode($doc); + if (! empty($code)) { + 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..3a9a50e0d5 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)) { + echo "$code".PHP_EOL.PHP_EOL; + } + foreach ($doc->childNodes as $node) { if ($node->nodeName === 'standard') { $this->printTextBlock($node); From de1633e75a8606256f7a356bfaa5e462e70af04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Hernik?= Date: Sun, 28 Jan 2024 22:44:27 +0100 Subject: [PATCH 5/5] adjust coding standards --- src/Generators/Generator.php | 18 +++++++++++++----- src/Generators/HTML.php | 6 ++++-- src/Generators/Markdown.php | 6 ++++-- src/Generators/Text.php | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Generators/Generator.php b/src/Generators/Generator.php index b42c624dd8..9d27ace938 100644 --- a/src/Generators/Generator.php +++ b/src/Generators/Generator.php @@ -46,13 +46,13 @@ 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) { @@ -78,6 +78,7 @@ protected function getTitle(\DOMNode $doc) }//end getTitle() + /** * Retrieves the code of the sniff from the DOMNode supplied. * @@ -91,7 +92,12 @@ protected function getSniffCode(\DOMNode $doc) { $code = $doc->getAttribute('code'); - return ! empty($code) ? $code : ''; + if (empty($code) === true) { + return ''; + } + + return $code; + }//end getSniffCode() @@ -111,9 +117,11 @@ public function generate() $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); - if (empty($documentation->getAttribute('code'))) { + + 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 d4e0de3a04..fba43c6e71 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -35,9 +35,11 @@ public function generate() $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); - if (empty($documentation->getAttribute('code'))) { + + if (empty($documentation->getAttribute('code')) === true) { $documentation->setAttribute('code', $code); } + $this->processSniff($documentation); } @@ -198,7 +200,7 @@ public function processSniff(\DOMNode $doc) echo "

$title

".PHP_EOL; $code = $this->getSniffCode($doc); - if (! empty($code)) { + if (empty($code) === false) { echo " $code".PHP_EOL; } diff --git a/src/Generators/Markdown.php b/src/Generators/Markdown.php index 0ef4e2ceb4..39bf20b269 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -30,9 +30,11 @@ public function generate() $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); - if (empty($documentation->getAttribute('code'))) { + + if (empty($documentation->getAttribute('code')) === true) { $documentation->setAttribute('code', $code); } + $this->processSniff($documentation); } @@ -90,7 +92,7 @@ protected function processSniff(\DOMNode $doc) echo PHP_EOL."## $title".PHP_EOL; $code = $this->getSniffCode($doc); - if (! empty($code)) { + if (empty($code) === false) { echo PHP_EOL."`$code`".PHP_EOL.PHP_EOL; } diff --git a/src/Generators/Text.php b/src/Generators/Text.php index 3a9a50e0d5..d67ae521b0 100644 --- a/src/Generators/Text.php +++ b/src/Generators/Text.php @@ -29,7 +29,7 @@ public function processSniff(\DOMNode $doc) $this->printTitle($doc); $code = $this->getSniffCode($doc); - if (! empty($code)) { + if (empty($code) === false) { echo "$code".PHP_EOL.PHP_EOL; }