Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 1 addition & 44 deletions src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
Expand All @@ -34,17 +31,6 @@
*/
private const string MULTI_NEW_LINES_REGEX = '#(?<new_line>\r\n|\n){2,}#';

/**
* @var int[]
*/
private const array NO_TEXT_TOKEN_TYPES = [
Lexer::TOKEN_OPEN_PHPDOC,
Lexer::TOKEN_CLOSE_PHPDOC,
Lexer::TOKEN_PHPDOC_EOL,
Lexer::TOKEN_HORIZONTAL_WS,
Lexer::TOKEN_END,
];

/**
* @var PhpDocNodeDecoratorInterface[]
*/
Expand All @@ -71,12 +57,7 @@ public function __construct(

public function parseWithNode(TokenIterator $tokenIterator, Node $node): PhpDocNode
{
try {
$phpDocNode = $this->phpDocParser->parse($tokenIterator);
} catch (ParserException) {
// invalid Doctrine annotation arguments; keep the docblock as it is
return $this->createTextPhpDocNode($tokenIterator);
}
$phpDocNode = $this->phpDocParser->parse($tokenIterator);

foreach ($phpDocNode->children as $phpDocChildNode) {
$this->setStartAndEnd($phpDocChildNode);
Expand Down Expand Up @@ -120,28 +101,4 @@ private function setStartAndEnd(PhpDocParserNode $phpDocParserNode): void
new StartAndEnd($startIndex, max($startIndex, $endIndex + 1))
);
}

/**
* One text node with the content between "/**" and "*\/"
*/
private function createTextPhpDocNode(TokenIterator $tokenIterator): PhpDocNode
{
$tokens = $tokenIterator->getTokens();
$startIndex = 0;
$endIndex = count($tokens) - 1;

while ($startIndex < $endIndex && in_array($tokens[$startIndex][Lexer::TYPE_OFFSET], self::NO_TEXT_TOKEN_TYPES, true)) {
++$startIndex;
}

while ($endIndex > $startIndex && in_array($tokens[$endIndex][Lexer::TYPE_OFFSET], self::NO_TEXT_TOKEN_TYPES, true)) {
--$endIndex;
}

$phpDocTextNode = new PhpDocTextNode($tokenIterator->getContentBetween($startIndex, $endIndex + 1));
$phpDocTextNode->setAttribute(Attribute::START_INDEX, $startIndex);
$phpDocTextNode->setAttribute(Attribute::END_INDEX, $endIndex);

return new PhpDocNode([$phpDocTextNode]);
}
}
29 changes: 0 additions & 29 deletions src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public function printFormatPreserving(PhpDocInfo $phpDocInfo): string
return $docContent;
}

$this->reindexArrays($phpDocNode);

$originalPhpDocNode = $phpDocInfo->getOriginalPhpDocNode();
$phpDocNode = $this->removeTrailingEmptyLines($phpDocNode, $originalPhpDocNode);

Expand Down Expand Up @@ -108,33 +106,6 @@ public function printToComments(PhpDocInfo $phpDocInfo): array
return [new Comment($printedPhpDocContents)];
}

/**
* The printer compares lists of nodes; unset() in rules leaves gaps in the keys
*/
private function reindexArrays(Node $node): void
{
foreach (get_object_vars($node) as $propertyName => $value) {
if ($value instanceof Node) {
$this->reindexArrays($value);
continue;
}

if (! is_array($value)) {
continue;
}

if (! array_is_list($value)) {
$node->{$propertyName} = array_values($value);
}

foreach ($value as $item) {
if ($item instanceof Node) {
$this->reindexArrays($item);
}
}
}
}

/**
* Removing the last tag keeps the empty line above it, remove it too
*/
Expand Down
Loading