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 @@ -43,4 +43,9 @@ public function __toString(): string
sprintf('%s %s%s%s %s', $this->type, $variadic, $reference, $this->parameterName, $this->description)
);
}

public function isReference(): bool
{
return $this->isReference;
}
}
45 changes: 45 additions & 0 deletions packages/better-php-doc-parser/src/Printer/PatternFactory.php
Original file line number Diff line number Diff line change
@@ -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 . '#';
}
}
18 changes: 13 additions & 5 deletions packages/better-php-doc-parser/src/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -172,6 +179,7 @@ private function printNode(

if ($startEndValueObject !== null) {
$isLastToken = ($nodeCount === $i);

$output = $this->addTokensFromTo(
$output,
$this->currentTokenPosition,
Expand Down Expand Up @@ -268,9 +276,8 @@ private function printPhpDocTagNode(
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;
});

Expand Down Expand Up @@ -306,6 +313,7 @@ private function getRemovedNodesPositions(): array
);

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
Expand Down Expand Up @@ -348,8 +356,8 @@ private function appendToOutput(string $output, int $from, int $to, array $posit
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'] ?? '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @param string $key
* @param int $selected
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @param int $key
* @param int $selected
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @param AttributeAwareIdentifierTypeNode&IdentifierTypeNode $typeNode
* @param string[] ...$types
*/