Skip to content

Commit 7e6bd46

Browse files
committed
refactor: use static methods in bbcode parser
They're not modifying instance state, so it makes more sense for them to be static. Part of HDInnovations#4860.
1 parent 48f62be commit 7e6bd46

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

app/Helpers/Bbcode.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,27 @@ public function parse(?string $source, bool $replaceLineBreaks = true): string
286286
$source = str_replace('[hr]', '<hr>', $source);
287287
$source = preg_replace_callback(
288288
'/\[url](.*?)\[\/url]/i',
289-
fn ($matches) => '<a href="'.$this->sanitizeUrl($matches[1]).'">'.$this->sanitizeUrl($matches[1]).'</a>',
289+
fn ($matches) => '<a href="'.self::sanitizeUrl($matches[1]).'">'.self::sanitizeUrl($matches[1]).'</a>',
290290
$source
291291
);
292292
$source = preg_replace_callback(
293293
'/\[url=(.*?)](.*?)\[\/url]/i',
294-
fn ($matches) => '<a href="'.$this->sanitizeUrl($matches[1]).'">'.$matches[2].'</a>',
294+
fn ($matches) => '<a href="'.self::sanitizeUrl($matches[1]).'">'.$matches[2].'</a>',
295295
$source ?? ''
296296
);
297297
$source = preg_replace_callback(
298298
'/\[img](.*?)\[\/img]/i',
299-
fn ($matches) => '<img src="'.$this->sanitizeUrl($matches[1], isImage: true).'" loading="lazy" class="img-responsive" style="display: inline !important;">',
299+
fn ($matches) => '<img src="'.self::sanitizeUrl($matches[1], isImage: true).'" loading="lazy" class="img-responsive" style="display: inline !important;">',
300300
$source ?? ''
301301
);
302302
$source = preg_replace_callback(
303303
'/\[img width=(\d+)](.*?)\[\/img]/i',
304-
fn ($matches) => '<img src="'.$this->sanitizeUrl($matches[2], isImage: true).'" loading="lazy" width="'.$matches[1].'px">',
304+
fn ($matches) => '<img src="'.self::sanitizeUrl($matches[2], isImage: true).'" loading="lazy" width="'.$matches[1].'px">',
305305
$source ?? ''
306306
);
307307
$source = preg_replace_callback(
308308
'/\[img=(\d+)(?:x\d+)?](.*?)\[\/img]/i',
309-
fn ($matches) => '<img src="'.$this->sanitizeUrl($matches[2], isImage: true).'" loading="lazy" width="'.$matches[1].'px">',
309+
fn ($matches) => '<img src="'.self::sanitizeUrl($matches[2], isImage: true).'" loading="lazy" width="'.$matches[1].'px">',
310310
$source ?? ''
311311
);
312312

@@ -340,7 +340,7 @@ function ($matches) {
340340
return 'Broken comparison';
341341
}
342342

343-
$validatedUrls = collect($urls)->map(fn ($url) => $this->sanitizeUrl($url, isImage: true));
343+
$validatedUrls = collect($urls)->map(fn ($url) => self::sanitizeUrl($url, isImage: true));
344344
$chunkedUrls = $validatedUrls->chunk(\count($comparates));
345345
$html = view('partials.comparison', ['comparates' => $comparates, 'urls' => $chunkedUrls])->render();
346346
$html = preg_replace('/\s+/', ' ', $html);
@@ -387,7 +387,7 @@ function ($matches) {
387387
$source = substr_replace((string) $source, (string) $el['closeHtml'], $index, \strlen((string) $el['closeBbcode']));
388388

389389
if ($replaceLineBreaks === true && $el['block'] === true) {
390-
$this->handleBlockElementSpacing($source, $index, $index, $index + \strlen((string) $el['closeHtml']) - 1);
390+
self::handleBlockElementSpacing($source, $index, $index, $index + \strlen((string) $el['closeHtml']) - 1);
391391
}
392392
} else {
393393
$openedElements[] = $name;
@@ -404,7 +404,7 @@ function ($matches) {
404404
$source = substr_replace((string) $source, $replacement, $index, \strlen($matches[0]));
405405

406406
if ($replaceLineBreaks === true && $el['block'] === true) {
407-
$this->handleBlockElementSpacing($source, $index, $index, $index + \strlen($replacement) - 1);
407+
self::handleBlockElementSpacing($source, $index, $index, $index + \strlen($replacement) - 1);
408408
}
409409

410410
$openedElements[] = $name;
@@ -446,7 +446,7 @@ function ($matches) {
446446
* @param int $tagStartIndex The index of the first character of the tag being parsed inside `$source`. Should be the `[` character.
447447
* @param int $tagStopIndex The index of the last character of the tag being parsed inside `$source`. Should be the `]` character.
448448
*/
449-
private function handleBlockElementSpacing(string &$source, int &$index, int $tagStartIndex, int $tagStopIndex): void
449+
private static function handleBlockElementSpacing(string &$source, int &$index, int $tagStartIndex, int $tagStopIndex): void
450450
{
451451
// Remove two line breaks (if they exist) instead of one, since a
452452
// line break after a block element is positioned on the line after
@@ -479,7 +479,7 @@ private function handleBlockElementSpacing(string &$source, int &$index, int $ta
479479
}
480480
}
481481

482-
private function sanitizeUrl(string $url, ?bool $isImage = null): string
482+
private static function sanitizeUrl(string $url, ?bool $isImage = null): string
483483
{
484484
// Do NOT add `javascript`, `data` or `vbscript` here
485485
// or else you will allow an XSS vulnerability!

0 commit comments

Comments
 (0)