add test case for as/of template tag

This commit is contained in:
TomasVotruba 2020-06-15 12:12:04 +02:00
parent abb11dcdcf
commit 4132d9d885
5 changed files with 42 additions and 1 deletions

View File

@ -4,11 +4,36 @@ declare(strict_types=1);
namespace Rector\AttributeAwarePhpDoc\Ast\PhpDoc;
use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareTemplateTagValueNode extends TemplateTagValueNode implements AttributeAwareNodeInterface
{
use AttributeTrait;
/**
* @var string
*/
private $preposition;
public function __construct(string $name, ?TypeNode $bound, string $description, string $originalContent)
{
parent::__construct($name, $bound, $description);
$matches = Strings::match($originalContent, '#\s+(?<preposition>as|of)\s+#');
$this->preposition = $matches['preposition'] ?? 'of';
}
public function __toString(): string
{
// @see https://github.com/rectorphp/rector/issues/3438
# 'as'/'of'
$bound = $this->bound !== null ? ' ' . $this->preposition . ' ' . $this->bound : '';
$content = $this->name . $bound . ' ' . $this->description;
return trim($content);
}
}

View File

@ -27,6 +27,6 @@ final class AttributeAwareTemplateTagValueNodeFactory implements AttributeNodeAw
*/
public function create(Node $node, string $docContent): AttributeAwareNodeInterface
{
return new AttributeAwareTemplateTagValueNode($node->name, $node->bound, $node->description);
return new AttributeAwareTemplateTagValueNode($node->name, $node->bound, $node->description, $docContent);
}
}

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\TableTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Property_\ColumnTagValueNode;
@ -48,5 +49,6 @@ final class TagValueToPhpParserNodeMap
SensioTemplateTagValueNode::class => Class_::class,
SensioMethodTagValueNode::class => ClassMethod::class,
TemplateTagValueNode::class => Class_::class,
];
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Fixture\Native\Template;
/** @template T as int */
final class TemplateTagAsOf
{
}

View File

@ -6,6 +6,7 @@ namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint;
use Iterator;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\TableTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Property_\ColumnTagValueNode;
@ -65,6 +66,8 @@ final class TagValueNodeReprintTest extends AbstractPhpDocInfoTest
SensioTemplateTagValueNode::class => __DIR__ . '/Fixture/SensioTemplate',
SensioMethodTagValueNode::class => __DIR__ . '/Fixture/SensioMethod',
TemplateTagValueNode::class => __DIR__ . '/Fixture/Native/Template',
];
}
}