Merge pull request #2984 from rectorphp/fix-spacing-multie-collon-mixed

improve array shape double collon spacing
This commit is contained in:
Tomas Votruba 2020-03-02 18:26:12 +01:00 committed by GitHub
commit d7b9d4907b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 2 deletions

View File

@ -28,7 +28,9 @@ final class AttributeAwareArrayShapeItemNode extends ArrayShapeItemNode implemen
{ {
parent::__construct($keyName, $optional, $typeNode); 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 public function __toString(): string
@ -45,4 +47,19 @@ final class AttributeAwareArrayShapeItemNode extends ArrayShapeItemNode implemen
(string) $this->valueType (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);
}
} }

View File

@ -8,6 +8,7 @@ use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory; use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
final class AnnotationContentResolver final class AnnotationContentResolver
{ {
@ -16,9 +17,15 @@ final class AnnotationContentResolver
*/ */
private $tokenIteratorFactory; private $tokenIteratorFactory;
public function __construct(TokenIteratorFactory $tokenIteratorFactory) /**
* @var PrivatesAccessor
*/
private $privatesAccessor;
public function __construct(TokenIteratorFactory $tokenIteratorFactory, PrivatesAccessor $privatesAccessor)
{ {
$this->tokenIteratorFactory = $tokenIteratorFactory; $this->tokenIteratorFactory = $tokenIteratorFactory;
$this->privatesAccessor = $privatesAccessor;
} }
/** /**
@ -30,6 +37,7 @@ final class AnnotationContentResolver
$annotationContent = ''; $annotationContent = '';
$unclosedOpenedBracketCount = 0; $unclosedOpenedBracketCount = 0;
// keep spaces?
while (true) { while (true) {
if ($tokenIterator->currentTokenType() === Lexer::TOKEN_OPEN_PARENTHESES) { if ($tokenIterator->currentTokenType() === Lexer::TOKEN_OPEN_PARENTHESES) {
++$unclosedOpenedBracketCount; ++$unclosedOpenedBracketCount;
@ -44,6 +52,8 @@ final class AnnotationContentResolver
} }
// remove new line "*" // remove new line "*"
$annotationContent = $this->appendPreviousWhitespace($tokenIterator, $annotationContent);
if (Strings::contains($tokenIterator->currentTokenValue(), '*')) { if (Strings::contains($tokenIterator->currentTokenValue(), '*')) {
$tokenValueWithoutAsterisk = Strings::replace($tokenIterator->currentTokenValue(), '#\*#'); $tokenValueWithoutAsterisk = Strings::replace($tokenIterator->currentTokenValue(), '#\*#');
$annotationContent .= $tokenValueWithoutAsterisk; $annotationContent .= $tokenValueWithoutAsterisk;
@ -59,6 +69,9 @@ final class AnnotationContentResolver
$tokenIterator->next(); $tokenIterator->next();
} }
// remove appended first space
// $annotationContent = ltrim($annotationContent);
return $this->cleanMultilineAnnotationContent($annotationContent); return $this->cleanMultilineAnnotationContent($annotationContent);
} }
@ -127,4 +140,29 @@ final class AnnotationContentResolver
return false; 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];
}
} }

View File

@ -214,6 +214,9 @@ final class BetterPhpDocParser extends PhpDocParser
$originalTokenIterator = clone $tokenIterator; $originalTokenIterator = clone $tokenIterator;
$docContent = $this->annotationContentResolver->resolveFromTokenIterator($originalTokenIterator); $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); $tokenStart = $this->getTokenIteratorIndex($tokenIterator);
$phpDocNode = $this->privatesCaller->callPrivateMethod($this, 'parseChild', $tokenIterator); $phpDocNode = $this->privatesCaller->callPrivateMethod($this, 'parseChild', $tokenIterator);
$tokenEnd = $this->getTokenIteratorIndex($tokenIterator); $tokenEnd = $this->getTokenIteratorIndex($tokenIterator);

View File

@ -0,0 +1,3 @@
/**
* @psalm-param array{cache:string, callbacks?: mixed, plugin?:mixed} $options
*/