Skip to content

Commit 2088cb0

Browse files
authored
Merge ad8e854 into a97683d
2 parents a97683d + ad8e854 commit 2088cb0

File tree

3 files changed

+180
-139
lines changed

3 files changed

+180
-139
lines changed

src/Generators/HTML.php

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace PHP_CodeSniffer\Generators;
1717

1818
use DOMDocument;
19+
use DOMElement;
1920
use DOMNode;
2021
use PHP_CodeSniffer\Config;
2122

@@ -400,23 +401,11 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
400401
return '';
401402
}
402403

403-
$firstTitle = trim($firstCodeElm->getAttribute('title'));
404-
$firstTitle = str_replace(' ', '  ', $firstTitle);
405-
$first = trim($firstCodeElm->nodeValue);
406-
$first = str_replace('<?php', '&lt;?php', $first);
407-
$first = str_replace("\n", '</br>', $first);
408-
$first = str_replace(' ', '&nbsp;', $first);
409-
$first = str_replace('<em>', '<span class="code-comparison-highlight">', $first);
410-
$first = str_replace('</em>', '</span>', $first);
411-
412-
$secondTitle = trim($secondCodeElm->getAttribute('title'));
413-
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
414-
$second = trim($secondCodeElm->nodeValue);
415-
$second = str_replace('<?php', '&lt;?php', $second);
416-
$second = str_replace("\n", '</br>', $second);
417-
$second = str_replace(' ', '&nbsp;', $second);
418-
$second = str_replace('<em>', '<span class="code-comparison-highlight">', $second);
419-
$second = str_replace('</em>', '</span>', $second);
404+
$firstTitle = $this->formatCodeTitle($firstCodeElm);
405+
$first = $this->formatCodeSample($firstCodeElm);
406+
407+
$secondTitle = $this->formatCodeTitle($secondCodeElm);
408+
$second = $this->formatCodeSample($secondCodeElm);
420409

421410
$titleRow = '';
422411
if ($firstTitle !== '' || $secondTitle !== '') {
@@ -447,4 +436,43 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
447436
}//end getFormattedCodeComparisonBlock()
448437

449438

439+
/**
440+
* Retrieve a code block title and prepare it for output as HTML.
441+
*
442+
* @param \DOMElement $codeElm The DOMElement object for a code block.
443+
*
444+
* @since 3.12.0
445+
*
446+
* @return string
447+
*/
448+
private function formatCodeTitle(DOMElement $codeElm)
449+
{
450+
$title = trim($codeElm->getAttribute('title'));
451+
return str_replace(' ', '&nbsp;&nbsp;', $title);
452+
453+
}//end formatCodeTitle()
454+
455+
456+
/**
457+
* Retrieve a code block contents and prepare it for output as HTML.
458+
*
459+
* @param \DOMElement $codeElm The DOMElement object for a code block.
460+
*
461+
* @since 3.12.0
462+
*
463+
* @return string
464+
*/
465+
private function formatCodeSample(DOMElement $codeElm)
466+
{
467+
$code = (string) $codeElm->nodeValue;
468+
$code = trim($code);
469+
$code = str_replace('<?php', '&lt;?php', $code);
470+
$code = str_replace(["\n", ' '], ['</br>', '&nbsp;'], $code);
471+
$code = str_replace(['<em>', '</em>'], ['<span class="code-comparison-highlight">', '</span>'], $code);
472+
473+
return $code;
474+
475+
}//end formatCodeSample()
476+
477+
450478
}//end class

src/Generators/Markdown.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace PHP_CodeSniffer\Generators;
1313

14+
use DOMElement;
1415
use DOMNode;
1516
use PHP_CodeSniffer\Config;
1617

@@ -249,19 +250,11 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
249250
return '';
250251
}
251252

252-
$firstTitle = trim($firstCodeElm->getAttribute('title'));
253-
$firstTitle = str_replace(' ', '&nbsp;&nbsp;', $firstTitle);
254-
$first = trim($firstCodeElm->nodeValue);
255-
$first = str_replace("\n", PHP_EOL.' ', $first);
256-
$first = str_replace('<em>', '', $first);
257-
$first = str_replace('</em>', '', $first);
253+
$firstTitle = $this->formatCodeTitle($firstCodeElm);
254+
$first = $this->formatCodeSample($firstCodeElm);
258255

259-
$secondTitle = trim($secondCodeElm->getAttribute('title'));
260-
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
261-
$second = trim($secondCodeElm->nodeValue);
262-
$second = str_replace("\n", PHP_EOL.' ', $second);
263-
$second = str_replace('<em>', '', $second);
264-
$second = str_replace('</em>', '', $second);
256+
$secondTitle = $this->formatCodeTitle($secondCodeElm);
257+
$second = $this->formatCodeSample($secondCodeElm);
265258

266259
$titleRow = '';
267260
if ($firstTitle !== '' || $secondTitle !== '') {
@@ -296,4 +289,42 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
296289
}//end getFormattedCodeComparisonBlock()
297290

298291

292+
/**
293+
* Retrieve a code block title and prepare it for output as HTML.
294+
*
295+
* @param \DOMElement $codeElm The DOMElement object for a code block.
296+
*
297+
* @since 3.12.0
298+
*
299+
* @return string
300+
*/
301+
private function formatCodeTitle(DOMElement $codeElm)
302+
{
303+
$title = trim($codeElm->getAttribute('title'));
304+
return str_replace(' ', '&nbsp;&nbsp;', $title);
305+
306+
}//end formatCodeTitle()
307+
308+
309+
/**
310+
* Retrieve a code block contents and prepare it for output as HTML.
311+
*
312+
* @param \DOMElement $codeElm The DOMElement object for a code block.
313+
*
314+
* @since 3.12.0
315+
*
316+
* @return string
317+
*/
318+
private function formatCodeSample(DOMElement $codeElm)
319+
{
320+
$code = (string) $codeElm->nodeValue;
321+
$code = trim($code);
322+
$code = str_replace("\n", PHP_EOL.' ', $code);
323+
$code = str_replace(['<em>', '</em>'], '', $code);
324+
325+
return $code;
326+
327+
}//end formatCodeSample()
328+
329+
299330
}//end class

src/Generators/Text.php

Lines changed: 92 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace PHP_CodeSniffer\Generators;
1515

16+
use DOMElement;
1617
use DOMNode;
1718

1819
class Text extends Generator
@@ -179,123 +180,21 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
179180
return '';
180181
}
181182

182-
$first = trim($firstCodeElm->nodeValue);
183-
$firstTitle = trim($firstCodeElm->getAttribute('title'));
184-
185-
$firstTitleLines = [];
186-
$tempTitle = '';
187-
$words = explode(' ', $firstTitle);
188-
189-
foreach ($words as $word) {
190-
if (strlen($tempTitle.$word) >= 45) {
191-
if (strlen($tempTitle.$word) === 45) {
192-
// Adding the extra space will push us to the edge
193-
// so we are done.
194-
$firstTitleLines[] = $tempTitle.$word;
195-
$tempTitle = '';
196-
} else if (strlen($tempTitle.$word) === 46) {
197-
// We are already at the edge, so we are done.
198-
$firstTitleLines[] = $tempTitle.$word;
199-
$tempTitle = '';
200-
} else {
201-
$firstTitleLines[] = $tempTitle;
202-
$tempTitle = $word.' ';
203-
}
204-
} else {
205-
$tempTitle .= $word.' ';
206-
}
207-
}//end foreach
208-
209-
if ($tempTitle !== '') {
210-
$firstTitleLines[] = $tempTitle;
211-
}
212-
213-
$first = str_replace(['<em>', '</em>'], '', $first);
214-
$firstLines = explode("\n", $first);
215-
216-
$second = trim($secondCodeElm->nodeValue);
217-
$secondTitle = trim($secondCodeElm->getAttribute('title'));
218-
219-
$secondTitleLines = [];
220-
$tempTitle = '';
221-
$words = explode(' ', $secondTitle);
222-
223-
foreach ($words as $word) {
224-
if (strlen($tempTitle.$word) >= 45) {
225-
if (strlen($tempTitle.$word) === 45) {
226-
// Adding the extra space will push us to the edge
227-
// so we are done.
228-
$secondTitleLines[] = $tempTitle.$word;
229-
$tempTitle = '';
230-
} else if (strlen($tempTitle.$word) === 46) {
231-
// We are already at the edge, so we are done.
232-
$secondTitleLines[] = $tempTitle.$word;
233-
$tempTitle = '';
234-
} else {
235-
$secondTitleLines[] = $tempTitle;
236-
$tempTitle = $word.' ';
237-
}
238-
} else {
239-
$tempTitle .= $word.' ';
240-
}
241-
}//end foreach
242-
243-
if ($tempTitle !== '') {
244-
$secondTitleLines[] = $tempTitle;
245-
}
183+
$firstTitleLines = $this->codeTitleToLines($firstCodeElm);
184+
$firstLines = $this->codeToLines($firstCodeElm);
246185

247-
$second = str_replace(['<em>', '</em>'], '', $second);
248-
$secondLines = explode("\n", $second);
186+
$secondTitleLines = $this->codeTitleToLines($secondCodeElm);
187+
$secondLines = $this->codeToLines($secondCodeElm);
249188

250189
$titleRow = '';
251-
if ($firstTitle !== '' || $secondTitle !== '') {
252-
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
253-
for ($i = 0; $i < $maxTitleLines; $i++) {
254-
if (isset($firstTitleLines[$i]) === true) {
255-
$firstLineText = $firstTitleLines[$i];
256-
} else {
257-
$firstLineText = '';
258-
}
259-
260-
if (isset($secondTitleLines[$i]) === true) {
261-
$secondLineText = $secondTitleLines[$i];
262-
} else {
263-
$secondLineText = '';
264-
}
265-
266-
$titleRow .= '| ';
267-
$titleRow .= $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
268-
$titleRow .= ' | ';
269-
$titleRow .= $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
270-
$titleRow .= ' |'.PHP_EOL;
271-
}//end for
272-
190+
if ($firstTitleLines !== [] || $secondTitleLines !== []) {
191+
$titleRow = $this->linesToTableRows($firstTitleLines, $secondTitleLines);
273192
$titleRow .= str_repeat('-', 100).PHP_EOL;
274193
}//end if
275194

276195
$codeRow = '';
277-
if ($first !== '' || $second !== '') {
278-
$maxCodeLines = max(count($firstLines), count($secondLines));
279-
for ($i = 0; $i < $maxCodeLines; $i++) {
280-
if (isset($firstLines[$i]) === true) {
281-
$firstLineText = $firstLines[$i];
282-
} else {
283-
$firstLineText = '';
284-
}
285-
286-
if (isset($secondLines[$i]) === true) {
287-
$secondLineText = $secondLines[$i];
288-
} else {
289-
$secondLineText = '';
290-
}
291-
292-
$codeRow .= '| ';
293-
$codeRow .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
294-
$codeRow .= '| ';
295-
$codeRow .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
296-
$codeRow .= '|'.PHP_EOL;
297-
}//end for
298-
196+
if ($firstLines !== [] || $secondLines !== []) {
197+
$codeRow = $this->linesToTableRows($firstLines, $secondLines);
299198
$codeRow .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
300199
}//end if
301200

@@ -313,4 +212,87 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
313212
}//end getFormattedCodeComparisonBlock()
314213

315214

215+
/**
216+
* Retrieve a code block title and split it into lines for use in an ASCII table.
217+
*
218+
* @param \DOMElement $codeElm The DOMElement object for a code block.
219+
*
220+
* @since 3.12.0
221+
*
222+
* @return array<string>
223+
*/
224+
private function codeTitleToLines(DOMElement $codeElm)
225+
{
226+
$title = trim($codeElm->getAttribute('title'));
227+
if ($title === '') {
228+
return [];
229+
}
230+
231+
$title = wordwrap($title, 46, "\n");
232+
233+
return explode("\n", $title);
234+
235+
}//end codeTitleToLines()
236+
237+
238+
/**
239+
* Retrieve a code block contents and split it into lines for use in an ASCII table.
240+
*
241+
* @param \DOMElement $codeElm The DOMElement object for a code block.
242+
*
243+
* @since 3.12.0
244+
*
245+
* @return array<string>
246+
*/
247+
private function codeToLines(DOMElement $codeElm)
248+
{
249+
$code = trim($codeElm->nodeValue);
250+
if ($code === '') {
251+
return [];
252+
}
253+
254+
$code = str_replace(['<em>', '</em>'], '', $code);
255+
return explode("\n", $code);
256+
257+
}//end codeToLines()
258+
259+
260+
/**
261+
* Transform two sets of text lines into rows for use in an ASCII table.
262+
*
263+
* The sets may not contains an equal amount of lines, while the resulting rows should.
264+
*
265+
* @param array<string> $column1Lines Lines of text to place in column 1.
266+
* @param array<string> $column2Lines Lines of text to place in column 2.
267+
*
268+
* @return string
269+
*/
270+
private function linesToTableRows(array $column1Lines, array $column2Lines)
271+
{
272+
$maxLines = max(count($column1Lines), count($column2Lines));
273+
274+
$rows = '';
275+
for ($i = 0; $i < $maxLines; $i++) {
276+
$column1Text = '';
277+
if (isset($column1Lines[$i]) === true) {
278+
$column1Text = $column1Lines[$i];
279+
}
280+
281+
$column2Text = '';
282+
if (isset($column2Lines[$i]) === true) {
283+
$column2Text = $column2Lines[$i];
284+
}
285+
286+
$rows .= '| ';
287+
$rows .= $column1Text.str_repeat(' ', max(0, (47 - strlen($column1Text))));
288+
$rows .= '| ';
289+
$rows .= $column2Text.str_repeat(' ', max(0, (48 - strlen($column2Text))));
290+
$rows .= '|'.PHP_EOL;
291+
}//end for
292+
293+
return $rows;
294+
295+
}//end linesToTableRows()
296+
297+
316298
}//end class

0 commit comments

Comments
 (0)