From 84d0a5f6960dd5b93b2bf779ac6dbf9aa6b216cd Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 5 Nov 2024 01:37:34 +0100 Subject: [PATCH 1/3] Generators/HTML::getFormattedHeader(): minor simplification Use a single call to `sprintf()` instead of lots of string concatenation. --- src/Generators/HTML.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index a1093a1660..a8686d5332 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -149,15 +149,20 @@ protected function printHeader() protected function getFormattedHeader() { $standard = $this->ruleset->name; - $output = ''.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= " $standard Coding Standards".PHP_EOL; - $output .= ' '.str_replace("\n", PHP_EOL, self::STYLESHEET).PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= "

$standard Coding Standards

".PHP_EOL; - - return $output; + $output = sprintf( + ' + + %1$s Coding Standards + %2$s + + +

%1$s Coding Standards

', + $standard, + self::STYLESHEET + ); + + // Use the correct line endings based on the OS. + return str_replace("\n", PHP_EOL, $output).PHP_EOL; }//end getFormattedHeader() From 8f9f5d22cfb59616c4ad568050942d53a1d5d051 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 6 Nov 2024 15:41:22 +0100 Subject: [PATCH 2/3] Generators/HTML::getFormattedFooter(): minor simplification Use a single call to `sprintf()` instead of lots of string concatenation. --- src/Generators/HTML.php | 17 +++++++++-------- tests/Core/Generators/Fixtures/HTMLDouble.php | 12 +++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index a8686d5332..0b0a40e6da 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -245,16 +245,17 @@ protected function getFormattedFooter() // Turn off errors so we don't get timezone warnings if people // don't have their timezone set. $errorLevel = error_reporting(0); - $output = '
'; - $output .= 'Documentation generated on '.date('r'); - $output .= ' by PHP_CodeSniffer '.Config::VERSION.''; - $output .= '
'.PHP_EOL; + $output = sprintf( + '
Documentation generated on %s by PHP_CodeSniffer %s
+ +', + date('r'), + Config::VERSION + ); error_reporting($errorLevel); - $output .= ' '.PHP_EOL; - $output .= ''.PHP_EOL; - - return $output; + // Use the correct line endings based on the OS. + return str_replace("\n", PHP_EOL, $output).PHP_EOL; }//end getFormattedFooter() diff --git a/tests/Core/Generators/Fixtures/HTMLDouble.php b/tests/Core/Generators/Fixtures/HTMLDouble.php index e3d797c118..8b737fb7c4 100644 --- a/tests/Core/Generators/Fixtures/HTMLDouble.php +++ b/tests/Core/Generators/Fixtures/HTMLDouble.php @@ -20,14 +20,12 @@ class HTMLDouble extends HTML */ protected function getFormattedFooter() { - $output = '
'; - $output .= 'Documentation generated on #REDACTED#'; - $output .= ' by PHP_CodeSniffer #VERSION#'; - $output .= '
'.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= ''.PHP_EOL; + $output ='
Documentation generated on #REDACTED# by PHP_CodeSniffer #VERSION#
+ +'; - return $output; + // Use the correct line endings based on the OS. + return str_replace("\n", PHP_EOL, $output).PHP_EOL; } /** From 6e5eabcf89dd4e43d6a0690fa9029098d8186b40 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 6 Nov 2024 16:13:37 +0100 Subject: [PATCH 3/3] Generators/HTML::getFormattedToc(): minor simplification Use `sprintf()` on a template string. --- src/Generators/HTML.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index 0b0a40e6da..51721afa4b 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -202,12 +202,14 @@ protected function getFormattedToc() $output = '

Table of Contents

'.PHP_EOL; $output .= '
    '.PHP_EOL; + $listItemTemplate = '
  • %s
  • '.PHP_EOL; + foreach ($this->docFiles as $file) { $doc = new DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); $title = $this->getTitle($documentation); - $output .= '
  • '.$title.'
  • '.PHP_EOL; + $output .= sprintf($listItemTemplate, str_replace(' ', '-', $title), $title); } $output .= '
'.PHP_EOL;