13
13
14
14
namespace PHP_CodeSniffer \Generators ;
15
15
16
+ use DOMElement ;
16
17
use DOMNode ;
17
18
18
19
class Text extends Generator
@@ -179,123 +180,21 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
179
180
return '' ;
180
181
}
181
182
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 );
246
185
247
- $ second = str_replace ([ ' <em> ' , ' </em> ' ], '' , $ second );
248
- $ secondLines = explode ( "\n" , $ second );
186
+ $ secondTitleLines = $ this -> codeTitleToLines ( $ secondCodeElm );
187
+ $ secondLines = $ this -> codeToLines ( $ secondCodeElm );
249
188
250
189
$ 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 );
273
192
$ titleRow .= str_repeat ('- ' , 100 ).PHP_EOL ;
274
193
}//end if
275
194
276
195
$ 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 );
299
198
$ codeRow .= str_repeat ('- ' , 100 ).PHP_EOL .PHP_EOL ;
300
199
}//end if
301
200
@@ -313,4 +212,87 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
313
212
}//end getFormattedCodeComparisonBlock()
314
213
315
214
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
+
316
298
}//end class
0 commit comments