Skip to content

Commit 2fa51a5

Browse files
authored
Merge pull request #823 from PHPCSStandards/feature/generator-html-various-simplifications
Generators/HTML: various minor code simplifications
2 parents 894bf72 + 6e5eabc commit 2fa51a5

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/Generators/HTML.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,20 @@ protected function printHeader()
149149
protected function getFormattedHeader()
150150
{
151151
$standard = $this->ruleset->name;
152-
$output = '<html>'.PHP_EOL;
153-
$output .= ' <head>'.PHP_EOL;
154-
$output .= " <title>$standard Coding Standards</title>".PHP_EOL;
155-
$output .= ' '.str_replace("\n", PHP_EOL, self::STYLESHEET).PHP_EOL;
156-
$output .= ' </head>'.PHP_EOL;
157-
$output .= ' <body>'.PHP_EOL;
158-
$output .= " <h1>$standard Coding Standards</h1>".PHP_EOL;
159-
160-
return $output;
152+
$output = sprintf(
153+
'<html>
154+
<head>
155+
<title>%1$s Coding Standards</title>
156+
%2$s
157+
</head>
158+
<body>
159+
<h1>%1$s Coding Standards</h1>',
160+
$standard,
161+
self::STYLESHEET
162+
);
163+
164+
// Use the correct line endings based on the OS.
165+
return str_replace("\n", PHP_EOL, $output).PHP_EOL;
161166

162167
}//end getFormattedHeader()
163168

@@ -197,12 +202,14 @@ protected function getFormattedToc()
197202
$output = ' <h2>Table of Contents</h2>'.PHP_EOL;
198203
$output .= ' <ul class="toc">'.PHP_EOL;
199204

205+
$listItemTemplate = ' <li><a href="#%s">%s</a></li>'.PHP_EOL;
206+
200207
foreach ($this->docFiles as $file) {
201208
$doc = new DOMDocument();
202209
$doc->load($file);
203210
$documentation = $doc->getElementsByTagName('documentation')->item(0);
204211
$title = $this->getTitle($documentation);
205-
$output .= ' <li><a href="#'.str_replace(' ', '-', $title).'">'.$title.'</a></li>'.PHP_EOL;
212+
$output .= sprintf($listItemTemplate, str_replace(' ', '-', $title), $title);
206213
}
207214

208215
$output .= ' </ul>'.PHP_EOL;
@@ -240,16 +247,17 @@ protected function getFormattedFooter()
240247
// Turn off errors so we don't get timezone warnings if people
241248
// don't have their timezone set.
242249
$errorLevel = error_reporting(0);
243-
$output = ' <div class="tag-line">';
244-
$output .= 'Documentation generated on '.date('r');
245-
$output .= ' by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer '.Config::VERSION.'</a>';
246-
$output .= '</div>'.PHP_EOL;
250+
$output = sprintf(
251+
' <div class="tag-line">Documentation generated on %s by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer %s</a></div>
252+
</body>
253+
</html>',
254+
date('r'),
255+
Config::VERSION
256+
);
247257
error_reporting($errorLevel);
248258

249-
$output .= ' </body>'.PHP_EOL;
250-
$output .= '</html>'.PHP_EOL;
251-
252-
return $output;
259+
// Use the correct line endings based on the OS.
260+
return str_replace("\n", PHP_EOL, $output).PHP_EOL;
253261

254262
}//end getFormattedFooter()
255263

tests/Core/Generators/Fixtures/HTMLDouble.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ class HTMLDouble extends HTML
2020
*/
2121
protected function getFormattedFooter()
2222
{
23-
$output = ' <div class="tag-line">';
24-
$output .= 'Documentation generated on #REDACTED#';
25-
$output .= ' by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a>';
26-
$output .= '</div>'.PHP_EOL;
27-
$output .= ' </body>'.PHP_EOL;
28-
$output .= '</html>'.PHP_EOL;
23+
$output =' <div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
24+
</body>
25+
</html>';
2926

30-
return $output;
27+
// Use the correct line endings based on the OS.
28+
return str_replace("\n", PHP_EOL, $output).PHP_EOL;
3129
}
3230

3331
/**

0 commit comments

Comments
 (0)