Skip to content

Commit

Permalink
[Performance][BetterPhpDocPaser] Reduce regex usage on start docblock…
Browse files Browse the repository at this point in the history
… check on PhpDocInfoPrinter (#4966)

* [BetterPhpDocPaser] Reduce regex usage on start docblock check on PhpDocInfoPrinter

* [ci-review] Rector Rectify

* cs fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 10, 2023
1 parent 9f29bf8 commit 038f809
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ final class PhpDocInfoPrinter
private const CALLABLE_REGEX = '#callable(\s+)\(#';

/**
* @var string
* @see https://regex101.com/r/LLWiPl/1
* @var string[]
*/
private const DOCBLOCK_START_REGEX = '#^(\/\/|\/\*\*|\/\*|\#)#';
private const DOCBLOCK_STARTS = ['//', '/**', '/*', '#'];

/**
* @var string Uses a hardcoded unix-newline since most codes use it (even on windows) - otherwise we would need to normalize newlines
Expand Down Expand Up @@ -157,7 +156,7 @@ private function printPhpDocNode(PhpDocNode $phpDocNode): string
$output = $this->printEnd($output);

// fix missing start
if (! StringUtils::isMatch($output, self::DOCBLOCK_START_REGEX) && $output !== '') {
if (! $this->hasDocblockStart($output) && $output !== '') {
$output = '/**' . $output;
}

Expand All @@ -169,6 +168,17 @@ private function printPhpDocNode(PhpDocNode $phpDocNode): string
return str_replace(" \n", "\n", $output);
}

private function hasDocblockStart(string $output): bool
{
foreach (self::DOCBLOCK_STARTS as $docblockStart) {
if (str_starts_with($output, $docblockStart)) {
return true;
}
}

return false;
}

private function printDocChildNode(
PhpDocChildNode $phpDocChildNode,
int $key = 0,
Expand Down

0 comments on commit 038f809

Please sign in to comment.