Skip to content

Commit 5998203

Browse files
committed
Generators/Text::getFormattedCodeComparisonBlock(): minor refactor [1]
Move some duplicate code to dedicated `private` methods.
1 parent 394731e commit 5998203

File tree

1 file changed

+78
-68
lines changed

1 file changed

+78
-68
lines changed

src/Generators/Text.php

Lines changed: 78 additions & 68 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,76 +180,14 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
179180
return '';
180181
}
181182

182-
$first = trim($firstCodeElm->nodeValue);
183-
$firstTitle = trim($firstCodeElm->getAttribute('title'));
183+
$firstTitleLines = $this->codeTitleToLines($firstCodeElm);
184+
$firstLines = $this->codeToLines($firstCodeElm);
184185

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-
}
246-
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 !== '') {
190+
if ($firstTitleLines !== [] || $secondTitleLines !== []) {
252191
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
253192
for ($i = 0; $i < $maxTitleLines; $i++) {
254193
if (isset($firstTitleLines[$i]) === true) {
@@ -274,7 +213,7 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
274213
}//end if
275214

276215
$codeRow = '';
277-
if ($first !== '' || $second !== '') {
216+
if ($firstLines !== [] || $secondLines !== []) {
278217
$maxCodeLines = max(count($firstLines), count($secondLines));
279218
for ($i = 0; $i < $maxCodeLines; $i++) {
280219
if (isset($firstLines[$i]) === true) {
@@ -313,4 +252,75 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
313252
}//end getFormattedCodeComparisonBlock()
314253

315254

255+
/**
256+
* Retrieve a code block title and split it into lines for use in an ASCII table.
257+
*
258+
* @param \DOMElement $codeElm The DOMElement object for a code block.
259+
*
260+
* @since 3.12.0
261+
*
262+
* @return array<string>
263+
*/
264+
private function codeTitleToLines(DOMElement $codeElm)
265+
{
266+
$title = trim($codeElm->getAttribute('title'));
267+
if ($title === '') {
268+
return [];
269+
}
270+
271+
$titleLines = [];
272+
$tempTitle = '';
273+
$words = explode(' ', $title);
274+
275+
foreach ($words as $word) {
276+
if (strlen($tempTitle.$word) >= 45) {
277+
if (strlen($tempTitle.$word) === 45) {
278+
// Adding the extra space will push us to the edge
279+
// so we are done.
280+
$titleLines[] = $tempTitle.$word;
281+
$tempTitle = '';
282+
} else if (strlen($tempTitle.$word) === 46) {
283+
// We are already at the edge, so we are done.
284+
$titleLines[] = $tempTitle.$word;
285+
$tempTitle = '';
286+
} else {
287+
$titleLines[] = $tempTitle;
288+
$tempTitle = $word.' ';
289+
}
290+
} else {
291+
$tempTitle .= $word.' ';
292+
}
293+
}//end foreach
294+
295+
if ($tempTitle !== '') {
296+
$titleLines[] = $tempTitle;
297+
}
298+
299+
return $titleLines;
300+
301+
}//end codeTitleToLines()
302+
303+
304+
/**
305+
* Retrieve a code block contents and split it into lines for use in an ASCII table.
306+
*
307+
* @param \DOMElement $codeElm The DOMElement object for a code block.
308+
*
309+
* @since 3.12.0
310+
*
311+
* @return array<string>
312+
*/
313+
private function codeToLines(DOMElement $codeElm)
314+
{
315+
$code = trim($codeElm->nodeValue);
316+
if ($code === '') {
317+
return [];
318+
}
319+
320+
$code = str_replace(['<em>', '</em>'], '', $code);
321+
return explode("\n", $code);
322+
323+
}//end codeToLines()
324+
325+
316326
}//end class

0 commit comments

Comments
 (0)