Merge pull request #2988 from rectorphp/extra-space

add space between name and value
This commit is contained in:
Tomas Votruba 2020-03-03 09:50:47 +01:00 committed by GitHub
commit 3e284b0511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 5 deletions

View File

@ -43,4 +43,9 @@ final class AttributeAwareParamTagValueNode extends ParamTagValueNode implements
sprintf('%s %s%s%s %s', $this->type, $variadic, $reference, $this->parameterName, $this->description)
);
}
public function isReference(): bool
{
return $this->isReference;
}
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Printer;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareParamTagValueNode;
final class PatternFactory
{
/**
* @var string
*/
private const TYPE_PATTERN = '[\w\\\\\[\]\(\)\{\}\:\?\$\-\,\&|<>\s]+';
public function createSpacePattern(PhpDocTagNode $phpDocTagNode): string
{
$spacePattern = preg_quote($phpDocTagNode->name, '#') . '(?<space>\s+)';
// we have to match exact @param space, in case of multiple @param s
if ($phpDocTagNode->value instanceof AttributeAwareParamTagValueNode) {
/** @var AttributeAwareParamTagValueNode $paramTagValueNode */
$paramTagValueNode = $phpDocTagNode->value;
// type could be changed, so better keep it here
$spacePattern .= self::TYPE_PATTERN;
if ($paramTagValueNode->parameterName !== '') {
$spacePattern .= '\s+';
if ($paramTagValueNode->isReference()) {
$spacePattern .= '&';
}
if ($paramTagValueNode->isVariadic) {
$spacePattern .= '...';
}
$spacePattern .= preg_quote($paramTagValueNode->parameterName);
}
}
return '#' . $spacePattern . '#';
}
}

View File

@ -63,12 +63,19 @@ final class PhpDocInfoPrinter
*/
private $multilineSpaceFormatPreserver;
/**
* @var PatternFactory
*/
private $patternFactory;
public function __construct(
OriginalSpacingRestorer $originalSpacingRestorer,
MultilineSpaceFormatPreserver $multilineSpaceFormatPreserver
MultilineSpaceFormatPreserver $multilineSpaceFormatPreserver,
PatternFactory $patternFactory
) {
$this->originalSpacingRestorer = $originalSpacingRestorer;
$this->multilineSpaceFormatPreserver = $multilineSpaceFormatPreserver;
$this->patternFactory = $patternFactory;
}
/**
@ -172,6 +179,7 @@ final class PhpDocInfoPrinter
if ($startEndValueObject !== null) {
$isLastToken = ($nodeCount === $i);
$output = $this->addTokensFromTo(
$output,
$this->currentTokenPosition,
@ -268,9 +276,8 @@ final class PhpDocInfoPrinter
if ($this->hasDescription($phpDocTagNode)) {
$quotedDescription = preg_quote($phpDocTagNode->value->description, '#');
$pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+');
$nodeOutput = Strings::replace($nodeOutput, '#' . $pattern . '#', function (array $matched) use (
$phpDocTagNode
) {
$nodeOutput = Strings::replace($nodeOutput, '#' . $pattern . '#', function () use ($phpDocTagNode) {
// warning: classic string replace() breaks double "\\" slashes to "\"
return $phpDocTagNode->value->description;
});
@ -306,6 +313,7 @@ final class PhpDocInfoPrinter
);
foreach ($removedNodes as $removedNode) {
/** @var StartEndValueObject $removedPhpDocNodeInfo */
$removedPhpDocNodeInfo = $removedNode->getAttribute(Attribute::START_END);
// change start position to start of the line, so the whole line is removed
@ -348,8 +356,8 @@ final class PhpDocInfoPrinter
private function resolveTagSpaceSeparator(PhpDocTagNode $phpDocTagNode): string
{
$originalContent = $this->phpDocInfo->getOriginalContent();
$spacePattern = $this->patternFactory->createSpacePattern($phpDocTagNode);
$spacePattern = '#' . preg_quote($phpDocTagNode->name, '#') . '(?<space>\s+)#';
$matches = Strings::match($originalContent, $spacePattern);
return $matches['space'] ?? '';

View File

@ -0,0 +1,4 @@
/**
* @param string $key
* @param int $selected
*/

View File

@ -0,0 +1,4 @@
/**
* @param int $key
* @param int $selected
*/

View File

@ -0,0 +1,4 @@
/**
* @param AttributeAwareIdentifierTypeNode&IdentifierTypeNode $typeNode
* @param string[] ...$types
*/