Skip to content

Commit

Permalink
simplify getReformattedText
Browse files Browse the repository at this point in the history
  • Loading branch information
shmax committed Oct 5, 2023
1 parent bfa5991 commit 3b6ff9a
Showing 1 changed file with 1 addition and 44 deletions.
45 changes: 1 addition & 44 deletions src/Ast/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,7 @@ public function __construct(string $text, int $startLine = -1, int $startIndex =

public function getReformattedText(): ?string
{
$text = trim($this->text);
$newlinePos = strpos($text, "\n");
if ($newlinePos === false) {
// Single line comments don't need further processing
return $text;
} elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text) === 1) {
// Multi line comment of the type
//
// /*
// * Some text.
// * Some more text.
// */
//
// is handled by replacing the whitespace sequences before the * by a single space
return preg_replace('(^\s+\*)m', ' *', $this->text);
} elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) === 1 && preg_match('(\n(\s*)\*/$)', $text, $matches) === 1) {
// Multi line comment of the type
//
// /*
// Some text.
// Some more text.
// */
//
// is handled by removing the whitespace sequence on the line before the closing
// */ on all lines. So if the last line is " */", then " " is removed at the
// start of all lines.
return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
} elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches) === 1) {
// Multi line comment of the type
//
// /* Some text.
// Some more text.
// Indented text.
// Even more text. */
//
// is handled by removing the difference between the shortest whitespace prefix on all
// lines and the length of the "/* " opening sequence.
$prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1));
$removeLen = $prefixLen - strlen($matches[0]);
return preg_replace('(^\s{' . $removeLen . '})m', '', $text);
}

// No idea how to format this comment, so simply return as is
return $text;
return trim($this->text);
}

private function getShortestWhitespacePrefixLen(string $str): int

Check failure on line 39 in src/Ast/Comment.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2)

Method PHPStan\PhpDocParser\Ast\Comment::getShortestWhitespacePrefixLen() is unused.

Check failure on line 39 in src/Ast/Comment.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3)

Method PHPStan\PhpDocParser\Ast\Comment::getShortestWhitespacePrefixLen() is unused.

Check failure on line 39 in src/Ast/Comment.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Method PHPStan\PhpDocParser\Ast\Comment::getShortestWhitespacePrefixLen() is unused.

Check failure on line 39 in src/Ast/Comment.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0)

Method PHPStan\PhpDocParser\Ast\Comment::getShortestWhitespacePrefixLen() is unused.

Check failure on line 39 in src/Ast/Comment.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method PHPStan\PhpDocParser\Ast\Comment::getShortestWhitespacePrefixLen() is unused.

Check failure on line 39 in src/Ast/Comment.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Method PHPStan\PhpDocParser\Ast\Comment::getShortestWhitespacePrefixLen() is unused.
Expand Down

0 comments on commit 3b6ff9a

Please sign in to comment.