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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function __construct($keyName, bool $optional, TypeNode $typeNode, string
{
parent::__construct($keyName, $optional, $typeNode);

$this->hasSpaceAfterDoubleColon = (bool) Strings::match($docComment, '#\:\s+#');
// spaces after double colon
$keyWithSpacePattern = $this->createKeyWithSpacePattern($keyName, $optional);
$this->hasSpaceAfterDoubleColon = (bool) Strings::matchAll($docComment, $keyWithSpacePattern);
}

public function __toString(): string
Expand All @@ -45,4 +47,19 @@ public function __toString(): string
(string) $this->valueType
);
}

/**
* @param ConstExprIntegerNode|IdentifierTypeNode|null $keyName
*/
private function createKeyWithSpacePattern($keyName, bool $optional): string
{
$keyNameString = (string) $keyName;
if ($optional) {
$keyNameString .= '?';
}

$keyNameStringPregQuoted = preg_quote($keyNameString);

return sprintf('#%s\:\s+#', $keyNameStringPregQuoted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;

final class AnnotationContentResolver
{
Expand All @@ -16,9 +17,15 @@ final class AnnotationContentResolver
*/
private $tokenIteratorFactory;

public function __construct(TokenIteratorFactory $tokenIteratorFactory)
/**
* @var PrivatesAccessor
*/
private $privatesAccessor;

public function __construct(TokenIteratorFactory $tokenIteratorFactory, PrivatesAccessor $privatesAccessor)
{
$this->tokenIteratorFactory = $tokenIteratorFactory;
$this->privatesAccessor = $privatesAccessor;
}

/**
Expand All @@ -30,6 +37,7 @@ public function resolveFromTokenIterator(TokenIterator $tokenIterator): string
$annotationContent = '';
$unclosedOpenedBracketCount = 0;

// keep spaces?
while (true) {
if ($tokenIterator->currentTokenType() === Lexer::TOKEN_OPEN_PARENTHESES) {
++$unclosedOpenedBracketCount;
Expand All @@ -44,6 +52,8 @@ public function resolveFromTokenIterator(TokenIterator $tokenIterator): string
}

// remove new line "*"
$annotationContent = $this->appendPreviousWhitespace($tokenIterator, $annotationContent);

if (Strings::contains($tokenIterator->currentTokenValue(), '*')) {
$tokenValueWithoutAsterisk = Strings::replace($tokenIterator->currentTokenValue(), '#\*#');
$annotationContent .= $tokenValueWithoutAsterisk;
Expand All @@ -59,6 +69,9 @@ public function resolveFromTokenIterator(TokenIterator $tokenIterator): string
$tokenIterator->next();
}

// remove appended first space
// $annotationContent = ltrim($annotationContent);

return $this->cleanMultilineAnnotationContent($annotationContent);
}

Expand Down Expand Up @@ -127,4 +140,29 @@ private function tryStartWithKey(string $name, bool $start, TokenIterator $local

return false;
}

private function appendPreviousWhitespace(TokenIterator $tokenIterator, string $annotationContent): string
{
// is space?
if (! $tokenIterator->isPrecededByHorizontalWhitespace()) {
return $annotationContent;
}

$tokens = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'tokens');
$currentIndex = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'index');

if (! isset($tokens[$currentIndex - 1])) {
return $annotationContent;
}

$previousWhitespaceToken = $tokens[$currentIndex - 1];

// do not prepend whitespace without any content
if ($annotationContent === '') {
return $annotationContent;
}

// get the space
return $annotationContent . $previousWhitespaceToken[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ private function parseChildAndStoreItsPositions(TokenIterator $tokenIterator): N
$originalTokenIterator = clone $tokenIterator;
$docContent = $this->annotationContentResolver->resolveFromTokenIterator($originalTokenIterator);

// @todo here space is missing, probably skipped in resolveFromTokenIterator(), or in PHPStan doc parser
// we need evne more original content :)

$tokenStart = $this->getTokenIteratorIndex($tokenIterator);
$phpDocNode = $this->privatesCaller->callPrivateMethod($this, 'parseChild', $tokenIterator);
$tokenEnd = $this->getTokenIteratorIndex($tokenIterator);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* @psalm-param array{cache:string, callbacks?: mixed, plugin?:mixed} $options
*/