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 @@ -241,42 +241,12 @@ public function getTagsByName(Node $node, string $name): array

public function addVarTag(Node $node, string $type): void
{
// there might be no phpdoc at all
if ($node->getDocComment() !== null) {
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocNode = $phpDocInfo->getPhpDocNode();

$varTagValueNode = new AttributeAwareVarTagValueNode(new AttributeAwareIdentifierTypeNode(
'\\' . $type
), '', '');
$phpDocNode->children[] = new AttributeAwarePhpDocTagNode('@var', $varTagValueNode);

$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
} else {
// create completely new docblock
$varDocComment = sprintf("/**\n * @var %s\n */", $type);
$node->setDocComment(new Doc($varDocComment));
}
$this->addTypeSpecificTag($node, 'var', $type);
}

public function addReturnTag(Node $node, string $type): void
{
// there might be no phpdoc at all
if ($node->getDocComment() !== null) {
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocNode = $phpDocInfo->getPhpDocNode();

$varTagValueNode = new AttributeAwareVarTagValueNode(new AttributeAwareIdentifierTypeNode(
'\\' . $type
), '', '');
$phpDocNode->children[] = new AttributeAwarePhpDocTagNode('@return', $varTagValueNode);

$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
} else {
// create completely new docblock
$varDocComment = sprintf("/**\n * @return %s\n */", $type);
$node->setDocComment(new Doc($varDocComment));
}
$this->addTypeSpecificTag($node, 'return', $type);
}

/**
Expand Down Expand Up @@ -455,6 +425,26 @@ public function changeUnderscoreType(Node $node, string $namespacePrefix, ?array
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
}

private function addTypeSpecificTag(Node $node, string $name, string $type): void
{
// there might be no phpdoc at all
if ($node->getDocComment() !== null) {
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocNode = $phpDocInfo->getPhpDocNode();

$varTagValueNode = new AttributeAwareVarTagValueNode(new AttributeAwareIdentifierTypeNode(
'\\' . $type
), '', '');
$phpDocNode->children[] = new AttributeAwarePhpDocTagNode('@' . $name, $varTagValueNode);

$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
} else {
// create completely new docblock
$varDocComment = sprintf("/**\n * @%s %s\n */", $name, $type);
$node->setDocComment(new Doc($varDocComment));
}
}

private function updateNodeWithPhpDocInfo(Node $node, PhpDocInfo $phpDocInfo): void
{
// skip if has no doc comment
Expand Down
19 changes: 16 additions & 3 deletions src/Guard/RectorGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rector\Guard;

use Rector\Exception\NoRectorsLoadedException;
use Rector\FileSystemRector\FileSystemFileProcessor;
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;

final class RectorGuard
Expand All @@ -12,14 +13,26 @@ final class RectorGuard
*/
private $rectorNodeTraverser;

public function __construct(RectorNodeTraverser $rectorNodeTraverser)
{
/**
* @var FileSystemFileProcessor
*/
private $fileSystemFileProcessor;

public function __construct(
RectorNodeTraverser $rectorNodeTraverser,
FileSystemFileProcessor $fileSystemFileProcessor
) {
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->fileSystemFileProcessor = $fileSystemFileProcessor;
}

public function ensureSomeRectorsAreRegistered(): void
{
if ($this->rectorNodeTraverser->getRectorCount() !== 0) {
if ($this->rectorNodeTraverser->getRectorCount() > 0) {
return;
}

if ($this->fileSystemFileProcessor->getFileSystemRectorsCount() > 0) {
return;
}

Expand Down