Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DocBlock] Update docblock contents right in the rule #4999

Merged
merged 16 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 2 additions & 24 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\ShortenedIdentifierTypeNode;
Expand All @@ -54,8 +53,6 @@ final class PhpDocInfo

private readonly PhpDocNode $originalPhpDocNode;

private bool $hasChanged = false;

public function __construct(
private readonly PhpDocNode $phpDocNode,
private readonly BetterTokenIterator $betterTokenIterator,
Expand Down Expand Up @@ -374,30 +371,11 @@ public function getTemplateTagValueNodes(): array
* @deprecated Change doc block and print directly in the node instead
* @internal
* Should be handled by attributes of phpdoc node - if stard_and_end is missing in one of nodes, it has been changed
* Similar to missing original node in php-aprser
*
* @api
*/
public function markAsChanged(): void
{
$this->hasChanged = true;
}

public function hasChanged(): bool
{
if ($this->isNewNode()) {
return true;
}

if ($this->hasChanged) {
return true;
}

// has a single node with missing start_end
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$changedPhpDocNodeVisitor = new ChangedPhpDocNodeVisitor();
$phpDocNodeTraverser->addPhpDocNodeVisitor($changedPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($this->phpDocNode);

return $changedPhpDocNodeVisitor->hasChanged();
}

public function makeMultiLined(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public function __construct(
*
* @param string[] $oldToNewClasses
*/
public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo, array $oldToNewClasses): void
public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo, array $oldToNewClasses): bool
{
// @todo union with bool returns

$this->processAssertChoiceTagValueNode($oldToNewClasses, $phpDocInfo);
$this->processDoctrineRelationTagValueNode($node, $oldToNewClasses, $phpDocInfo);
$this->processSerializerTypeTagValueNode($oldToNewClasses, $phpDocInfo);
Expand Down

This file was deleted.

35 changes: 0 additions & 35 deletions packages/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,6 @@ public function __construct(
) {
}

public function updateNodeWithPhpDocInfo(Node $node): void
{
// nothing to change? don't save it
$phpDocInfo = $this->resolveChangedPhpDocInfo($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}

$phpDoc = $this->printPhpDocInfoToString($phpDocInfo);

// make sure, that many separated comments are not removed
if ($phpDoc === '') {
$this->setCommentsAttribute($node);

return;
}

// this is needed to remove duplicated // commentsAsText
$node->setDocComment(new Doc($phpDoc));
}

public function updateRefactoredNodeWithPhpDocInfo(Node $node): void
{
// nothing to change? don't save it
Expand All @@ -63,20 +42,6 @@ private function setCommentsAttribute(Node $node): void
$node->setAttribute(AttributeKey::COMMENTS, $comments);
}

private function resolveChangedPhpDocInfo(Node $node): ?PhpDocInfo
{
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}

if (! $phpDocInfo->hasChanged()) {
return null;
}

return $phpDocInfo;
}

private function printPhpDocInfoToString(PhpDocInfo $phpDocInfo): string
{
if ($phpDocInfo->isNewNode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function __construct(
/**
* @param OldToNewType[] $oldToNewTypes
*/
public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes): void
public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes): bool
{
if ($oldToNewTypes === []) {
return;
return false;
}

$phpDocNodeTraverser = new PhpDocNodeTraverser();
Expand All @@ -31,5 +31,7 @@ public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes):
$this->classRenamePhpDocNodeVisitor->setOldToNewTypes($oldToNewTypes);

$phpDocNodeTraverser->traverse($phpDocInfo->getPhpDocNode());

return $this->classRenamePhpDocNodeVisitor->hasChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ public function __construct(
) {
}

public function importNames(PhpDocNode $phpDocNode, Node $node): void
public function importNames(PhpDocNode $phpDocNode, Node $node): bool
{
if ($phpDocNode->children === []) {
return;
return false;
}

$this->nameImportingPhpDocNodeVisitor->setCurrentNode($node);

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->nameImportingPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($phpDocNode);

return $this->nameImportingPhpDocNodeVisitor->hasChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
*/
private array $oldToNewTypes = [];

private bool $hasChanged = false;

public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly CurrentNodeProvider $currentNodeProvider,
Expand All @@ -45,6 +47,8 @@ public function beforeTraverse(Node $node): void
if ($this->oldToNewTypes === []) {
throw new ShouldNotHappenException('Configure "$oldToNewClasses" first');
}

$this->hasChanged = true;
}

public function enterNode(Node $node): ?Node
Expand Down Expand Up @@ -90,6 +94,8 @@ public function enterNode(Node $node): ?Node
$newTypeNode->setAttribute(PhpDocAttributeKey::PARENT, $parentType);
}

$this->hasChanged = true;

return $newTypeNode;
}

Expand All @@ -104,6 +110,11 @@ public function setOldToNewTypes(array $oldToNewTypes): void
$this->oldToNewTypes = $oldToNewTypes;
}

public function hasChanged(): bool
{
return $this->hasChanged;
}

private function resolveNamespacedName(
IdentifierTypeNode $identifierTypeNode,
PhpParserNode $phpParserNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
{
private ?PhpParserNode $currentPhpParserNode = null;

private bool $hasChanged = false;

public function __construct(
private readonly ClassNameImportSkipper $classNameImportSkipper,
private readonly UseNodesToAddCollector $useNodesToAddCollector,
Expand Down Expand Up @@ -61,7 +63,6 @@ public function enterNode(Node $node): ?Node
}

$staticType = $this->identifierTypeMapper->mapIdentifierTypeNode($node, $this->currentPhpParserNode);

if (! $staticType instanceof FullyQualifiedObjectType) {
return null;
}
Expand All @@ -85,9 +86,15 @@ public function enterNode(Node $node): ?Node

public function setCurrentNode(PhpParserNode $phpParserNode): void
{
$this->hasChanged = false;
$this->currentPhpParserNode = $phpParserNode;
}

public function hasChanged(): bool
{
return $this->hasChanged;
}

private function processFqnNameImport(
PhpParserNode $phpParserNode,
IdentifierTypeNode $identifierTypeNode,
Expand All @@ -100,6 +107,7 @@ private function processFqnNameImport(
return null;
}

// standardize to FQN
if (str_starts_with($fullyQualifiedObjectType->getClassName(), '@')) {
$fullyQualifiedObjectType = new FullyQualifiedObjectType(ltrim(
$fullyQualifiedObjectType->getClassName(),
Expand All @@ -125,6 +133,8 @@ private function processFqnNameImport(

if ($this->shouldImport($newNode, $identifierTypeNode, $fullyQualifiedObjectType)) {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
$this->hasChanged = true;

return $newNode;
}

Expand Down
34 changes: 24 additions & 10 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\CodingStyle\Node\NameImporter;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
Expand All @@ -38,7 +40,8 @@ public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly CurrentFileProvider $currentFileProvider,
private readonly UseImportsResolver $useImportsResolver,
private readonly AliasNameResolver $aliasNameResolver
private readonly AliasNameResolver $aliasNameResolver,
private readonly DocBlockUpdater $docBlockUpdater,
) {
}

Expand All @@ -53,25 +56,36 @@ public function enterNode(Node $node): ?Node
return null;
}

$currentStmt = current($file->getNewStmts());
if ($currentStmt instanceof FileWithoutNamespace && current($currentStmt->stmts) instanceof InlineHTML) {
$firstStmt = current($file->getNewStmts());
if ($firstStmt instanceof FileWithoutNamespace && current($firstStmt->stmts) instanceof InlineHTML) {
return null;
}

if ($node instanceof Name) {
return $this->processNodeName($node, $file);
}

if (($node instanceof Stmt || $node instanceof Param) && SimpleParameterProvider::provideBoolParameter(
Option::AUTO_IMPORT_DOC_BLOCK_NAMES
)) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);
if (! $node instanceof Stmt && ! $node instanceof Param) {
return null;
}

return $node;
$shouldImportDocBlocks = SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_DOC_BLOCK_NAMES);
if (! $shouldImportDocBlocks) {
return null;
}

return null;
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}

$hasDocChanged = $this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);
if (! $hasDocChanged) {
return null;
}

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}

private function processNodeName(Name $name, File $file): ?Node
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,5 @@ parameters:

# remove in next step
- '#Call to deprecated method markAsChanged\(\) of class Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo#'
- '#Method "(importNames|renamePhpDocType)\(\)" returns bool type, so the name should start with is/has/was#'
- '#Call to an undefined method Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo\:\:markAsChanged\(\)#'
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector;
use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector;
Expand Down Expand Up @@ -57,6 +58,10 @@
__DIR__ . '/src/Configuration/ConfigInitializer.php',
],

RemoveEmptyClassMethodRector::class => [
__DIR__ . '/packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
],

// resolve later
RenameParamToMatchTypeRector::class => [
__DIR__ . '/src/Console/Command/ListRulesCommand.php',
Expand Down