add comments constant to prevent typos

This commit is contained in:
TomasVotruba 2020-05-25 17:00:27 +02:00
parent 897f226f94
commit e3463b1073
15 changed files with 31 additions and 14 deletions

View File

@ -43,6 +43,7 @@ expectedArguments(
\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_SHORT_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_REGULAR_PATTERN,
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS,
);
expectedArguments(
@ -71,6 +72,7 @@ expectedArguments(
\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_SHORT_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_REGULAR_PATTERN,
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS,
);
expectedArguments(

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Comment;
use PhpParser\Node;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class MergedNodeCommentPreserver
{
@ -21,6 +22,6 @@ final class MergedNodeCommentPreserver
}
$comments = array_merge($newNode->getComments(), $comments);
$newNode->setAttribute('comments', $comments);
$newNode->setAttribute(AttributeKey::COMMENTS, $comments);
}
}

View File

@ -77,6 +77,14 @@ final class AttributeKey
*/
public const ORIGINAL_NODE = 'origNode';
/**
* Internal php-parser name.
* Do not change this even if you want!
*
* @var string
*/
public const COMMENTS = 'comments';
/**
* Internal php-parser name.
* Do not change this even if you want!

View File

@ -106,7 +106,7 @@ final class DocBlockManipulator
if ($phpDoc === '') {
if ($phpDocInfo->getOriginalPhpDocNode()->children !== []) {
// all comments were removed → null
$node->setAttribute('comments', null);
$node->setAttribute(AttributeKey::COMMENTS, null);
}
return;

View File

@ -6,6 +6,7 @@ namespace Rector\PhpAttribute\Printer;
use PhpParser\Comment;
use PhpParser\Node\Stmt\Nop;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpAttribute\Collector\PlaceholderToValueCollector;
use Rector\PhpAttribute\Contract\PhpAttributableTagNodeInterface;
@ -37,7 +38,7 @@ final class PlaceholderNodeFactory
// 2. create placeholder node
$placeholderNop = new Nop();
$placeholderName = $this->createPlaceholderName();
$placeholderNop->setAttribute('comments', [new Comment($placeholderName)]);
$placeholderNop->setAttribute(AttributeKey::COMMENTS, [new Comment($placeholderName)]);
// 3. store key/value placeholder
$this->placeholderToValueCollector->add($placeholderName, $phpAttributesString);

View File

@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see https://stackoverflow.com/questions/559844/whats-better-to-use-in-php-array-value-or-array-pusharray-value
@ -81,7 +82,7 @@ PHP
// keep comments of first line
if ($position === 1) {
$assign = new Expression($assign);
$assign->setAttribute('comments', $node->getComments());
$assign->setAttribute(AttributeKey::COMMENTS, $node->getComments());
}
$this->addNodeAfterNode($assign, $node);

View File

@ -109,7 +109,7 @@ PHP
return;
}
$firstNode->setAttribute('comments', $comments);
$firstNode->setAttribute(AttributeKey::COMMENTS, $comments);
$firstNode->setAttribute(AttributeKey::PHP_DOC_INFO, null);
}
}

View File

@ -176,6 +176,6 @@ PHP
}
$classMethod->setAttribute(AttributeKey::PHP_DOC_INFO, $this->returnPhpDocInfo);
$classMethod->setAttribute('comments', $this->returnComments);
$classMethod->setAttribute(AttributeKey::COMMENTS, $this->returnComments);
}
}

View File

@ -102,7 +102,7 @@ PHP
// remove commnets
$nextNode->setAttribute(AttributeKey::PHP_DOC_INFO, null);
$nextNode->setAttribute('comments', null);
$nextNode->setAttribute(AttributeKey::COMMENTS, null);
return $node;
}

View File

@ -91,7 +91,7 @@ PHP
$newCommentText = Strings::replace($singleComment->getText(), '#^\/\*(\s|b)#', '/**$1');
$comments[0] = new Comment($newCommentText);
$node->setAttribute('comments', $comments);
$node->setAttribute(AttributeKey::COMMENTS, $comments);
return $node;
}

View File

@ -148,7 +148,7 @@ PHP
private function refactorFreshlyCreatedNode(Node $node, PhpDocInfo $phpDocInfo, Variable $variable): ?Node
{
$node->setAttribute('comments', null);
$node->setAttribute(AttributeKey::COMMENTS, null);
$type = $phpDocInfo->getVarType();
$assertFuncCall = $this->createFuncCallBasedOnType($type, $variable);

View File

@ -7,6 +7,7 @@ namespace Rector\Core\PhpDoc;
use PhpParser\Comment;
use PhpParser\Node;
use Rector\Core\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class CommentCombiner
{
@ -44,6 +45,6 @@ final class CommentCombiner
$commentContent .= $comment->getText() . PHP_EOL;
}
$newNode->setAttribute('comments', [new Comment($commentContent)]);
$newNode->setAttribute(AttributeKey::COMMENTS, [new Comment($commentContent)]);
}
}

View File

@ -14,6 +14,7 @@ use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class NodeTransformer
{
@ -91,7 +92,7 @@ final class NodeTransformer
foreach ($arrayNode->items as $arrayItem) {
$expressionNode = new Expression(new Yield_($arrayItem->value, $arrayItem->key));
if ($arrayItem->getComments() !== []) {
$expressionNode->setAttribute('comments', $arrayItem->getComments());
$expressionNode->setAttribute(AttributeKey::COMMENTS, $arrayItem->getComments());
}
$yieldNodes[] = $expressionNode;

View File

@ -8,6 +8,7 @@ use Nette\Utils\FileSystem;
use PhpParser\Node;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* This could be part of @see AbstractRector, but decopuling to trait
@ -79,12 +80,12 @@ trait BetterStandardPrinterTrait
{
// remove comments, only content is relevant
$singleNode = clone $singleNode;
$singleNode->setAttribute('comments', null);
$singleNode->setAttribute(AttributeKey::COMMENTS, null);
foreach ($availableNodes as $availableNode) {
// remove comments, only content is relevant
$availableNode = clone $availableNode;
$availableNode->setAttribute('comments', null);
$availableNode->setAttribute(AttributeKey::COMMENTS, null);
if ($this->areNodesEqual($singleNode, $availableNode)) {
return true;

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Scalar\String_;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class BetterStandardPrinterTest extends AbstractKernelTestCase
@ -28,7 +29,7 @@ final class BetterStandardPrinterTest extends AbstractKernelTestCase
public function testNodeWithComment(): void
{
$string = new String_('value');
$string->setAttribute('comments', [new Comment('// todo: fix')]);
$string->setAttribute(AttributeKey::COMMENTS, [new Comment('// todo: fix')]);
$printed = $this->betterStandardPrinter->print($string) . PHP_EOL;
$this->assertStringEqualsFile(__DIR__ . '/Source/expected_code_with_comment.php.inc', $printed);